Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try @mediapipe/task-visionfor background segmentation #13209

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/story-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@googleforcreators/units": "*",
"@googleforcreators/url": "*",
"@hello-pangea/color-picker": "^3.2.2",
"@mediapipe/selfie_segmentation": "^0.1.1675465747",
"@mediapipe/tasks-vision": "^0.1.0-alpha-8",
"@pioug/colorthief": "^3.0.1",
"@wmik/use-media-recorder": "^1.6.5-beta.0",
"blurhash": "^2.0.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,75 +98,92 @@

const selfieSegmentation = useRef();

const onSelfieSegmentationResults = (results) => {
const onSelfieSegmentationResults = async (masks) => {
const canvas = canvasRef.current;
if (!canvas || !results.image || results.image.width === 0) {
if (!canvas || !masks) {

Check warning on line 103 in packages/story-editor/src/components/mediaRecording/playbackMedia.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/mediaRecording/playbackMedia.js#L103

Added line #L103 was not covered by tests
return;
}
const context = canvasRef.current.getContext('2d');
const canvasBlur = 'filter' in CanvasRenderingContext2D.prototype;

context.save();

const segmentationMask = new Uint8ClampedArray(

Check warning on line 110 in packages/story-editor/src/components/mediaRecording/playbackMedia.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/mediaRecording/playbackMedia.js#L110

Added line #L110 was not covered by tests
canvas.width * canvas.height * 4
);
const mask = masks[0];
for (let i = 0; i < mask.length; i++) {
const isBackround = mask[i] === 0;
segmentationMask[i * 4] = isBackround ? 0 : 255;
segmentationMask[i * 4 + 1] = isBackround ? 0 : 255;
segmentationMask[i * 4 + 2] = isBackround ? 0 : 255;
segmentationMask[i * 4 + 3] = isBackround ? 0 : 255;

Check warning on line 119 in packages/story-editor/src/components/mediaRecording/playbackMedia.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/mediaRecording/playbackMedia.js#L113-L119

Added lines #L113 - L119 were not covered by tests
}
const segmentationMaskBitMap = await createImageBitmap(

Check warning on line 121 in packages/story-editor/src/components/mediaRecording/playbackMedia.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/mediaRecording/playbackMedia.js#L121

Added line #L121 was not covered by tests
new ImageData(segmentationMask, canvas.width, canvas.height)
);

if (!canvasBlur) {
context.drawImage(results.image, 0, 0, canvas.width, canvas.height);
context.drawImage(streamNode, 0, 0, canvas.width, canvas.height);

Check warning on line 126 in packages/story-editor/src/components/mediaRecording/playbackMedia.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/mediaRecording/playbackMedia.js#L126

Added line #L126 was not covered by tests

blur(context, BACKGROUND_BLUR_PX);

context.globalCompositeOperation = 'destination-out';
context.drawImage(
results.segmentationMask,
segmentationMaskBitMap,
0,
0,
canvas.width,
canvas.height
);
context.globalCompositeOperation = 'destination-over';
context.drawImage(results.image, 0, 0, canvas.width, canvas.height);
context.drawImage(streamNode, 0, 0, canvas.width, canvas.height);

Check warning on line 139 in packages/story-editor/src/components/mediaRecording/playbackMedia.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/mediaRecording/playbackMedia.js#L139

Added line #L139 was not covered by tests
} else {
context.globalCompositeOperation = 'copy';
context.filter = `blur(${BACKGROUND_BLUR_PX}px)`;
context.drawImage(
results.segmentationMask,
segmentationMaskBitMap,
0,
0,
canvas.width,
canvas.height
);

context.globalCompositeOperation = 'source-in';
context.filter = 'none';
context.drawImage(results.image, 0, 0, canvas.width, canvas.height);
context.filter = `none`;
context.drawImage(streamNode, 0, 0, canvas.width, canvas.height);

Check warning on line 153 in packages/story-editor/src/components/mediaRecording/playbackMedia.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/mediaRecording/playbackMedia.js#L152-L153

Added lines #L152 - L153 were not covered by tests

context.globalCompositeOperation = 'destination-over';
context.filter = `blur(${BACKGROUND_BLUR_PX}px)`;
context.drawImage(results.image, 0, 0, canvas.width, canvas.height);
context.drawImage(streamNode, 0, 0, canvas.width, canvas.height);

Check warning on line 157 in packages/story-editor/src/components/mediaRecording/playbackMedia.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/mediaRecording/playbackMedia.js#L157

Added line #L157 was not covered by tests
}

context.restore();
};

useEffect(() => {
if (!hasVideoEffect || selfieSegmentation.current) {
if (selfieSegmentation.current) {
return;
}

(async () => {
const { SelfieSegmentation } = await import(
/* webpackChunkName: "chunk-selfie-segmentation" */ '@mediapipe/selfie_segmentation'
const { ImageSegmenter, FilesetResolver } = await import(
/* webpackChunkName: "chunk-selfie-segmentation" */ '@mediapipe/tasks-vision'
);

selfieSegmentation.current = new SelfieSegmentation({
// TODO: Consider fetching from wp.stories.google instead.
locateFile: (file) =>
`https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation/${file}`,
});

selfieSegmentation.current.setOptions({
modelSelection: 1,
});
const vision = await FilesetResolver.forVisionTasks(
'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.1.0-alpha-8/wasm'
);

await selfieSegmentation.current.initialize();
selfieSegmentation.current = await ImageSegmenter.createFromOptions(
vision,
{
baseOptions: {
modelAssetPath:
'https://storage.googleapis.com/mediapipe-tasks/image_segmenter/selfie_segm_128_128_3.tflite',
},
runningMode: 'VIDEO',
}
);
})();
}, [hasVideoEffect]);

Expand All @@ -183,11 +200,14 @@
setCanvasStream(canvasStreamRaw);
}
if (videoEffect === VIDEO_EFFECTS.BLUR && selfieSegmentation.current) {
selfieSegmentation.current.onResults(onSelfieSegmentationResults);
const sendFrame = async () => {
if (streamNode && streamNode.videoWidth && canvasRef.current) {
try {
await selfieSegmentation.current.send({ image: streamNode });
await selfieSegmentation.current.segmentForVideo(

Check warning on line 206 in packages/story-editor/src/components/mediaRecording/playbackMedia.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/mediaRecording/playbackMedia.js#L206

Added line #L206 was not covered by tests
streamNode,
performance.now(),
onSelfieSegmentationResults
);
} catch (e) {
// We can't do much about the WASM memory issue.
}
Expand Down