-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcam_index.html
98 lines (86 loc) · 2.42 KB
/
webcam_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html>
<body>
<video id = "video" style ="display:none;"></video>
<canvas id = "canvasOutput"></canvas>
</body>
<script src = "opencv.js"></script>
<script src = "cv.js"></script>
<script>
// In this case, We set width 320, and the height will be computed based on the input stream.
let width = 600;
let height = 0;
// whether streaming video from the camera.
let streaming = false;
let video = document.getElementById("video");
let stream = null;
let vc = null;
function startCamera() {
if (streaming) return;
navigator.mediaDevices.getUserMedia({video: true, audio: false})
.then(function(s) {
stream = s;
video.srcObject = s;
video.play();
})
.catch(function(err) {
console.log("An error occured! " + err);
});
video.addEventListener("canplay", function(ev){
if (!streaming) {
height = video.videoHeight / (video.videoWidth/width);
video.setAttribute("width", width);
video.setAttribute("height", height);
streaming = true;
vc = new cv.VideoCapture(video);
}
startVideoProcessing();
}, false);
}
let lastFilter = '';
let src = null;
let dstC1 = null;
let dstC3 = null;
let dstC4 = null;
function startVideoProcessing() {
if (!streaming) { console.warn("Please startup your webcam"); return; }
stopVideoProcessing();
src = new cv.Mat(height, width, cv.CV_8UC4);
requestAnimationFrame(processVideo);
}
function processVideo() {
vc.read(src);
var mask = new cv.Mat();
hand = find_hand(src, mask);
var handMin = new cv.Point(hand[0], hand[1]);
var handMax = new cv.Point(hand[2], hand[3]);
cv.rectangle(src, handMin, handMax, new cv.Scalar(255, 0, 0), 2, cv.LINE_AA, 0);
var handCenter = bounds_center(hand);
console.log(handCenter);
//cv.imshow("canvasOutput", src);
requestAnimationFrame(processVideo);
mask.delete();
}
function stopVideoProcessing() {
if (src != null && !src.isDeleted()) src.delete();
if (dstC1 != null && !dstC1.isDeleted()) dstC1.delete();
if (dstC3 != null && !dstC3.isDeleted()) dstC3.delete();
if (dstC4 != null && !dstC4.isDeleted()) dstC4.delete();
}
function stopCamera() {
if (!streaming) return;
stopVideoProcessing();
document.getElementById("canvasOutput").getContext("2d").clearRect(0, 0, width, height);
video.pause();
video.srcObject=null;
stream.getVideoTracks()[0].stop();
streaming = false;
}
function opencvIsReady() {
console.log('OpenCV.js is ready');
initUI();
startCamera();
}
startCamera();
</script>
</html>