-
Notifications
You must be signed in to change notification settings - Fork 792
/
webcam_demo.html
72 lines (64 loc) · 2.84 KB
/
webcam_demo.html
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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function video() {
let video = document.getElementById("live");
let canvas = document.getElementById("canvas");
let canvas2 = document.getElementById("canvas2");
let ctx = canvas.getContext('2d');
let ctx2 = canvas2.getContext('2d');
let apiKey = document.getElementById("apiKey").value;
navigator.mediaDevices.getUserMedia({
video: {width: 640, height: 480}
}).then(function (stream) {
video.srcObject = stream;
document.addEventListener("next_frame", draw);
const evt = new Event("next_frame", {"bubbles": true, "cancelable": false});
document.dispatchEvent(evt);
});
function draw() {
ctx.drawImage(video, 0, 0, 640, 480);
canvas.toBlob(function (blob) {
blob.name = "blob.jpeg"
let fd = new FormData();
fd.append('file', blob, "blob.jpeg");
fetch('http://localhost:8000/api/v1/recognition/recognize',
{
method: "POST",
headers: {
"x-api-key": apiKey
},
body: fd
}
).then(r => r.json()).then(
function (data) {
const evt = new Event("next_frame", {"bubbles": true, "cancelable": false});
document.dispatchEvent(evt);
ctx2.clearRect(0, 0, 640, 480);
ctx2.drawImage(video, 0, 0, 640, 480);
if (!data.result) {
return;
}
let box = data.result[0].box;
let name = data.result[0].subjects[0].subject;
ctx2.lineWidth = 3;
ctx2.strokeStyle = 'green';
ctx2.strokeRect(box.x_min, box.y_min, box.x_max - box.x_min, box.y_max - box.y_min);
ctx2.font = '24px serif';
ctx2.strokeText(name, box.x_min, box.y_min - 20);
});
}, 'image/jpeg', 0.95);
}
}
</script>
<title>test</title>
</head>
<body>
<label for="apiKey">API key:</label><input id="apiKey" />
<button onclick="video()">video</button>
<video id="live" width="640" height="480" autoplay style="display:none;"></video>
<canvas width="640" id="canvas" height="480" style="display:none;"></canvas>
<canvas width="640" id="canvas2" height="480"></canvas>
</body>
</html>