-
Notifications
You must be signed in to change notification settings - Fork 13
/
video.html
66 lines (61 loc) · 1.71 KB
/
video.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
<!doctype html>
<html>
<head>
<title>OpenCV Video Examples - Video</title>
<link href="app.css" rel="stylesheet">
</head>
<body>
<h1>OpenCV Video</h1>
<video id="video" src="trailer.mp4" muted width="320" height="240"></video>
<canvas id="canvasOutput" width="320" height="240"></canvas>
<script async src="opencv.js" type="text/javascript" onload="onCvLoaded();"></script>
<script>
function onCvLoaded () {
console.log('cv', cv);
cv.onRuntimeInitialized = onReady;
}
const video = document.getElementById('video');
const actionBtn = document.getElementById('actionBtn');
const FPS = 30;
let stream;
let streaming = false;
function onReady () {
console.log('ready');
let src;
let dst;
let cap;
video.controls = true;
video.addEventListener('play', start);
video.addEventListener('pause', stop);
video.addEventListener('ended', stop);
function start () {
console.log('playing...');
streaming = true;
const width = video.width;
const height = video.height;
src = new cv.Mat(height, width, cv.CV_8UC4);
dst = new cv.Mat(height, width, cv.CV_8UC1);
cap = new cv.VideoCapture(video);
setTimeout(processVideo, 0);
}
function stop () {
console.log('paused or ended');
streaming = false;
}
function processVideo () {
if (!streaming) {
src.delete();
dst.delete();
return;
}
const begin = Date.now();
cap.read(src)
cv.cvtColor(src, dst, cv.COLOR_RGBA2GRAY);
cv.imshow('canvasOutput', dst);
const delay = 1000/FPS - (Date.now() - begin);
setTimeout(processVideo, delay);
}
}
</script>
</body>
</html>