Skip to content

Commit

Permalink
Refs #97. Added missing tera_processing.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
SBriere committed Nov 18, 2024
1 parent 0b2190d commit feaab05
Showing 1 changed file with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
let unblurredStream = undefined;

// Blur local video. Only first local stream is supported for now.
function blur(enable = true) {
if ( (enable && unblurredStream !== undefined) || (!enable && unblurredStream === undefined)) {
//console.error("Blur: Can't unblur or blur a stream that's already blurred or unblurred.")
return;
}

let videoTrack = document.getElementById("selfVideo").srcObject.getVideoTracks()[0];
let videoStream = document.getElementById("selfVideo").srcObject;
const canvas = new OffscreenCanvas(videoTrack.getSettings().width, videoTrack.getSettings().height);
const ctx = canvas.getContext("2d");

if (!enable){
videoTrack.stop();
document.getElementById("selfVideo").srcObject = unblurredStream;
unblurredStream = undefined;
}else{
const selfieSegmentation = new SelfieSegmentation({
locateFile: (file) =>
`https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation/${file}`,
});

selfieSegmentation.setOptions({
modelSelection: 0
});

selfieSegmentation.onResults(function(results){
ctx.save();
ctx.clearRect(
0,
0,
canvas.width,
canvas.height
);
ctx.drawImage(
results.segmentationMask,
0,
0,
canvas.width,
canvas.height
);

ctx.globalCompositeOperation = "source-in";
ctx.drawImage(
results.image,
0,
0,
canvas.width,
canvas.height
);

// Only overwrite missing pixels.
ctx.globalCompositeOperation = "destination-atop";
ctx.filter = `blur(16px)`;
ctx.drawImage(
results.image,
0,
0,
canvas.width,
canvas.height
);
ctx.restore();
});

const trackProcessor = new MediaStreamTrackProcessor({ track: videoTrack });
const trackGenerator = new MediaStreamTrackGenerator({ kind: 'video' });

const transformer = new TransformStream({
async transform(videoFrame, controller) {
videoFrame.width = videoFrame.displayWidth;
videoFrame.height = videoFrame.displayHeight;
await selfieSegmentation.send({ image: videoFrame });

const timestamp = videoFrame.timestamp;
const newFrame = new VideoFrame(canvas, {timestamp});

videoFrame.close();
controller.enqueue(newFrame);
}
});

trackProcessor.readable.pipeThrough(transformer).pipeTo(trackGenerator.writable);

const processedStream = new MediaStream();
unblurredStream = videoStream;
processedStream.addTrack(trackGenerator);
document.getElementById("selfVideo").srcObject = processedStream;

}
}

0 comments on commit feaab05

Please sign in to comment.