forked from dkurt/cvpr2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
74 lines (63 loc) · 2.39 KB
/
index.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
73
74
<!DOCTYPE html>
<html>
<head>
<script src="https://docs.opencv.org/master/opencv.js" type="text/javascript"></script>
<script src="https://docs.opencv.org/master/utils.js" type="text/javascript"></script>
<script type='text/javascript'>
cv['onRuntimeInitialized']=()=>{
// Create a camera object.
var output = document.getElementById('output');
var camera = document.createElement("video");
camera.setAttribute("width", 640);
camera.setAttribute("height", 480);
// Get a permission from user to use a camera.
navigator.mediaDevices.getUserMedia({video: true, audio: false})
.then(function(stream) {
camera.srcObject = stream;
camera.onloadedmetadata = function(e) {
camera.play();
};
});
// Open camera stream and start a capturing loop.
var cap = new cv.VideoCapture(camera);
var frame = new cv.Mat(camera.height, camera.width, cv.CV_8UC4);
var frameBGR = new cv.Mat(camera.height, camera.width, cv.CV_8UC3);
var net;
function captureFrame() {
var begin = Date.now();
cap.read(frame); // Read a frame from camera
cv.cvtColor(frame, frameBGR, cv.COLOR_RGBA2BGR);
var blob = cv.blobFromImage(frameBGR, 1, {width: 192, height: 144}, [104, 117, 123, 0]);
net.setInput(blob);
var out = net.forward();
for (var i = 0, n = out.data32F.length; i < n; i += 7) {
var confidence = out.data32F[i + 2];
if (confidence < 0.5)
continue;
var left = out.data32F[i + 3] * frame.cols;
var top = out.data32F[i + 4] * frame.rows;
var right = out.data32F[i + 5] * frame.cols;
var bottom = out.data32F[i + 6] * frame.rows;
cv.rectangle(frame, {x: left, y: top}, {x: right, y: bottom}, [0, 255, 0, 255]);
}
blob.delete();
out.delete();
// Visualize frame
cv.imshow(output, frame);
setTimeout(captureFrame, 0);
};
// Download models
var utils = new Utils('');
utils.createFileFromUrl("opencv_face_detector.prototxt", "opencv_face_detector.prototxt", () => {
utils.createFileFromUrl("opencv_face_detector.caffemodel", "opencv_face_detector.caffemodel", () => {
net = cv.readNet("opencv_face_detector.prototxt", "opencv_face_detector.caffemodel");
captureFrame();
});
});
};
</script>
</head>
<body>
<canvas id="output" width=640 height=480 style="max-width: 100%"></canvas>
</body>
</html>