-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
614 lines (536 loc) · 19.4 KB
/
index.js
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/////////////// labels and languages ////////////////////
var classLabel = document.getElementById("classLabel")
var intervalLabel = document.getElementById("intervalLabel");
var instanceLabel = document.getElementById("instanceLabel");
var brightLabel = document.getElementById("brightLabel");
var contrastLabel = document.getElementById("contrastLabel");
var saturationLabel = document.getElementById("saturationLabel");
var whiteBallLabel = document.getElementById("whiteballLabel");
var awbLabel = document.getElementById("awbLabel");
var aeLabel = document.getElementById("aeLabel");
var exposureLabel = document.getElementById("exposureLabel");
var aec2Label = document.getElementById("aec2Label");
var gainLabel = document.getElementById("gainLabel");
var agcLabel = document.getElementById("agcLabel");
var gainCelingLabel = document.getElementById("gainCelingLabel");
var rawGmaLabel = document.getElementById("rawGmaLabel");
var effectLabel = document.getElementById("effectLabel");
var modeLabel = document.getElementById("modeLabel");
var vflipLabel = document.getElementById("vflipLabel");
var hmirrorLabel = document.getElementById("hmirrorLabel");
var englishSelect = document.getElementById("englishSelect");
var thaiSelect = document.getElementById("thaiSelect");
const en =
{
"class": "Class",
"interval": "Interval",
"instances": "Instances",
"brightness": "Brightness",
"contrast": "Contrast",
"saturation": "Saturation",
"whiteBall": "White Ball",
"awb": "Auto Whiteball",
"ae_level": "Auto Exposure",
"aec2": "Auto Exposure Control",
"agc": "Auto Gain Control",
"gainCeling": "Gain Ceiling",
"rawGma": "Raw GMA",
"effects": "Effect",
"mode": "Mode",
"vflip": "Rotate Image",
"hmirror": "Mirror Image"
}
const th =
{
"class": "หมวด",
"interval": "เวลาระหว่างการถ่ายรูป",
"instances": "จำนวนรูป",
"brightness": "ความสว่าง",
"contrast": "ความต่างระดับสี",
"saturation": "ความอิ่มสี",
"whiteBall": "สมดุลแสงสีขาว",
"awb": "สมดุลแสงสีขาวแบบออโต้",
"ae_level": "การเปิดรับแสงแบบออโต้",
"aec2": "การคุมแสงแบบออโต้",
"agc": "ความไวต่อแสงแบบออโต้",
"gainCeling": "ระดับค่าของความไวต่อแสง",
"rawGma": "ความสว่างกล้องแบบออโต้",
"effects": "เอฟเฟคกล้อง",
"mode": "โหมดกล้อง",
"vflip": "หมุนรูป",
"hmirror": "พลิกรูป",
}
function changeLanguage(lang) {
classLabel.innerHTML = lang.class;
intervalLabel.innerHTML = lang.interval;
instanceLabel.innerHTML = lang.instances;
brightLabel.innerHTML = lang.brightness;
contrastLabel.innerHTML = lang.contrast;
saturationLabel.innerHTML = lang.saturation;
whiteBallLabel.innerHTML = lang.whiteBall;
awbLabel.innerHTML = lang.awb;
aeLabel.innerHTML = lang.ae_level;
aec2Label.innerHTML = lang.aec2;
agcLabel.innerHTML = lang.agc;
gainCelingLabel.innerHTML = lang.gainCeling;
rawGmaLabel.innerHTML = lang.rawGma;
effectLabel.innerHTML = lang.effects;
modeLabel.innerHTML = lang.mode;
vflipLabel.innerHTML = lang.vflip;
hmirrorLabel.innerHTML = lang.hmirror;
}
englishSelect.addEventListener("click", function () {
thaiSelect.style.color = "bisque";
englishSelect.style.color = "blue";
changeLanguage(en);
localStorage.setItem("lang", "en");
})
thaiSelect.addEventListener("click", function () {
thaiSelect.style.color = "blue";
englishSelect.style.color = "bisque";
changeLanguage(th);
localStorage.setItem("lang", "th");
})
function getLanguage() {
var getLang = localStorage.getItem("lang");
if (getLang == "en") {
englishSelect.click();
}
else if (getLang = "th") {
thaiSelect.click();
}
else {
englishSelect.click();
}
}
//////////////////////////////////////////
/////////////// console buttons ///////////////////
var classLabel = document.getElementById("class");
var captureButton = document.getElementById("capture");
var clearButton = document.getElementById("clear");
var stopButton = document.getElementById("stop");
var downloadButton = document.getElementById("download");
var picture = document.getElementById("stream");
var loadingIcon = document.getElementById("loading");
var setting = document.getElementById('setting');
var settings_contents = document.getElementById("setting-contents");
var arrow = document.getElementById("arrow");
/////////////////////////////
var width = 0;
var height = 0;
var state = 0;
var settingHide = true;
var galleryDict = new Map();
let index = 0;
async function getStreamAddress() {
let previousStreamRequest = localStorage.getItem('streamPath');
let streamState = localStorage.getItem('streamState');
if (streamState !== '1') {
var inputAddress = prompt('ip address from arduino');
var convertedHttpStream = "http://" + inputAddress + "/stream";
var convertedHttpSetting = "http://" + inputAddress + "/setting?";
localStorage.setItem('streamPath', convertedHttpStream);
localStorage.setItem('settingPath', convertedHttpSetting);
return convertedHttpStream;
}
console.log(previousStreamRequest);
return previousStreamRequest;
}
async function checkStream(url) {
showLoadingIcon();
picture.setAttribute("src", url.toString());
return new Promise((resolve) => {
picture.onload = () => {
width = picture.clientWidth;
height = picture.clientHeight;
console.log(width + " X " + height);
localStorage.setItem('streamState', '1');
resolve(true);
};
picture.onerror = () => {
alert("check if the ip address is valid or if CORS is enabled")
localStorage.setItem('streamState', '0');
resolve(false);
}
}
);
}
async function fetchStream() {
for (let i = 0; i < 5; i++) {
const inputAddress = await getStreamAddress();
await checkStream(inputAddress).then(console.log);
await new Promise(resolve => setTimeout(resolve, 200));
let streamState = localStorage.getItem('streamState');
if (streamState == '1') {
removeLoadingIcon();
break;
}
}
}
async function stopStream() {
picture.removeAttribute("src");
}
var inputInterval = document.getElementById('interval');
inputInterval.value = 0.35;
var inputInstance = document.getElementById('instance');
inputInstance.value = 30;
////////// camera settings /////////////
var resolution = document.getElementById("resolution");
resolution.addEventListener("change", changeResolution);
var brightness = document.getElementById("brightness");
brightness.addEventListener("change", changeBrightness);
var contrast = document.getElementById("contrast");
contrast.addEventListener("change", changeContrast);
var saturation = document.getElementById("saturation");
saturation.addEventListener("change", changeSaturation);
var whiteball = document.getElementById("whiteball");
whiteball.addEventListener("change", changeWhiteball);
var awb = document.getElementById("awb");
awb.addEventListener("change", changeAwb);
var ae = document.getElementById("ae");
ae.addEventListener("change", changeAe);
var aec2 = document.getElementById("aec2");
aec2.addEventListener("change", changeAec2);
aec2.value = 1;
var agc = document.getElementById("agc");
agc.addEventListener("change", changeAgc);
var gainCeling = document.getElementById("gainCeling");
gainCeling.addEventListener("change", changeGainCeiling);
var rawGma = document.getElementById("rawGma");
rawGma.addEventListener("change", changeRawGma);
rawGma.value = 1;
var effect = document.getElementById("effect");
effect.addEventListener("change", changeEffect);
var mode = document.getElementById("mode");
mode.addEventListener("change", changeMode);
var vflip = document.getElementById("vflip");
vflip.addEventListener("change", changeVflip);
var hmirror = document.getElementById("hmirror");
hmirror.addEventListener("change", changeHmirror);
async function changeBrightness() {
var selectedBrightness = brightness.options[brightness.selectedIndex].value;
var params = "bright=" + selectedBrightness
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeContrast() {
var selectedContrast = contrast.options[contrast.selectedIndex].value;
var params = "contrast=" + selectedContrast;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeSaturation() {
var selectedSaturation = saturation.options[saturation.selectedIndex].value;
var params = "saturation=" + selectedSaturation;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeWhiteball(){
var selectedWhiteball = whiteball.options[whiteball.selectedIndex].value;
var params = "whiteball=" + selectedWhiteball;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeAwb(){
var selectedAwb = awb.options[awb.selectedIndex].value;
var params = "awb=" + selectedAwb;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeAe() {
var selectedAe = ae.options[ae.selectedIndex].value;
var params = "ae=" + selectedAe;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeAec2(){
var selectedAec2 = aec2.options[aec2.selectedIndex].value;
var params = "aec2=" + selectedAec2;
await changeSettingApi(params);
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeAgc(){
var selectedAgc = agc.options[agc.selectedIndex].value;
var params = "agc=" + selectedAgc;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeGainCeiling(){
var selectedGainCeiling = gainCeling.options[gainCeling.selectedIndex].value;
var params = "gainCeiling=" + selectedGainCeiling;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeRawGma(){
var selectedRawGma = rawGma.options[rawGma.selectedIndex].value;
var params = "rawGma=" + selectedRawGma;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeEffect() {
var selectedEffect = effect.options[effect.selectedIndex].value;
var params = "effect=" + selectedEffect;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeMode() {
var selectedMode = mode.options[mode.selectedIndex].value;
var params = "mode=" + selectedMode;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeVflip() {
var selectedVflip = vflip.options[vflip.selectedIndex].value;
var params = "vflip=" + selectedVflip;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeHmirror() {
var selectedHmirror = hmirror.options[hmirror.selectedIndex].value;
var params = "hmirror=" + selectedHmirror;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeResolution() {
var selectedRes = resolution.options[resolution.selectedIndex].value;
var params = "resolution=" + selectedRes;
const jsonCameraStatus = await changeSettingApi(params);
await updateSettings(jsonCameraStatus);
}
async function changeSettingApi(params) {
await stopStream();
let settingAddress = localStorage.getItem('settingPath');
const response = await fetch(settingAddress + params);
if (response.status == 200){
const data = await response.json();
console.log(data);
return data;
}
}
async function updateSettings(cameraStatus){
console.log(cameraStatus);
brightness.value = cameraStatus.brightness; //
contrast.value = cameraStatus.contrast; //
saturation.value = cameraStatus.saturation; //
awb.value = cameraStatus.awb;
ae.value = cameraStatus.ae_level;
aec2.value = cameraStatus.aec2;
agc.value = cameraStatus.agc;
gainCeling.value = cameraStatus.gainceiling;
rawGma.value = cameraStatus.raw_gma;
effect.value = cameraStatus.special_effect;
mode.value = cameraStatus.wb_mode;
vflip.value = cameraStatus.vflip;
hmirror.value = cameraStatus.hmirror;
resolution.selectedIndex = cameraStatus.framesize;
await new Promise(resolve => setTimeout(resolve, 500));
await fetchStream();
}
////////////////////////////////////////////////////////
async function getInitialSettingsAndStream() {
await fetchStream();
getLanguage();
const jsonCameraStatus = await changeSettingApi("resolution=4");
await updateSettings(jsonCameraStatus);
updateImageNo();
}
getInitialSettingsAndStream();
var galleryImg = document.getElementById("galleryImg");
galleryImg.style.visibility = "hidden";
downloadDelete = document.getElementById('download-delete')
downloadDelete.style.visibility = 'hidden';
stopButton.style.visibility = 'hidden';
async function displayCanvas(picIndex) {
const displayCanvas = document.createElement("canvas");
displayCanvas.id = "cap" + picIndex.toString();
var WidthRatio = Math.ceil((width / height) * 240);
//console.log(WidthRatio);
displayCanvas.setAttribute("height", "240px");
displayCanvas.setAttribute("width", WidthRatio + "px");
displayCanvas.style.padding = "10px"
displayCanvas.style.zIndex = "1";
const newDiv = createPictureDiv(picIndex);
const deleteButton = createDeleteButton(picIndex);
galleryImg.appendChild(newDiv);
newDiv.appendChild(displayCanvas);
newDiv.appendChild(deleteButton);
displayCanvas.getContext('2d').drawImage(picture, 0, 0, WidthRatio, 240);
galleryImg.style.visibility = "visible";
downloadDelete.style.visibility = "visible";
}
function createPictureDiv(picIndex) {
const newDiv = document.createElement("div");
newDiv.id = "imgDiv" + picIndex.toString();
newDiv.style.display = "flex";
newDiv.style.justifyContent = "center"
newDiv.style.width = "350px";
newDiv.style.height = "240px"
newDiv.style.color = "red";
return newDiv;
}
function createDeleteButton(picIndex) {
const deleteButton = document.createElement("button");
deleteButton.id = picIndex.toString();
deleteButton.style.zIndex = "2";
deleteButton.style.position = "relative";
deleteButton.style.fontSize = "17px";
deleteButton.style.fontWeight = "700";
deleteButton.style.left = "-20px";
deleteButton.style.width = "22px";
deleteButton.style.height = "22px"
deleteButton.style.border = "none";
deleteButton.style.borderRadius = "50%";
deleteButton.style.backgroundColor = "red";
deleteButton.style.color = "bisque";
deleteButton.style.cursor = "pointer";
deleteButton.innerText = "-";
deleteButton.setAttribute("onclick", "deleteButton(this.id)");
return deleteButton;
}
let testUrl = [];
async function savePicture(picIndex) {
const hiddenCanvas = document.createElement("canvas");
hiddenCanvas.id = "saved" + picIndex;
hiddenCanvas.setAttribute("width", width + "px");
hiddenCanvas.setAttribute("height", height + "px");
hiddenCanvas.getContext('2d').drawImage(picture, 0, 0, width, height);
let image_data_url = hiddenCanvas.toDataURL('image' + index - 1 + '/jpg');
galleryDict.set("saved" + picIndex, image_data_url);
await updateImageNo();
}
function getInterval() {
let interval = Math.round(inputInterval.value * 1000);
if (interval < 100) {
alert("cannot be less than 0.1!!");
inputInterval.value = 0.1;
return 100;
}
return interval;
}
function getInstance() {
if (inputInstance.value < 1) {
alert("instances cannot be less than 1!");
inputInstance.value = 1;
}
if (!Number.isInteger(inputInstance.value)) {
if (inputInstance < 1) {
inputInstance.value = Math.round(inputInstance.value + 1);
}
else {
inputInstance.value = Math.round(inputInstance.value);
}
}
return inputInstance.value;
}
function deleteButton(pictureId) {
document.getElementById("imgDiv" + pictureId).remove();
galleryDict.delete("saved" + pictureId);
updateImageNo();
if (galleryDict.size == 0) {
galleryImg.style.visibility = "hidden";
downloadDelete.style.visibility = "hidden";
}
}
async function chageOtherSettings(setting) {
if (setting !== "bright") {
brightness.value = 0;
}
if (setting !== "contrast") {
contrast.value = 0;
}
if (setting !== "saturation") {
saturation.value = 0;
}
if (setting !== "ae") {
ae.value = 0;
}
else if (setting !== "effect") {
effect.value = 0;
}
}
/////////////////////////////////////////////////
setting.addEventListener("click", function () {
//console.log(settingHide);
if (settingHide == true) {
settings_contents.style.visibility = "hidden";
arrow.src = "icons/caret-up.png";
settingHide = false;
} else if (settingHide == false) {
arrow.src = "icons/caret-down.png";
settings_contents.style.visibility = "visible";
settingHide = true;
}
})
captureButton.addEventListener("click", async function () {
captureButton.style.visibility = 'hidden';
stopButton.style.visibility = 'visible';
state = 1;
var instance = getInstance();
for (let i = 0; i < instance; i++) {
if (state === 0) {
break;
}
await displayCanvas(index);
await savePicture(index);
index++;
await new Promise(resolve => setTimeout(resolve, getInterval()));
}
captureButton.style.visibility = 'visible';
stopButton.style.visibility = 'hidden';
})
clearButton.addEventListener("click", function () {
const hiddenCan = document.getElementById("savedImg");
while (galleryImg.firstChild) {
galleryImg.removeChild(galleryImg.firstChild);
}
while (hiddenCan.firstChild) {
hiddenCan.removeChild(hiddenCan.firstChild);
}
galleryDict.clear();
updateImageNo();
galleryImg.style.visibility = "hidden";
downloadDelete.style.visibility = "hidden";
})
stopButton.addEventListener("click", async function () {
captureButton.style.visibility = 'visible';
stopButton.style.visibility = 'hidden';
state = 0;
})
downloadButton.addEventListener("click", async function () {
var downloadLink = document.createElement("a");
const zip = new JSZip();
if (galleryDict.size == 0) {
alert("No image found!!");
return;
}
var count = 0;
for (const value of galleryDict.values()) {
let data = value.substr(value.indexOf(",") + 1)
count++;
zip.file(classLabel.value + count.toString() + ".jpg", data, { base64: true })
}
const zipFile = await zip.generateAsync({ type: 'blob' });
downloadLink.download = "images_of_" + classLabel.value + '.zip';
const url = URL.createObjectURL(zipFile);
downloadLink.href = url;
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
downloadLink.click();
downloadLink.remove();
URL.revokeObjectURL(url);
})
async function updateImageNo() {
var imageNo = document.getElementById("image_no");
imageNo.value = galleryDict.size;
}
function showLoadingIcon() {
loadingIcon.src = "./loading.gif";
loadingIcon.setAttribute("width", "120");
loadingIcon.setAttribute("height", "120");
loadingIcon.style.visibility = "visible";
}
function removeLoadingIcon() {
loadingIcon.removeAttribute('src');
loadingIcon.style.visibility = 'hidden';
}