generated from lauirvin/reactjs-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWebcamDemo.tsx
59 lines (55 loc) · 1.62 KB
/
WebcamDemo.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
58
59
import Webcam from 'react-webcam';
import { CameraOptions, useFaceDetection } from 'react-use-face-detection';
import FaceDetection from '@mediapipe/face_detection';
import { Camera } from '@mediapipe/camera_utils';
const width = 500;
const height = 500;
const WebcamDemo = (): JSX.Element => {
const { webcamRef, boundingBox, isLoading, detected, facesDetected } = useFaceDetection({
faceDetectionOptions: {
model: 'short',
},
faceDetection: new FaceDetection.FaceDetection({
locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/face_detection/${file}`,
}),
camera: ({ mediaSrc, onFrame }: CameraOptions) =>
new Camera(mediaSrc, {
onFrame,
width,
height,
}),
});
return (
<div>
<p>{`Loading: ${isLoading}`}</p>
<p>{`Face Detected: ${detected}`}</p>
<p>{`Number of faces detected: ${facesDetected}`}</p>
<div style={{ width, height, position: 'relative' }}>
{boundingBox.map((box, index) => (
<div
key={`${index + 1}`}
style={{
border: '4px solid red',
position: 'absolute',
top: `${box.yCenter * 100}%`,
left: `${box.xCenter * 100}%`,
width: `${box.width * 100}%`,
height: `${box.height * 100}%`,
zIndex: 1,
}}
/>
))}
<Webcam
ref={webcamRef}
forceScreenshotSourceSize
style={{
height,
width,
position: 'absolute',
}}
/>
</div>
</div>
);
};
export default WebcamDemo;