Skip to content

Instantly share code, notes, and snippets.

@meganetaaan
Created September 23, 2021 17:31
Show Gist options
  • Save meganetaaan/75672057786a0bcd5ff97af5708d8c76 to your computer and use it in GitHub Desktop.
Save meganetaaan/75672057786a0bcd5ff97af5708d8c76 to your computer and use it in GitHub Desktop.
@mediapipe/holistic で上半身のトラッキングを行うhooks
import { Camera } from "@mediapipe/camera_utils";
import { Holistic, Results } from "@mediapipe/holistic";
import { Ref, useCallback, useState } from "react";
export default function useMediaPipe(): [
Ref<HTMLVideoElement>,
Results | null
] {
const [results, setResults] = useState<Results | null>(null);
const ref = useCallback(async (videoElement: HTMLVideoElement | null) => {
if (videoElement == null) {
return;
}
const stream = await navigator.mediaDevices.getUserMedia({
video: true,
audio: false,
});
videoElement.srcObject = stream;
const holistic = new Holistic({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/holistic@0.4/${file}`;
},
});
holistic.setOptions({
selfieMode: true,
modelComplexity: 1,
smoothLandmarks: true,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5,
});
holistic.onResults((results) => {
console.log(results);
setResults(results);
});
const camera = new Camera(videoElement, {
onFrame: async () => {
await holistic.send({ image: videoElement });
},
});
camera.start();
return () => {
camera?.stop();
holistic?.close();
};
}, []);
return [ref, results];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment