forked from google-ai-edge/mediapipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add drawConfidenceMask() to our public API
PiperOrigin-RevId: 582647409
- Loading branch information
1 parent
e440a4d
commit 47e2178
Showing
7 changed files
with
309 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
mediapipe/tasks/web/vision/core/drawing_utils_confidence_mask.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* Copyright 2023 The MediaPipe Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {assertNotNull, MPImageShaderContext} from '../../../../tasks/web/vision/core/image_shader_context'; | ||
import {ImageSource} from '../../../../web/graph_runner/graph_runner'; | ||
|
||
/** | ||
* A fragment shader that blends a default image and overlay texture based on an | ||
* input texture that contains confidence values. | ||
*/ | ||
const FRAGMENT_SHADER = ` | ||
precision mediump float; | ||
uniform sampler2D maskTexture; | ||
uniform sampler2D defaultTexture; | ||
uniform sampler2D overlayTexture; | ||
varying vec2 vTex; | ||
void main() { | ||
float confidence = texture2D(maskTexture, vTex).r; | ||
vec4 defaultColor = texture2D(defaultTexture, vTex); | ||
vec4 overlayColor = texture2D(overlayTexture, vTex); | ||
// Apply the alpha from the overlay and merge in the default color | ||
overlayColor = mix(defaultColor, overlayColor, overlayColor.a); | ||
gl_FragColor = mix(defaultColor, overlayColor, confidence); | ||
} | ||
`; | ||
|
||
/** A drawing util class for confidence masks. */ | ||
export class ConfidenceMaskShaderContext extends MPImageShaderContext { | ||
defaultTexture?: WebGLTexture; | ||
overlayTexture?: WebGLTexture; | ||
defaultTextureUniform?: WebGLUniformLocation; | ||
overlayTextureUniform?: WebGLUniformLocation; | ||
maskTextureUniform?: WebGLUniformLocation; | ||
|
||
protected override getFragmentShader(): string { | ||
return FRAGMENT_SHADER; | ||
} | ||
|
||
protected override setupTextures(): void { | ||
const gl = this.gl!; | ||
gl.activeTexture(gl.TEXTURE0); | ||
this.defaultTexture = this.createTexture(gl); | ||
gl.activeTexture(gl.TEXTURE1); | ||
this.overlayTexture = this.createTexture(gl); | ||
} | ||
|
||
protected override setupShaders(): void { | ||
super.setupShaders(); | ||
const gl = this.gl!; | ||
this.defaultTextureUniform = assertNotNull( | ||
gl.getUniformLocation(this.program!, 'defaultTexture'), | ||
'Uniform location'); | ||
this.overlayTextureUniform = assertNotNull( | ||
gl.getUniformLocation(this.program!, 'overlayTexture'), | ||
'Uniform location'); | ||
this.maskTextureUniform = assertNotNull( | ||
gl.getUniformLocation(this.program!, 'maskTexture'), | ||
'Uniform location'); | ||
} | ||
|
||
protected override configureUniforms(): void { | ||
super.configureUniforms(); | ||
const gl = this.gl!; | ||
gl.uniform1i(this.defaultTextureUniform!, 0); | ||
gl.uniform1i(this.overlayTextureUniform!, 1); | ||
gl.uniform1i(this.maskTextureUniform!, 2); | ||
} | ||
|
||
bindAndUploadTextures( | ||
defaultImage: ImageSource, overlayImage: ImageSource, | ||
confidenceMask: WebGLTexture) { | ||
// TODO: We should avoid uploading textures from CPU to GPU | ||
// if the textures haven't changed. This can lead to drastic performance | ||
// slowdowns (~50ms per frame). Users can reduce the penalty by passing a | ||
// canvas object instead of ImageData/HTMLImageElement. | ||
const gl = this.gl!; | ||
gl.activeTexture(gl.TEXTURE0); | ||
gl.bindTexture(gl.TEXTURE_2D, this.defaultTexture!); | ||
gl.texImage2D( | ||
gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, defaultImage); | ||
|
||
gl.activeTexture(gl.TEXTURE1); | ||
gl.bindTexture(gl.TEXTURE_2D, this.overlayTexture!); | ||
gl.texImage2D( | ||
gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, overlayImage); | ||
|
||
gl.activeTexture(gl.TEXTURE2); | ||
gl.bindTexture(gl.TEXTURE_2D, confidenceMask); | ||
} | ||
|
||
unbindTextures() { | ||
const gl = this.gl!; | ||
gl.activeTexture(gl.TEXTURE0); | ||
gl.bindTexture(gl.TEXTURE_2D, null); | ||
|
||
gl.activeTexture(gl.TEXTURE1); | ||
gl.bindTexture(gl.TEXTURE_2D, null); | ||
|
||
gl.activeTexture(gl.TEXTURE2); | ||
gl.bindTexture(gl.TEXTURE_2D, null); | ||
} | ||
|
||
override close(): void { | ||
if (this.defaultTexture) { | ||
this.gl!.deleteTexture(this.defaultTexture); | ||
} | ||
if (this.overlayTexture) { | ||
this.gl!.deleteTexture(this.overlayTexture); | ||
} | ||
super.close(); | ||
} | ||
} |
Oops, something went wrong.