-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledtest.html
165 lines (157 loc) · 5.15 KB
/
ledtest.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<style>
.js-loader {
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.8);
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;
}
.js-loader div {
text-align: center;
font-size: 1.25em;
color: white;
}
#canvas {
position:fixed;
left:0;
top:0;
width:100%;
height:100%;
}
</style>
<script type="text/javascript">
const threshold = 0.75;
const TEMP_SRC = "web/led-crop.png";
let streaming = false;
let tagColor;
let video;
let srcIcon;
let cap;
let frame;
let gray;
let width;
let height;
let stream;
let template;
function hideLoading() {
document.getElementById("divmask").style.visibility="hidden";
}
function startCamera() {
if (streaming)
return;
console.log('start camera');
navigator.mediaDevices.getUserMedia({video: true, audio: false})
.then(function(s) {
stream = s;
video.srcObject = s;
video.play();
})
.catch(function(err) {
console.log("start camera error: " + err);
});
video.addEventListener("canplay", function(ev){
if (!streaming) {
tagColor = new cv.Scalar(255, 0, 0, 255);
width = window.innerWidth > video.videoWidth? video.videoWidth: window.width;
height = video.videoHeight / (video.videoWidth/width);
video.setAttribute("width", width);
video.setAttribute("height", height);
console.log('preview size: '+width+'x'+height);
streaming = true;
frame = new cv.Mat(height, width, cv.CV_8UC4);
gray = new cv.Mat();
cap = new cv.VideoCapture(video);
}
requestAnimationFrame(processVideo);
}, false);
}
function processVideo(){
if (!streaming)
return;
cap.read(frame);
cv.cvtColor(frame, gray, cv.COLOR_BGR2GRAY);
let res = new cv.Mat();
cv.matchTemplate(gray,template,res,cv.TM_CCOEFF_NORMED);
var newDst = [];
var start = 0;
var end = res.cols;
var count = 0;
for (var i = 0; i < res.rows; i++) {
newDst[i] = [];
for (var k = 0; k < res.cols; k++) {
newDst[i][k] = res.data32F[start];
if (newDst[i][k] > threshold) {
let maxPoint = {
"x": k,
"y": i
}
let point = new cv.Point(k + template.cols, i + template.rows);
cv.rectangle(frame, maxPoint, point, tagColor, 2, cv.LINE_8, 0);
console.log('matched template ('+point.x+','+point.y+')');
count++;
delete point;
}
start++;
}
start = end;
end = end + res.cols;
}
console.log('matched template: '+count);
cv.imshow('the-canvas', frame); // display the output to canvas
delete res;
requestAnimationFrame(processVideo);
}
function onTemplateLoaded(){
console.log('src-image.js is ready');
template = new cv.Mat();
let original = cv.imread(srcIcon); // load the image from <img>
cv.cvtColor(original, template, cv.COLOR_BGR2GRAY);
console.log('template size: '+template.size().width+'x'+template.size().height);
startCamera();
hideLoading();
}
function onOpenCvReady() {
cv['onRuntimeInitialized']=()=>{
console.log('opencv.js is ready');
video = document.getElementById("video");
srcIcon = document.getElementById('src-image');
//srcIcon.crossOrigin = "Anonymous";
srcIcon.onload = function(){
onTemplateLoaded();
}
srcIcon.src=TEMP_SRC;
}
}
</script>
<script type="text/javascript">
var Module = {
locateFile: function (name) {
let files = {
"opencv.wasm": 'web/opencv.wasm'
}
return files[name]
},
preRun: [() => {
//Module.FS_createPreloadedFile("/", "face.xml", "data/haarcascade_frontalface_default.xml",
// true, false);
}],
postRun: [
//run
]};
</script>
<script src="web/opencv.js" async onload="onOpenCvReady();" type="text/javascript"></script>
<body style='margin : 0px; overflow: hidden;'>
<img id="src-image" alt="" style="display:none;">
<div style="display:none;">
<video id="video" class="hidden">Your browser does not support the video tag.</video>
</div>
<div class="js-loader" id="divmask" onclick="hideLoading();">
<div>Loading, please wait...</div>
</div>
<canvas id="the-canvas"></canvas>
</body>