-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDetector.js
114 lines (106 loc) · 3.38 KB
/
Detector.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { useEffect, useState, useRef } from "react";
import * as mpHolistic from "@mediapipe/holistic";
import * as tf from "@tensorflow/tfjs";
import { onResults } from "./helperFunctions";
import LoadingComponent from "./LoadingComponent";
import { DetectorContainer } from "./detectorStyles";
import { Camera } from "@mediapipe/camera_utils";
import VideoContainer from "./VideoContainer";
import ButtonContainer from "./ButtonContainer";
import { Typography } from "@mui/material";
function Detector() {
const [loadingStates, setLoadingStates] = useState(0);
const [holisticModel, setHolisticModel] = useState(null);
const [camera, setCamera] = useState(null);
const [isVideoOn, setIsVideoOn] = useState(0);
const videoElementRef = useRef();
const textAreaRef = useRef();
useEffect(() => {
// load our custom model and set it
const speechSynthesisUtterance = new SpeechSynthesisUtterance();
tf.loadLayersModel("jsonmodel/model.json")
.then((fetched_model) => {
setLoadingStates((prev) => prev + 1);
// initialize the holistic model
const holistic = new mpHolistic.Holistic({
locateFile: (file) => {
return (
`https://cdn.jsdelivr.net/npm/@mediapipe/holistic@` +
`${mpHolistic.VERSION}/${file}`
);
},
});
holistic.setOptions({
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.8,
});
holistic.onResults((results) =>
onResults(
results,
fetched_model,
speechSynthesisUtterance,
textAreaRef
)
);
holistic.initialize().then((res) => {
setLoadingStates((prev) => prev + 1);
setHolisticModel(holistic);
});
// -----------------------------
})
.catch((err) => {
console.log(err);
});
// ------------------------------------------
}, []);
useEffect(() => {
// Start the camera using mediapipe camera utility
if (
typeof videoElementRef.current !== "undefined" &&
videoElementRef.current !== null &&
holisticModel !== null
) {
const camera = new Camera(videoElementRef.current, {
onFrame: async () => {
await holisticModel.send({ image: videoElementRef.current });
},
width: 640,
height: 480,
});
setCamera(camera);
}
// --------------------------------------------------
}, [videoElementRef, holisticModel]);
const initCamera = () => {
// resetSentence();
camera.start().then((res) => setIsVideoOn(1));
};
const stopVideo = () => {
camera.stop().then((res) => setIsVideoOn(0));
};
const toggleVideo = () => {
setIsVideoOn(-1);
if (isVideoOn === 0) {
initCamera();
} else {
stopVideo();
}
};
if (loadingStates < 2)
return <LoadingComponent loadingStates={loadingStates} />;
else
return (
<DetectorContainer>
<VideoContainer
videoElementRef={videoElementRef}
isVideoOn={isVideoOn}
textAreaRef={textAreaRef}
/>
<div style={{ height: "max-content", margin: "20px 0" }}>
<Typography ref={textAreaRef} sx={{ fontSize: "25px" }}></Typography>
</div>
<ButtonContainer isVideoOn={isVideoOn} toggleVideo={toggleVideo} />
</DetectorContainer>
);
}
export default Detector;