/
useHolistic.tsx
57 lines (48 loc) · 1.67 KB
/
useHolistic.tsx
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
import { useContext, useRef, useEffect, useState } from "react";
import { MYTYSDKContext, mytySDKContext } from "../context/MYTYSDKContext";
import { VideoDeviceContext, videoDeviceContext } from "../context/VideoDeviceContext";
import { Holistic } from '@mediapipe/holistic';
const useHolistic = () => {
const { processCapturedResult } = useContext(mytySDKContext) as MYTYSDKContext
const { currentCam, cameraElem } = useContext(videoDeviceContext) as VideoDeviceContext
const holisticRef = useRef<Holistic | null>(null);
const [ counter, setCounter ] = useState(0);
useEffect(() => {
if (!currentCam || !cameraElem) return;
holisticRef.current = new Holistic({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/holistic/${file}`;
}
})
holisticRef.current.setOptions({
selfieMode: true,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5,
modelComplexity: 1,
smoothLandmarks: true
})
holisticRef.current.onResults((result) => {
const motionData = {
face: result.faceLandmarks,
pose: result.poseLandmarks,
width: cameraElem.videoWidth,
height: cameraElem.videoHeight
}
setCounter(x => x+1)
processCapturedResult(JSON.stringify(motionData))
})
}, [processCapturedResult, currentCam, cameraElem]);
useEffect(() => {
if(counter > 0) updateFunc()
}, [counter])
const updateFunc = async () => {
if (cameraElem && holisticRef.current) {
await holisticRef.current.send({ image: cameraElem });
}
}
const startLoading = () => {
setCounter(1);
}
return { startLoading }
}
export default useHolistic