-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface-recognition.js
More file actions
63 lines (55 loc) · 2.03 KB
/
Copy pathface-recognition.js
File metadata and controls
63 lines (55 loc) · 2.03 KB
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
class FaceRecognitionService {
constructor() {
this.modelUrl = "https://justadudewhohacks.github.io/face-api.js/models";
this.isLoaded = false;
this.loadPromise = null;
}
async ensureReady() {
if (!window.faceapi) {
throw new Error("Face recognition library is unavailable.");
}
if (this.isLoaded) {
return;
}
if (!this.loadPromise) {
this.loadPromise = Promise.all([
window.faceapi.nets.tinyFaceDetector.loadFromUri(this.modelUrl),
window.faceapi.nets.faceLandmark68Net.loadFromUri(this.modelUrl),
window.faceapi.nets.faceRecognitionNet.loadFromUri(this.modelUrl)
]).then(() => {
this.isLoaded = true;
});
}
await this.loadPromise;
}
async descriptorFromCanvas(canvas) {
await this.ensureReady();
const detection = await window.faceapi
.detectSingleFace(canvas, new window.faceapi.TinyFaceDetectorOptions({
inputSize: 416,
scoreThreshold: 0.45
}))
.withFaceLandmarks()
.withFaceDescriptor();
if (!detection?.descriptor) {
throw new Error("No clear face was detected in the captured image.");
}
return Array.from(detection.descriptor);
}
async descriptorFromImageUrl(url) {
await this.ensureReady();
const image = await window.faceapi.fetchImage(url);
const detection = await window.faceapi
.detectSingleFace(image, new window.faceapi.TinyFaceDetectorOptions({
inputSize: 416,
scoreThreshold: 0.45
}))
.withFaceLandmarks()
.withFaceDescriptor();
if (!detection?.descriptor) {
throw new Error("No clear face was detected in the selected access record.");
}
return Array.from(detection.descriptor);
}
}
window.faceRecognitionService = new FaceRecognitionService();