-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
136 lines (123 loc) · 4.13 KB
/
app.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var videoEl;
var imageEl;
var videoCanvasEl;
var imageCanvasEl;
var run;
window.onload = function () {
videoEl = document.getElementById("video");
imageEl = document.getElementById("image");
videoCanvasEl = document.getElementById("videoCanvas");
imageCanvasEl = document.getElementById("imageCanvas");
document.getElementById("start").addEventListener("click", start);
document.getElementById("stop").addEventListener("click", stop);
document
.getElementById("addPerson")
.addEventListener("click", registerPerson);
document.getElementById("list").addEventListener("click", listFaces);
document.getElementById("delete").addEventListener("click", deleteAllFaces);
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true }).then((stream) => {
const videoTrack = stream.getVideoTracks()[0];
videoTrack.applyConstraints({ width: 640, height: 480, frameRate: 10 });
videoEl.srcObject = stream;
// videoEl.addEventListener("canplay", start);
});
}
};
async function start() {
run = true;
while (run) {
const imageData = await getImage();
const preditions = await getPredictions(imageData.blob);
imageCanvasEl.width = imageData.width;
imageCanvasEl.height = imageData.height;
const context = imageCanvasEl.getContext("2d");
await setImage(imageData);
context.drawImage(imageEl, 0, 0, imageData.width, imageData.height);
for (let p of preditions) {
context.strokeText(p.userid + " " + p.confidence, p.x_min, p.y_min - 7);
context.strokeRect(
p.x_min,
p.y_min,
p.x_max - p.x_min,
p.y_max - p.y_min
);
}
const imageWithLabels = await getBlob(imageCanvasEl).then((blob) => ({
blob,
width: imageData.width,
height: imageData.height,
}));
await setImage(imageWithLabels);
}
}
function stop() {
run = false;
}
async function registerPerson() {
const file = document.getElementById("file").files[0];
const name = document.getElementById("name").value;
var form = new FormData();
form.append("image", file);
form.append("userid", name);
const response = await fetch("v1/vision/face/register", {
method: "POST",
body: form,
});
const jsonResponse = await response.json();
console.log(response);
}
async function listFaces() {
const response = await fetch("v1/vision/face/list", { method: "POST" });
const jsonResponse = await response.json();
document.getElementById("listText").value = JSON.stringify(jsonResponse);
}
async function deleteAllFaces() {
const response = await fetch("v1/vision/face/list", { method: "POST" });
const jsonResponse = await response.json();
const faces = jsonResponse.faces;
for (let face of faces) {
var form = new FormData();
form.append("userid", face);
await fetch("v1/vision/face/delete", { method: "POST", body: form });
}
}
function getImage() {
const width = videoEl.videoWidth;
const height = videoEl.videoHeight;
videoCanvasEl.width = width;
videoCanvasEl.height = height;
var context = videoCanvasEl.getContext("2d");
context.drawImage(video, 0, 0, width, height);
return getBlob(videoCanvasEl).then((blob) => ({ blob, width, height }));
}
function getBlob(canvas) {
return new Promise((resolve, reject) =>
canvas.toBlob((blob) => resolve(blob), "image/jpeg", 0.5)
);
}
async function getPredictions(blob) {
var form = new FormData();
form.append("image", blob);
const response = await fetch("v1/vision/face/recognize", {
method: "POST",
body: form,
});
const jsonResponse = await response.json();
if (!jsonResponse.success) throw new Error(jsonResponse.error);
return jsonResponse.predictions;
}
function setImage(imageData) {
imageEl.width = imageData.width;
imageEl.height = imageData.height;
const dataUrl = window.URL.createObjectURL(imageData.blob);
return new Promise((resolve, reject) => {
const listener = () => {
resolve();
imageEl.removeEventListener("load", listener);
window.URL.revokeObjectURL(dataUrl);
};
imageEl.addEventListener("load", listener);
imageEl.src = dataUrl;
});
}