forked from WebDevSimplified/Face-Detection-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
102 lines (89 loc) · 4.04 KB
/
script.js
File metadata and controls
102 lines (89 loc) · 4.04 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
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
const video = document.getElementById("video");
// const street = document.getElementById("street");
// const speedDiv = document.getElementById("speed");
// const speedLimit = document.getElementById("speedLimit");
let facematcher;
let labeledFaceDescriptors;
// navigator.geolocation.getCurrentPosition(getSpeedlimit);
Promise.all([
faceapi.nets.ageGenderNet.loadFromUri("https://darthratz.github.io/Face-Detection-JavaScript/models"),
faceapi.nets.faceExpressionNet.loadFromUri("https://darthratz.github.io/Face-Detection-JavaScript/models"),
faceapi.nets.faceLandmark68Net.loadFromUri("https://darthratz.github.io/Face-Detection-JavaScript/models"),
faceapi.nets.faceLandmark68TinyNet.loadFromUri("https://darthratz.github.io/Face-Detection-JavaScript/models"),
faceapi.nets.faceRecognitionNet.loadFromUri("https://darthratz.github.io/Face-Detection-JavaScript/models"),
faceapi.nets.ssdMobilenetv1.loadFromUri("https://darthratz.github.io/Face-Detection-JavaScript/models"),
faceapi.nets.tinyFaceDetector.loadFromUri("https://darthratz.github.io/Face-Detection-JavaScript/models"),
]).then(startVideo);
async function startVideo() {
navigator.getUserMedia(
{ video: {} },
(stream) => (video.srcObject = stream),
(err) => console.error(err)
);
labeledFaceDescriptors = await loadLabeledImages();
faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, 0.6);
}
video.addEventListener("play", () => {
const canvas = faceapi.createCanvasFromMedia(video);
document.body.append(canvas);
const displaySize = { width: video.width, height: video.height };
faceapi.matchDimensions(canvas, displaySize);
setInterval(async () => {
const detections = await faceapi
.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions())
.withFaceLandmarks()
.withFaceDescriptors()
.withFaceExpressions()
.withAgeAndGender();
const resizedDetections = faceapi.resizeResults(detections, displaySize);
canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
faceapi.draw.drawDetections(canvas, resizedDetections);
faceapi.draw.drawFaceLandmarks(canvas, resizedDetections);
faceapi.draw.drawFaceExpressions(canvas, resizedDetections);
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)
})
navigator.geolocation.getCurrentPosition(getSpeedlimit);
}, 5000);
});
function httpGet(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false); // false for synchronous request
xmlHttp.send(null);
return xmlHttp.responseText;
}
function getSpeedlimit(position) {
const lat = position.coords.latitude;
const long = position.coords.longitude;
const speed = position.coords.speed;
const resp = httpGet(`https://atlas.microsoft.com/search/address/reverse/json?query=${lat},${long}&returnSpeedLimit=True&subscription-key=zqlUk9tcyTyRyycwex7Kqfmdm-ym0PQ_RMG2Aws3hmw`);
console.log(position);
const address = JSON.parse(resp).addresses[0].address;
street.innerText = `Street Name: ${JSON.stringify(address.streetName)}`;
speedLimit.innerText = `Speed Limit: ${JSON.stringify(address.speedLimit)}`;
if (!(speed === null || speed === undefined)) {
speedDiv.innerText = `Speed: ${speed}`;
}
}
function loadLabeledImages() {
const labels = ['Black Widow', 'Captain America', 'Captain Marvel', 'Hawkeye', 'Jim Rhodes', 'Thor', 'Tony Stark', 'Mick'];
return Promise.all(
labels.map(async (label) => {
const descriptions = [];
for (let i = 1; i <= 2; i++) {
const img = await faceapi.fetchImage(
`people/${label}/${i}.jpg`
);
const detections = await faceapi
.detectSingleFace(img)
.withFaceLandmarks()
.withFaceDescriptor();
descriptions.push(detections.descriptor);
}
return new faceapi.LabeledFaceDescriptors(label, descriptions);
})
);
}