-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
38 lines (33 loc) · 1.02 KB
/
script.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
let input1 = document.getElementById('img1');
let croppedImage1 = document.getElementById('cropped-img1');
let image1;
// Load the model.
Promise.all([
faceapi.nets.ssdMobilenetv1.loadFromUri('/models'),
]).then(() => {
image1 = detectface(input1, croppedImage1);
}
)
// Function to detect the face.
var detectface = async function (input, croppedImage){
const output = await faceapi.detectAllFaces(input);
detections = output[0].box;
let res = extractFaceFromBox(input, detections, croppedImage);
return res;
}
// Function to draw image from the box data.
async function extractFaceFromBox(imageRef, box, croppedImage) {
const regionsToExtract = [
new faceapi.Rect(box.x, box.y, box.width, box.height)
];
let faceImages = await faceapi.extractFaces(imageRef, regionsToExtract);
if (faceImages.length === 0) {
console.log("No face found");
} else {
faceImages.forEach((cnv) => {
croppedImage.src = cnv.toDataURL();
});
console.log(croppedImage.src);
return croppedImage.src;
}
}