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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
import React, { useState, useRef, useEffect } from 'react';
import * as faceapi from 'face-api.js';
import { useNavigate } from 'react-router-dom';
import axios from 'axios';
const Register = () => {
const navigate = useNavigate("/");
const userRef = useRef();
const passwordRef = useRef();
const emailRef = useRef();
const FullRef = useRef();
const snapshotRef = useRef(null);
const videoRef = useRef(null);
const canvasRef = useRef(null);
const [modelIsLoaded, setModelIsLoaded] = useState(false);
const [detections, setDetections] = useState([]);
const [error, setError] = useState(false);
const [snapshot, setSnapshot] = useState(null);
const [cameraActive, setCameraActive] = useState(true);
const [submitDisabled, setSubmitDisabled] = useState(true);
const [descriptionValue, setDescriptionValue] = useState(null);
const [faceDetected, setFaceDetected] = useState(false);
useEffect(() => {
const loadModels = async () => {
await faceapi.nets.tinyFaceDetector.loadFromUri('/models');
await faceapi.nets.faceLandmark68Net.loadFromUri('/models');
await faceapi.nets.faceRecognitionNet.loadFromUri('/models');
await faceapi.nets.faceExpressionNet.loadFromUri('/models');
await faceapi.nets.tinyFaceDetector.loadFromUri('/models');
setModelIsLoaded(true);
startVideo();
};
loadModels();
}, []);
const RegSubmit = async (e) => {
e.preventDefault();
console.log("hello");
try {
const res = await axios.post('http://localhost:5000/v1/users', {
username: userRef.current.value,
email: emailRef.current.value,
FullName: FullRef.current.value,
password: passwordRef.current.value,
faceDescriptor: descriptionValue
});
console.log(res.data);
setError(false);
navigate("/login");
console.log("help");
} catch (err) {
console.log(err);
setError(true);
}
};
const startVideo = () => {
navigator.mediaDevices
.getUserMedia({ video: true })
.then((stream) => {
videoRef.current.srcObject = stream;
})
.catch((err) => console.error("Error accessing webcam: ", err));
};
const stopVid = () => {
navigator.mediaDevices.getUserMedia({ video: false });
const stream = videoRef?.current?.srcObject;
if (stream) {
stream.getTracks().forEach((track) => track.stop());
videoRef.current.srcObject = null;
setCameraActive(false);
}
};
const captureSnapshot = async () => {
const canvas = snapshotRef.current;
const context = canvas.getContext('2d');
context.drawImage(videoRef.current, 0, 0, canvas.width, canvas.height);
const dataUrl = canvas.toDataURL('image/jpeg');
setSnapshot(dataUrl);
const detection = await faceapi
.detectSingleFace(canvas, new faceapi.TinyFaceDetectorOptions())
.withFaceLandmarks()
.withFaceDescriptor();
if (detection) {
const newDescriptor = detection.descriptor;
setDescriptionValue(newDescriptor);
console.log(newDescriptor);
setSubmitDisabled(false);
stopVid();
if (storedDescriptor && isMatchingFace(storedDescriptor, newDescriptor)) {
setInterval(alert("face matched"), 100);
} else {
alert("No Match Found!");
}
} else {
console.error("No face detected in snapshot");
}
};
const handleVideoPlay = async () => {
const video = videoRef.current;
const canvas = canvasRef.current;
const displaySize = { width: video.width, height: video.height };
faceapi.matchDimensions(canvas, displaySize);
setInterval(async () => {
if (!cameraActive) return;
const detections = await faceapi.detectAllFaces(
video,
new faceapi.TinyFaceDetectorOptions()
);
const resizedDetections = faceapi.resizeResults(detections, displaySize);
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
faceapi.draw.drawDetections(canvas, resizedDetections);
const detected = detections.length > 0;
if (detected && !faceDetected) {
captureSnapshot();
}
setFaceDetected(detected);
}, 100);
};
return (
<div className="flex flex-col w-full h-screen justify-center">
<div className="flex flex-col">
<form className="flex flex-col mb-2 w-full" onSubmit={RegSubmit}>
<h3 className="flex flex-col mx-auto mb-5">Registration Page</h3>
<div className="flex flex-col mb-2 w-[50%] mx-auto items-center">
<input
type="text"
placeholder="Email"
className="w-full rounded-2xl h-[50px] border-2 p-2 mb-2 border-gray-900"
required
ref={emailRef}
/>
<input
type="text"
placeholder="Username"
className="w-full rounded-2xl h-[50px] border-2 p-2 mb-2 border-gray-900"
required
ref={userRef}
/>
<input
type="text"
placeholder="Full Name"
className="w-full rounded-2xl h-[50px] border-2 p-2 mb-2 border-gray-900"
required
ref={FullRef}
/>
<input
type="password"
placeholder="Password"
className="w-full rounded-2xl h-[50px] border-2 p-2 mb-2 border-gray-900"
required
ref={passwordRef}
/>
<div>
{!modelIsLoaded && cameraActive && !descriptionValue ? (
<p>Loading</p>
) : (
<>
{!descriptionValue && (
<>
<video
ref={videoRef}
width="200"
height="160"
onPlay={handleVideoPlay}
autoPlay
muted
/>
<canvas
ref={canvasRef}
width="200"
height="160"
style={{ position: 'absolute', top: 0, left: 0 }}
/>
<p>
{faceDetected ? (
<span style={{ color: 'green' }}>Face Detected</span>
) : (
<span style={{ color: 'red' }}>No Face Detected</span>
)}
</p>
<canvas
ref={snapshotRef}
width="480"
height="360"
style={{ display: 'none' }}
/>
</>
)}
</>
)}
{snapshot && (
<div style={{ marginTop: '20px' }}>
<h4>Face Snapshot:</h4>
<img
src={snapshot}
alt="Face Snapshot"
width="200"
height="160"
/>
</div>
)}
</div>
<div className="mt-2">
<button type="button" onClick={stopVid}>
Stop Video
</button>
</div>
<button
disabled={submitDisabled}
className="mx-auto mt-4 rounded-2xl cursor-pointer text-white bg-primary w-[80%] lg:w-[50%] h-[40px] text-center items-center justify-center"
type="submit"
>
Register
</button>
</div>
<div className="flex flex-col mt-1 w-full">
<p className="flex justify-center">
Registered previously?
<a href="/login" className="text-blue-600 underline">
Login
</a>
</p>
</div>
{error && (
<p className="text-red-600 text-center mt-2">
Error while registering, try again
</p>
)}
</form>
</div>
</div>
);
};
export default Register;
|