Spaces:
Running
Running
File size: 1,875 Bytes
fddab62 09a7c47 fddab62 09a7c47 fddab62 09a7c47 fddab62 09a7c47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import { FilesetResolver, InteractiveSegmenter, InteractiveSegmenterResult } from "@mediapipe/tasks-vision"
export type InteractiveVideoSegmenter = (videoFrame: TexImageSource, x: number, y: number) => Promise<InteractiveSegmenterResult>
const getInteractiveSegmenter = async (): Promise<InteractiveVideoSegmenter> => {
const vision = await FilesetResolver.forVisionTasks(
"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@latest/wasm"
);
const interactiveSegmenter = await InteractiveSegmenter.createFromOptions(vision, {
baseOptions: {
modelAssetPath:
"https://storage.googleapis.com/mediapipe-tasks/interactive_segmenter/ptm_512_hdt_ptm_woid.tflite"
},
outputCategoryMask: true,
outputConfidenceMasks: false,
});
const segmenter: InteractiveVideoSegmenter = (
videoFrame: TexImageSource,
x: number,
y: number
): Promise<InteractiveSegmenterResult> => {
return new Promise((resolve, reject) => {
interactiveSegmenter.segment(
videoFrame,
{
keypoint: { x, y }
},
(results) => {
resolve(results)
})
})
}
return segmenter
}
const globalState: { segmenter?: InteractiveVideoSegmenter } = {};
(async () => {
globalState.segmenter = globalState.segmenter || (await getInteractiveSegmenter())
})();
export async function segmentFrame(frame: TexImageSource, x: number, y: number): Promise<InteractiveSegmenterResult> {
console.log("segmentFrame: loading segmenter..")
globalState.segmenter = globalState.segmenter || (await getInteractiveSegmenter())
console.log("segmentFrame: segmenting..")
return globalState.segmenter(frame, x, y)
}
// to run:
// see doc:
// https://developers.google.com/mediapipe/solutions/vision/image_segmenter/web_js#video
// imageSegmenter.segmentForVideo(video, startTimeMs, callbackForVideo);
|