-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecognize.js
61 lines (58 loc) · 2.47 KB
/
recognize.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
const imageUpload = document.getElementById('imageUpload')
const addImage = document.getElementById('addImage')
Promise.all([
faceapi.nets.faceRecognitionNet.loadFromUri('/models'),
faceapi.nets.faceLandmark68Net.loadFromUri('/models'),
faceapi.nets.ssdMobilenetv1.loadFromUri('/models')
]).then(start)
async function start() {
const container = document.createElement('div')
container.style.position = 'relative'
document.body.append(container)
const labeledFaceDescriptors = await loadLabeledImages()
const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, 0.6)
let image
let canvas
document.body.append('Loaded')
imageUpload.addEventListener('change', async () => {
if (image) image.remove()
if (canvas) canvas.remove()
image = await faceapi.bufferToImage(imageUpload.files[0])
container.append(image)
canvas = faceapi.createCanvasFromMedia(image)
container.append(canvas)
const displaySize = { width: image.width, height: image.height }
faceapi.matchDimensions(canvas, displaySize)
const detections = await faceapi.detectAllFaces(image).withFaceLandmarks().withFaceDescriptors()
const resizedDetections = faceapi.resizeResults(detections, displaySize)
const results = resizedDetections.map(d => faceMatcher.findBestMatch(d.descriptor))
results.forEach((result, i) => {
const box = resizedDetections[i].detection.box
const drawBox = new faceapi.draw.DrawBox(box, { label: result.toString() })
drawBox.draw(canvas)
})
})
let saved;
addImage.addEventListener('change', async () => {
console.log('got called!');
if (saved) saved.remove();
const descriptions = [];
saved = await faceapi.bufferToImage(addImage.files[0]);
const detections = await faceapi.detectSingleFace(saved).withFaceLandmarks().withFaceDescriptor()
descriptions.push(detections.descriptor);
return new faceapi.LabeledFaceDescriptors("Mucyo Miller", descriptions);
// console.log('descriptions', detections.descriptor);
})
}
function loadLabeledImages() {
const labels = ['Mucyo Miller'];
return Promise.all(
labels.map(async label => {
const descriptions = []
const img = await faceapi.fetchImage('https://avatars0.githubusercontent.com/u/11447549?s=460&v=4');
const detections = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor();
descriptions.push(detections.descriptor)
return new faceapi.LabeledFaceDescriptors(label, descriptions)
})
);
}