|
| 1 | +import "@tensorflow/tfjs-backend-webgl"; |
| 2 | +import * as bodyPix from "@tensorflow-models/body-pix"; |
| 3 | +import Stats from "stats.js"; |
| 4 | +import { useEffect, useRef } from "react"; |
| 5 | +import * as partColorScales from '/src/utils/part-color-scales'; |
| 6 | +import {drawKeypoints, drawSkeleton} from '/src/utils/draw'; |
| 7 | + |
| 8 | +export default function BodyPix() { |
| 9 | + let dat, stats |
| 10 | + const state = { |
| 11 | + video: null, |
| 12 | + stream: null, |
| 13 | + net: null, |
| 14 | + videoConstraints: {}, |
| 15 | + // Triggers the TensorFlow model to reload |
| 16 | + changingArchitecture: false, |
| 17 | + changingMultiplier: false, |
| 18 | + changingStride: false, |
| 19 | + changingResolution: false, |
| 20 | + changingQuantBytes: false, |
| 21 | + }; |
| 22 | + const guiState = { |
| 23 | + algorithm: "multi-person-instance", |
| 24 | + estimate: "partmap", |
| 25 | + camera: null, |
| 26 | + flipHorizontal: true, |
| 27 | + input: { |
| 28 | + architecture: "MobileNetV1", |
| 29 | + outputStride: 16, |
| 30 | + internalResolution: "low", |
| 31 | + multiplier: 0.5, |
| 32 | + quantBytes: 2, |
| 33 | + }, |
| 34 | + multiPersonDecoding: { |
| 35 | + maxDetections: 5, |
| 36 | + scoreThreshold: 0.3, |
| 37 | + nmsRadius: 20, |
| 38 | + numKeypointForMatching: 17, |
| 39 | + refineSteps: 10, |
| 40 | + }, |
| 41 | + segmentation: { |
| 42 | + segmentationThreshold: 0.7, |
| 43 | + effect: "mask", |
| 44 | + maskBackground: true, |
| 45 | + opacity: 0.7, |
| 46 | + backgroundBlurAmount: 3, |
| 47 | + maskBlurAmount: 0, |
| 48 | + edgeBlurAmount: 3, |
| 49 | + }, |
| 50 | + partMap: { |
| 51 | + colorScale: "rainbow", |
| 52 | + effect: "partMap", |
| 53 | + segmentationThreshold: 0.5, |
| 54 | + opacity: 0.9, |
| 55 | + blurBodyPartAmount: 3, |
| 56 | + bodyPartEdgeBlurAmount: 3, |
| 57 | + }, |
| 58 | + showFps: false, |
| 59 | + }; |
| 60 | + |
| 61 | + const canvas = useRef() |
| 62 | + const videoElement = useRef() |
| 63 | + |
| 64 | + useEffect(async () => { |
| 65 | + stats = new Stats(); |
| 66 | + dat = require('dat.gui') |
| 67 | + await loadBodyPix(); |
| 68 | + await loadVideo(guiState.camera); |
| 69 | + |
| 70 | + let cameras = await getVideoInputs(); |
| 71 | + |
| 72 | + // setupFPS(); |
| 73 | + // setupGui(cameras); |
| 74 | + |
| 75 | + segmentBodyInRealTime(); |
| 76 | + }); |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="max-w-5xl"> |
| 80 | + <video ref={videoElement} playsinline style={{ display: 'none' }}></video> |
| 81 | + <canvas ref={canvas}></canvas> |
| 82 | + </div> |
| 83 | + ); |
| 84 | + |
| 85 | + async function getVideoInputs() { |
| 86 | + if (!window.navigator.mediaDevices || !window.navigator.mediaDevices.enumerateDevices) { |
| 87 | + console.log('enumerateDevices() not supported.'); |
| 88 | + return []; |
| 89 | + } |
| 90 | + |
| 91 | + const devices = await window.navigator.mediaDevices.enumerateDevices(); |
| 92 | + |
| 93 | + const videoDevices = devices.filter(device => device.kind === 'videoinput'); |
| 94 | + |
| 95 | + return videoDevices; |
| 96 | + } |
| 97 | + |
| 98 | + async function setupCamera(cameraLabel) { |
| 99 | + if (!window.navigator.mediaDevices || !window.navigator.mediaDevices.getUserMedia) { |
| 100 | + throw new Error( |
| 101 | + "Browser API window.navigator.mediaDevices.getUserMedia not available" |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + // stopExistingVideoCapture(); |
| 106 | + |
| 107 | + const videoConstraints = { deviceId: null, facingMode: 'user', width: 400, |
| 108 | + height: 400,}; |
| 109 | + |
| 110 | + const stream = await window.navigator.mediaDevices.getUserMedia({ |
| 111 | + audio: false, |
| 112 | + video: videoConstraints |
| 113 | + }); |
| 114 | + videoElement.current.srcObject = stream; |
| 115 | + |
| 116 | + return new Promise((resolve) => { |
| 117 | + videoElement.current.onloadedmetadata = () => { |
| 118 | + videoElement.current.width = videoElement.current.videoWidth; |
| 119 | + videoElement.current.height = videoElement.current.videoHeight; |
| 120 | + resolve(videoElement.current); |
| 121 | + }; |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + async function loadVideo(cameraLabel) { |
| 126 | + await setupCamera(cameraLabel); |
| 127 | + videoElement.current.play(); |
| 128 | + } |
| 129 | + |
| 130 | + async function loadBodyPix() { |
| 131 | + state.net = await bodyPix.load({ |
| 132 | + architecture: guiState.input.architecture, |
| 133 | + outputStride: guiState.input.outputStride, |
| 134 | + multiplier: guiState.input.multiplier, |
| 135 | + quantBytes: guiState.input.quantBytes, |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + function drawPoses(personOrPersonPartSegmentation, flipHorizontally, ctx) { |
| 140 | + if (Array.isArray(personOrPersonPartSegmentation)) { |
| 141 | + personOrPersonPartSegmentation.forEach(personSegmentation => { |
| 142 | + let pose = personSegmentation.pose; |
| 143 | + if (flipHorizontally) { |
| 144 | + pose = bodyPix.flipPoseHorizontal(pose, personSegmentation.width); |
| 145 | + } |
| 146 | + drawKeypoints(pose.keypoints, 0.1, ctx); |
| 147 | + drawSkeleton(pose.keypoints, 0.1, ctx); |
| 148 | + }); |
| 149 | + } else { |
| 150 | + personOrPersonPartSegmentation.allPoses.forEach(pose => { |
| 151 | + if (flipHorizontally) { |
| 152 | + pose = bodyPix.flipPoseHorizontal( |
| 153 | + pose, personOrPersonPartSegmentation.width); |
| 154 | + } |
| 155 | + drawKeypoints(pose.keypoints, 0.1, ctx); |
| 156 | + drawSkeleton(pose.keypoints, 0.1, ctx); |
| 157 | + }) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + function segmentBodyInRealTime() { |
| 162 | + async function bodySegmentationFrame() { |
| 163 | + // if changing the model or the camera, wait a second for it to complete |
| 164 | + // then try again. |
| 165 | + if ( |
| 166 | + state.changingArchitecture || |
| 167 | + state.changingMultiplier || |
| 168 | + state.changingCamera || |
| 169 | + state.changingStride || |
| 170 | + state.changingQuantBytes |
| 171 | + ) { |
| 172 | + console.log("load model..."); |
| 173 | + loadBodyPix(); |
| 174 | + state.changingArchitecture = false; |
| 175 | + state.changingMultiplier = false; |
| 176 | + state.changingStride = false; |
| 177 | + state.changingQuantBytes = false; |
| 178 | + } |
| 179 | + |
| 180 | + // Begin monitoring code for frames per second |
| 181 | + stats.begin(); |
| 182 | + |
| 183 | + const flipHorizontally = guiState.flipHorizontal; |
| 184 | + |
| 185 | + if (!canvas.current) return |
| 186 | + if (!videoElement.current) return |
| 187 | + switch (guiState.estimate) { |
| 188 | + case "segmentation": |
| 189 | + const multiPersonSegmentation = await estimateSegmentation(); |
| 190 | + switch (guiState.segmentation.effect) { |
| 191 | + case "mask": |
| 192 | + const ctx = canvas.current.getContext("2d"); |
| 193 | + const foregroundColor = { r: 255, g: 255, b: 255, a: 255 }; |
| 194 | + const backgroundColor = { r: 0, g: 0, b: 0, a: 255 }; |
| 195 | + const mask = bodyPix.toMask( |
| 196 | + multiPersonSegmentation, |
| 197 | + foregroundColor, |
| 198 | + backgroundColor, |
| 199 | + true |
| 200 | + ); |
| 201 | + |
| 202 | + bodyPix.drawMask( |
| 203 | + canvas.current, |
| 204 | + videoElement.current, |
| 205 | + mask, |
| 206 | + guiState.segmentation.opacity, |
| 207 | + guiState.segmentation.maskBlurAmount, |
| 208 | + flipHorizontally |
| 209 | + ); |
| 210 | + drawPoses(multiPersonSegmentation, flipHorizontally, ctx); |
| 211 | + break; |
| 212 | + case "bokeh": |
| 213 | + bodyPix.drawBokehEffect( |
| 214 | + canvas.current, |
| 215 | + videoElement.current, |
| 216 | + multiPersonSegmentation, |
| 217 | + +guiState.segmentation.backgroundBlurAmount, |
| 218 | + guiState.segmentation.edgeBlurAmount, |
| 219 | + flipHorizontally |
| 220 | + ); |
| 221 | + break; |
| 222 | + } |
| 223 | + |
| 224 | + break; |
| 225 | + case "partmap": |
| 226 | + const ctx = canvas.current.getContext("2d"); |
| 227 | + const multiPersonPartSegmentation = await estimatePartSegmentation(); |
| 228 | + const coloredPartImageData = bodyPix.toColoredPartMask( |
| 229 | + multiPersonPartSegmentation, |
| 230 | + partColorScales[guiState.partMap.colorScale] |
| 231 | + ); |
| 232 | + |
| 233 | + const maskBlurAmount = 0; |
| 234 | + switch (guiState.partMap.effect) { |
| 235 | + case "pixelation": |
| 236 | + const pixelCellWidth = 10.0; |
| 237 | + |
| 238 | + bodyPix.drawPixelatedMask( |
| 239 | + canvas.current, |
| 240 | + videoElement.current, |
| 241 | + coloredPartImageData, |
| 242 | + guiState.partMap.opacity, |
| 243 | + maskBlurAmount, |
| 244 | + flipHorizontally, |
| 245 | + pixelCellWidth |
| 246 | + ); |
| 247 | + break; |
| 248 | + case "partMap": |
| 249 | + if(!videoElement.current) return |
| 250 | + bodyPix.drawMask( |
| 251 | + canvas.current, |
| 252 | + videoElement.current, |
| 253 | + coloredPartImageData, |
| 254 | + guiState.opacity, |
| 255 | + maskBlurAmount, |
| 256 | + flipHorizontally |
| 257 | + ); |
| 258 | + break; |
| 259 | + case "blurBodyPart": |
| 260 | + const blurBodyPartIds = [0, 1]; |
| 261 | + bodyPix.blurBodyPart( |
| 262 | + canvas.current, |
| 263 | + videoElement.current, |
| 264 | + multiPersonPartSegmentation, |
| 265 | + blurBodyPartIds, |
| 266 | + guiState.partMap.blurBodyPartAmount, |
| 267 | + guiState.partMap.edgeBlurAmount, |
| 268 | + flipHorizontally |
| 269 | + ); |
| 270 | + } |
| 271 | + drawPoses(multiPersonPartSegmentation, flipHorizontally, ctx); |
| 272 | + break; |
| 273 | + default: |
| 274 | + break; |
| 275 | + } |
| 276 | + |
| 277 | + // End monitoring code for frames per second |
| 278 | + stats.end(); |
| 279 | + |
| 280 | + requestAnimationFrame(bodySegmentationFrame); |
| 281 | + } |
| 282 | + |
| 283 | + bodySegmentationFrame(); |
| 284 | + } |
| 285 | + |
| 286 | + async function estimatePartSegmentation() { |
| 287 | + switch (guiState.algorithm) { |
| 288 | + case 'multi-person-instance': |
| 289 | + return await state.net.segmentMultiPersonParts(videoElement.current, { |
| 290 | + internalResolution: guiState.input.internalResolution, |
| 291 | + segmentationThreshold: guiState.segmentation.segmentationThreshold, |
| 292 | + maxDetections: guiState.multiPersonDecoding.maxDetections, |
| 293 | + scoreThreshold: guiState.multiPersonDecoding.scoreThreshold, |
| 294 | + nmsRadius: guiState.multiPersonDecoding.nmsRadius, |
| 295 | + numKeypointForMatching: |
| 296 | + guiState.multiPersonDecoding.numKeypointForMatching, |
| 297 | + refineSteps: guiState.multiPersonDecoding.refineSteps |
| 298 | + }); |
| 299 | + case 'person': |
| 300 | + return await state.net.segmentPersonParts(videoElement.current, { |
| 301 | + internalResolution: guiState.input.internalResolution, |
| 302 | + segmentationThreshold: guiState.segmentation.segmentationThreshold, |
| 303 | + maxDetections: guiState.multiPersonDecoding.maxDetections, |
| 304 | + scoreThreshold: guiState.multiPersonDecoding.scoreThreshold, |
| 305 | + nmsRadius: guiState.multiPersonDecoding.nmsRadius, |
| 306 | + }); |
| 307 | + default: |
| 308 | + break; |
| 309 | + }; |
| 310 | + return multiPersonPartSegmentation; |
| 311 | + } |
| 312 | +} |
0 commit comments