-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.html
283 lines (269 loc) · 8.23 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
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
274
275
276
277
278
279
280
281
282
283
<!DOCTYPE html>
<html>
<head>
<title>Video</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body
{
font-family: arial;
}
#color
{
position: fixed;
font-size: 45px;
bottom: 25px;
left: 0;
background-color: black;
color: white;
padding: 25px;
width: 300px;
}
canvas
{
display: none;
}
#video
{
width: 100%;
height: 500px;
}
#file
{
display: none;
}
.but {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 25px;
margin: 4px 2px;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
cursor: pointer;
}
.button1 {
background-color: white;
color: black;
border: 2px solid #4CAF50;
}
.button1:hover {
background-color: #4CAF50;
color: white;
}
.button2 {
background-color: white;
color: black;
border: 2px solid #008CBA;
}
.button2:hover {
background-color: #008CBA;
color: white;
}
.button3 {
background-color: white;
color: black;
border: 2px solid #f44336;
}
.button3:hover {
background-color: #f44336;
color: white;
}
.button4 {
background-color: white;
color: black;
border: 2px solid #e7e7e7;
}
.button4:hover {background-color: #e7e7e7;}
.button5 {
background-color: white;
color: black;
border: 2px solid #555555;
}
.button5:hover {
background-color: #555555;
color: white;
}
input
{
padding: 25px;
font-size: 25px;
}
#left
{
display: inline-block;
width: 50%;
}
#right
{
float: right;
width: 50%;
display: inline-block;
}
#disp
{
font-weight: 600;
font-size: 30px;
}
</style>
</head>
<body>
<div id="left">
<video id="video" width="640" height="480" autoplay></video>
<canvas id="canvas" width="640" height="480"></canvas>
<label id="color"></label>
</div>
<div id="right">
<h1>Training Area</h1>
<input type="text" id="nam" placeholder="Name of the Colour" autocomplete="off">
<button id="as" class="but button2" onclick="document.getElementById('file').click()" >Add Samples</button>
<input type="file" name="images[]" accept="image/*" multiple id="file">
<button class="but button1" id="snap" >Snap</button>
<hr>
<h2>Trained Colours</h2>
<div id="disp"></div>
</div>
<script>
// Grab elements, create settings, etc.
var video = document.getElementById('video');
// Get access to the camera!
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
// Not adding `{ audio: true }` since we only want video now
navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
//video.src = window.URL.createObjectURL(stream);
video.src = video.srcObject = stream;
video.play();
});
}
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var video = document.getElementById('video');
// Trigger photo take
document.getElementById("snap").addEventListener("click", function() {
context.drawImage(video, 0, 0, 640, 480);
var tn = nam.value;
var temp = getMedian(video);
if(!nodes[tn])
nodes[tn] = [];
nodes[tn].push(temp);
setTimeout(function(){
displayNodes();
}, 1000);
});
setInterval(function(){
context.drawImage(video, 0, 0, 640, 480);
var data = getColor(getMedian(video));
try
{
document.getElementById('color').innerHTML = Object.keys(data).reduce((a, b) => data[a] > data[b] ? a : b) + " " + data[Object.keys(data).reduce((a, b) => data[a] > data[b] ? a : b)].toFixed(2) + "%";
}
catch(err)
{
document.getElementById('color').innerHTML = '????';
}
}, 1000);
</script>
<script>
var nam = document.getElementById('nam');
var nodes = {};
document.getElementById('file').onchange = function(event)
{
var reader = [];
var file = event.target.files;
for(var i = 0; i<event.target.files.length; i++)
{
var tn = nam.value;
reader.push(new FileReader());
reader[i].onload = function()
{
var temp_img = document.createElement('img');
temp_img.src = this.result;
temp_img.onload = function()
{
var temp = getMedian(temp_img);
if(!nodes[tn])
nodes[tn] = [];
nodes[tn].push(temp);
};
};
reader[i].readAsDataURL(file[i]);
}
nam.value = '';
setTimeout(function(){
displayNodes();
}, 1000);
};
function getMedian(img)
{
var dict = {};
var c = document.createElement("Canvas");
var ctx = c.getContext("2d");
ctx.drawImage(img, 0, 0);
var imgData = ctx.getImageData(0, 0, c.width, c.height);
dict['red'] = 0, dict['green'] = 0, dict['blue'] = 0;
var alpha = 0;
for (var i = 0; i < imgData.data.length; i += 4) {
dict['red'] += imgData.data[i];
dict['green'] += imgData.data[i + 1];
dict['blue'] += imgData.data[i + 2];
alpha += imgData.data[i + 3];
}
dict['red'] /= (imgData.data.length/4);
dict['green'] /= (imgData.data.length/4);
dict['blue'] /= (imgData.data.length/4);
return dict;
}
function displayNodes()
{
document.getElementById('disp').innerHTML = '';
for(var key in nodes)
{
var p = document.createElement('p');
p.innerHTML = key;
p.style.color = 'rgb('+nodes[key][0].red+', '+nodes[key][0].green+', '+nodes[key][0].blue+')';
document.getElementById('disp').appendChild(p);
/*for(var i in nodes[key])
{
var p = document.createElement('p');
p.innerHTML = nodes[key][i].red + " " + nodes[key][i].green + " " + nodes[key][i].blue;
document.getElementById('disp').appendChild(p);
}*/
}
}
function getColor(key_node)
{
var color = {};
for(var key in nodes)
{
color[key] = getProbability(key_node, nodes[key]);
}
return color;
}
const low_limit = 0;
const high_limit = 255;
function getProbability(key_node, node)
{
var red = 0, green = 0, blue = 0;
for(var i in node)
{
red += node[i].red;
green += node[i].green;
blue += node[i].blue;
}
red /= node.length;
green /= node.length;
blue /= node.length;
var key_red = Math.abs(red - key_node.red);
var key_green = Math.abs(green - key_node.green);
var key_blue = Math.abs(blue - key_node.blue);
var limit = (key_red + key_green + key_blue) / 3;
return (Math.abs(high_limit - limit) / (high_limit - low_limit)) * 100;
}
</script>
</body>
</html>