This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.html
273 lines (247 loc) · 8.1 KB
/
controller.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!DOCTYPE html>
<html lang="en-GB" dir="ltr">
<head>
<meta charset="UTF-8">
<title>Controller</title>
<link rel="stylesheet" type="text/css" href="/css/controller.css">
<script type="text/javascript" src="/transforms/transforms.js"></script>
<script type="text/javascript" src="/utils/webgl-utils.js"></script>
<script type="text/javascript" src="/js/detection.js"></script>
<script type="text/javascript">
"use strict";
// HTMLVideoElement
let video;
// is detection currently being performed? - if animation is running
// also, it is used to make sure that animation is running only once
let isDetecting = false;
const TEST = false;
window.onload = function() {
let initResult = Detection.init();
if (!initResult) {
alert("Your browser does not support WebGL.");
document.querySelector(".loading").innerHTML = "Your browser does not support WebGL or some of its extensions.";
return;
}
// set video listener, important for starting animation
video = document.querySelector("video");
video.addEventListener("canplaythrough", setupAfterVideoStreamIsReady, false);
// set change and click listeners
document.querySelector("label#position input").addEventListener("change", doPosition, false);
document.querySelector("label#reset").addEventListener("click", restart, false);
// window size listener for detecting layout changes
// checks if either height or width is bigger and then changes the layout appropriately
window.addEventListener("resize", windowSizeChanged, false);
windowSizeChanged();
Utils.getCamera(video, 1280, 720, "environment", document.querySelector(".loading"));
};
/**
* Main animation function that triggers detection in the image from camera
*/
function animate() {
Detection.updateTexture(video);
Detection.repaint(isDetecting);
window.requestAnimationFrame(animate);
}
function test(start, end) {
let loaded = 0;
const data = [];
Utils.getDataFromFile("/python/test.txt", (text) => {
const testData = text.split("\r\n")
.map(t => t.split(","))
.filter(t => t[0] !== "")
.map(t => [t[0], t[1] * 1, t[2] * 1, t[3] * 1, t[4] / 100, t[5] / 100])
.sort((a, b) => a[0] <= b[0] ? -1 : 1);
for (let i = start; i <= end; i++) {
const name = String(i).padStart(3, "0");
const img = new Image();
img.onload = () => {
const color = testData[i];
Detection.setExternalColor(color[3]);
Detection.setupAfterVideoStreamIsReady(img.width, img.height);
Detection.updateTexture(img);
Detection.repaint(true);
const readData = Detection.getReadBuffer2();
if (readData[0] === 0 && readData[4] === 0) {
data.push(name + ".jpg,-1,-1");
} else {
data.push(name + ".jpg," + readData[0] + "," + (img.height - readData[4]));
}
loaded++;
};
img.src = "/python/" + name + ".jpg";
}
});
const total = end + 1;
const interval = function() {
if (loaded < total) {
setTimeout(interval, 50);
} else {
console.log(data.sort().reduce((a, b) => a + "\n" + b))
}
};
setTimeout(interval, 1);
}
function test2(start, end) {
// Utils.getDataFromFile("/python/test-hue2.txt", (text) => {
// const testData = text.split("\r\n")
// .map(t => t.split(" "))
// .map(t => [t[0], t[1] * 1, t[2] * 1]);
//
// for (let i = 467; i < 475; i++) {
// const img = new Image();
// img.onload = () => {
// testAllHues(testData, i)
// };
// img.src = "\\python\\draw3-test\\" + testData[i][0];
// // break;
// }
// });
// return;
if (start === undefined) start = 0;
let loaded = 0;
const results = [];
Utils.getDataFromFile("/python/hue3.txt", (text) => {
const testData = text.split("\r\n")
.map(t => t.split(" "))
.map(t => [t[0], t[1] * 1]);
if (end === undefined) end = testData.length;
console.log(testData);
const interval = function() {
if (loaded < end) {
setTimeout(interval, 50);
} else {
console.log(results.sort().reduce((a, b) => a + "\n" + b))
}
};
setTimeout(interval, 1);
for (let i = start; i < end; i++) {
const name = testData[i][0]
const img = new Image();
img.onload = () => {
const color = testData[i][1];
Detection.setExternalColor(color);
Detection.setupAfterVideoStreamIsReady(img.width, img.height);
Detection.updateTexture(img);
Detection.repaint(true);
const readData = Detection.getReadBuffer2();
if (readData[0] === 0 && readData[4] === 0) {
results.push(name + ",-1,-1");
} else {
const xCoord = readData[0];
const yCoord = img.height - readData[4];
const pixelsCount = readData[2]
const a = Math.sqrt(pixelsCount) * 2.1;
const aHalf = a / 2.0;
const x1 = xCoord - aHalf;
const x2 = xCoord + aHalf;
const y1 = yCoord - aHalf;
const y2 = yCoord + aHalf;
results.push(name + "," + xCoord + "," + yCoord + "," + pixelsCount + "," + x1 + "," + y1 + "," + x2 + "," + y2);
}
loaded++;
};
img.src = name;
}
});
}
function testAllHues(testData, i) {
const gtX = testData[i][1];
const gtY = testData[i][2];
let min = 99999;
let hueForMin = 0;
for (let j = 0; j < 360; j++) {
Detection.setExternalColor(j);
Detection.setupAfterVideoStreamIsReady(img.width, img.height);
Detection.updateTexture(img);
Detection.repaint(true);
const readData = Detection.getReadBuffer2();
console.log(readData);
if (readData[0] === 0 && readData[4] === 0) {
// results.push(name + ",-1,-1");
// console.log(j, "-1")
} else {
// results.push(name + "," + readData[0] + "," + (img.height - readData[4]));
const x2 = readData[0];
const y2 = (img.height - readData[4]);
const distance = Math.sqrt(Math.pow(x2 - gtX, 2) + Math.pow(y2 - gtY, 2));
console.log(j, distance);
if (min > distance) {
min = distance;
hueForMin = j;
}
}
}
console.log("Minimum", testData[i][0], hueForMin, min);
}
/**
* Event handler when camera is ready to give pictures
*/
function setupAfterVideoStreamIsReady() {
Detection.setupAfterVideoStreamIsReady(video.videoWidth, video.videoHeight);
// hide loading message
document.querySelector(".loading-container").classList.add("hidden");
document.querySelector(".main").classList.remove("hidden");
if (!TEST) {
window.requestAnimationFrame(animate);
} else {
test2(0, 139);
}
}
/**
* Event handler when position button is clicked
*/
function doPosition() {
if (document.querySelector("label#position input").checked) {
document.querySelector("label#position").classList.add("active");
isDetecting = true;
} else {
document.querySelector("label#position").classList.remove("active");
isDetecting = false;
}
}
function restart() {
Detection.restart();
document.querySelector("label#reset").classList.add("hidden");
document.querySelector("#color").style.backgroundColor = "white";
document.querySelector("#color_text").textContent = "";
}
/**
* Resize event handler
* Changes GUI so that buttons are either right or bottom
*/
function windowSizeChanged() {
if (window.innerHeight < window.innerWidth) {
// window is wider than higher
document.querySelector(".main").classList.remove("bottom");
document.querySelector(".main").classList.add("right");
} else {
// window is higher than wider
document.querySelector(".main").classList.remove("right");
document.querySelector(".main").classList.add("bottom");
}
}
</script>
</head>
<body>
<div class="loading-container">
<div class="loading">
Loading, please wait...<br>
Access to your camera is required for this application to work.<br>
You might be asked for giving a permission to access it.
</div>
</div>
<div class="main hidden">
<div class="center">
<canvas></canvas>
<video src="" autoplay muted></video>
</div>
<div class="controls">
Place the marker in the box in the middle of the video before starting detection. After starting, wait until the box disappears.
<label id="position"><input type="checkbox"> Detect</label><br>
<div id="color"></div>
<div id="color_text"></div>
<label id="reset" class="hidden">Reset</label><br>
</div>
</div>
</body>
</html>