-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
83 lines (66 loc) · 2.9 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
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
const video = document.getElementById('videoInput')
Promise.all([
faceapi.nets.faceRecognitionNet.loadFromUri('/models'),
faceapi.nets.faceLandmark68Net.loadFromUri('/models'),
// faceapi.nets.ssdMobilenetv1.loadFromUri('/models') //heavier/accurate version of tiny face detector
faceapi.nets.tinyFaceDetector.loadFromUri('/models')
]).then(start)
function start() {
document.body.append('Models Loaded')
navigator.getUserMedia(
{ video:{} },
stream => video.srcObject = stream,
err => console.error(err)
)
//video.src = '../videos/speech.mp4'
console.log('video added')
recognizeFaces()
}
async function recognizeFaces() {
console.log('0')
const labeledDescriptors = await loadLabeledImages()
console.log('1')
console.log(labeledDescriptors)
console.log('2')
const faceMatcher = new faceapi.FaceMatcher(labeledDescriptors, 0.7)
console.log('3')
video.addEventListener('play', async () => {
console.log('Playing')
const canvas = faceapi.createCanvasFromMedia(video)
document.body.append(canvas)
const displaySize = { width: video.width, height: video.height }
faceapi.matchDimensions(canvas, displaySize)
console.log('4')
setInterval(async () => {
const detections = await faceapi.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks().withFaceDescriptors()
const resizedDetections = faceapi.resizeResults(detections, displaySize)
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
const results = resizedDetections.map((d) => {
return faceMatcher.findBestMatch(d.descriptor)
})
console.log(results)
results.forEach( (result, i) => {
const box = resizedDetections[i].detection.box
const drawBox = new faceapi.draw.DrawBox(box, { label: result.toString() })
drawBox.draw(canvas)
})
}, 10)
})
}
function loadLabeledImages() {
//const labels = ['Black Widow', 'Captain America', 'Hawkeye' , 'Jim Rhodes', 'Tony Stark', 'Thor', 'Captain Marvel']
const labels = ['Parshva'] // for WebCam
return Promise.all(
labels.map(async (label)=>{
const descriptions = []
for(let i=1; i<2; i++) {
const img = await faceapi.fetchImage(`../labeled_images/${label}/${i}.jpg`)
const detections = await faceapi.detectSingleFace(img,new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks().withFaceDescriptor()
console.log(label + i + JSON.stringify(detections))
descriptions.push(detections.descriptor)
}
document.body.append(label+' Faces Loaded | ')
return new faceapi.LabeledFaceDescriptors(label, descriptions)
})
)
}