-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageTest.html
executable file
·585 lines (476 loc) · 19.1 KB
/
imageTest.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
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
<!DOCTYPE html>
<html>
<head>
<script src="resources/js/canvasjs.min.js"></script>
<script src="resources/js/jquery-2.2.3.js"></script>
<meta charset='utf-8'>
<title>Resim Testi</title>
</head>
<body>
<div>
<input id="interval" type="number">ms</input>
<button id="btnStart" onclick="startTimer()">Teste başla</button>
<button id="btnStop" onclick="stopTimer(1)" disabled = true>Durdur</button>
<output id="selectedFile"></output>
</div>
<div>
<div id="alert"></div>
</div>
<div>
<audio id="warningAudio" controls hidden>
<source src="resources/sounds/warning.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
<audio id="noiseAudio" controls hidden>
<source src="resources/sounds/chirp.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
</div>
<div>
<img id="testImage" src="resources/images/ResimTesti.png" style="width:800px;height:600px"/>
</div>
<div>
<a download="imageShownTime.txt" id="downloadlink" style="display: none"><span id="download">İndir</span></a>
</div>
<div>
<span id="eda_File">EDA Dosyası :</span><input id="edaFile" type="file" name="edaFile"/>
<span id="acc_File">ACC Dosyası :</span><input id="accFile" type="file" name="accFile"/>
<span id="record_File">Kayıt Dosyası :</span><input id="recordFile" type="file" name="recordFile"/>
</div>
<div id="edaChartContainer" style="height: 300px; width: 100%;"></div>
<div id="accChartContainer" style="height: 300px; width: 100%;"></div>
<div id="adjustTimeMode" style="display: none">
<span id="adjust_Time_Mode">Zaman Ayarlama Modu :</span><input id="adjustTimeModeOn" type="radio" name="adjustTimeMode"/><span id="adjust_Time_Mode_On">Açık</span>
<input id="adjustTimeModeOff" type="radio" name="adjustTimeMode" checked/><span id="adjust_Time_Mode_Off">Kapalı</span>
</div>
<!--Perform translation -->
<script type="text/javascript" src="translate.js"></script>
<!--Contains imageSequence variable-->
<script type="text/javascript" src="imageSequence.js"></script>
<script type="text/javascript">
window.addEventListener('load', init, false);
window.addEventListener('keydown',this.keyPress,false);
var adjustmentInMs = 0;
var deltaInMs = 250;
function keyPress(e) {
var code = e.keyCode;
switch (code) {
// + key
case 187: adjustmentInMs += deltaInMs; console.log("Adjustment: " + adjustmentInMs); break;
// - key
case 189: adjustmentInMs -= deltaInMs; console.log("Adjustment: " + adjustmentInMs); break;
}
}
var imageIndex = 0;
var numberOfImages = imageSequence.length;
var imageTimer = null;
var restTimer = null;
var noiseTimer = null;
var imagesShownTimeList=[];
var edaInitTime, accInitTime;
var edaFrequency, accFrequency;
var increment, accIncrement;
var testStartDate = 0;
var interval = 1000;
var noiseInterval = 3000;
var adjustTimeModeOn = false;
var categoryShift = 0;
var image = document.getElementById("testImage");
function loadImages(fs){
var allImages = [];
for (i = 0; i < imageSequence.length; i++) {
allImages[i] = new Image();
allImages[i].src = imageSequence[i];
}
}
function initLocalStorage(){
if(typeof(Storage) !== "undefined"){
// Clear local storage
localStorage.setItem("imagesShownTime", null);
}else{
image.src = "resources/images/Sorry.jpg";
stopTimer(true);
}
}
function startRestTimer(){
var restInterval = 1; // 30sec rest
if(restTimer == null){
image.src = "resources/images/rest.jpg";
restTimer = setInterval(haveRest, restInterval);
return restTimer;
}
}
function haveRest(){
if(restTimer != null){
clearInterval(restTimer);
restTimer = null;
var warning = document.getElementById("warningAudio");
warning.play();
imageTimer = setInterval(imageChanger, interval);
noiseInterval = interval * 3;
noiseTimer = setInterval(makeNoise, noiseInterval);
imageChanger();
}
}
function makeNoise(){
if(noiseTimer != null){
var noiseAudio = document.getElementById("noiseAudio");
noiseAudio.play();
}
}
function startTimer(){
initLocalStorage();
if(imageTimer == null) {
interval = document.getElementById("interval").value;
testStartDate = parseInt(Date.now());
document.getElementById("btnStart").disabled = true;
document.getElementById("btnStop").disabled = false;
document.getElementById('downloadlink').style.display="none";
imagesShownTimeList=[];
return startRestTimer();
}
}
var textFile = null,
makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
// returns a URL you can use as a href
return textFile;
};
function stopTimer(isStoppedManually){
if(imageTimer != null || restTimer != null) {
document.getElementById("btnStart").disabled = false;
document.getElementById("btnStop").disabled = true;
if(isStoppedManually) {
image.src = "resources/images/ResimTesti.png";
initLocalStorage();
}else{
// Update local storage
var strImagesShownTime = JSON.stringify(imagesShownTimeList);
localStorage.setItem("imagesShownTime", strImagesShownTime);
var parsableList = '{"times":' + strImagesShownTime + '}';
//console.log(parsableList);
imagesShownTimeList = JSON.parse(parsableList);
var link = document.getElementById('downloadlink');
link.download = testStartDate + "_" + interval + ".txt";
var age = localStorage.getItem("userAge");
var gender = localStorage.getItem("userGender");
var fileContent = age + '\n' + gender + '\n' + JSON.stringify(strImagesShownTime);
link.href = makeTextFile(fileContent);
link.style.display = 'block';
}
clearInterval(restTimer);
restTimer = null;
clearInterval(imageTimer);
imageTimer = null;
imageIndex = 0;
clearInterval(noiseTimer);
noiseTimer = null;
}
}
function imageChanger() {
if(imageIndex >= numberOfImages){
stopTimer(false);
image.src = "resources/images/TestFinished.png" ;
return;
}
image.src = imageSequence[imageIndex];
imagesShownTimeList.push({imageFile:image.src.substr(image.src.indexOf("resource")), date:Date.now()});
imageIndex++;
}
function init() {
initLocalStorage();
checkForFileApiSupport();
loadImages();
document.getElementById("interval").value = interval;
document.getElementById('edaFile').addEventListener('change', handleEdaFileSelection, false);
document.getElementById('accFile').addEventListener('change', handleAccFileSelection, false);
document.getElementById('recordFile').addEventListener('change', handleRecordFileSelection, false);
}
function checkForFileApiSupport() {
if (window.File && window.FileReader && window.FileList && window.Blob) {
// All the File APIs are supported.
}
else {
document.getElementById('alert').innerHTML = "The File APIs are not fully supported in this browser.";
}
}
function handleRecordFileSelection(evt){
var files = evt.target.files;
var output = [];
for (var i = 0, f; f = files[i]; i++) {
output.push('<li><strong>', f.name, '</strong> (', f.type || 'n/a', ') - ',
f.size, ' bytes, last modified: ',
f.lastModifiedDate, '</li>');
var r = new FileReader();
r.onload = (function(f) {
return function(e) {
var contents = e.target.result;
var splitted = contents.split('\n');
userAge = parseInt(splitted[0]);
userGender = splitted[1];
console.log("Age: " + userAge);
console.log("Gender: " + userGender);
var imagesShownTimeInfo = splitted[2];
// Remove quotes
imagesShownTimeInfo = imagesShownTimeInfo.slice(1);
imagesShownTimeInfo = imagesShownTimeInfo.slice(0, imagesShownTimeInfo.length-1);
// Remove backslashes
console.log("Before Replace :\n" + imagesShownTimeInfo);
imagesShownTimeInfo = imagesShownTimeInfo.split('\\').join('');
console.log("After Replace :\n" + imagesShownTimeInfo);
var parsableList = '{"times": ' + imagesShownTimeInfo + ' }';
console.log(parsableList);
imagesShownTimeList = JSON.parse(parsableList);
console.log(imagesShownTimeList);
//console.log("First Answered: " + imagesShownTimeList.times[0].date);
};
})(f);
r.readAsText(f);
}
document.getElementById('selectedFile').innerHTML = '<ul>' + output.join('') + '</ul>';
}
function handleEdaFileSelection(evt) {
var files = evt.target.files;
var output = [];
for (var i = 0, f; f = files[i]; i++) {
output.push('<li><strong>', f.name, '</strong> (', f.type || 'n/a', ') - ',
f.size, ' bytes, last modified: ',
f.lastModifiedDate, '</li>');
var r = new FileReader();
r.onload = (function(f) {
return function(e) {
var contents = e.target.result;
var splitted = contents.split(/\r\n|\n/);
edaInitTime = parseFloat(splitted[0]);
edaFrequency = parseFloat(splitted[1]);
drawEdaGraphics(splitted, edaInitTime, edaFrequency, "edaChartContainer");
};
})(f);
r.readAsText(f);
}
document.getElementById('selectedFile').innerHTML = '<ul>' + output.join('') + '</ul>';
}
function handleAccFileSelection(evt) {
var files = evt.target.files;
var output = [];
for (var i = 0, f; f = files[i]; i++) {
output.push('<li><strong>', f.name, '</strong> (', f.type || 'n/a', ') - ',
f.size, ' bytes, last modified: ',
f.lastModifiedDate, '</li>');
var r = new FileReader();
r.onload = (function(f) {
return function(e) {
var contents = e.target.result;
var splitted = contents.split(/\r\n|\n/);
accInitTime = parseFloat(splitted[0].split(',')[0]);
accFrequency = parseFloat(splitted[1].split(',')[0]);
console.log(accFrequency);
drawAccGraphics(splitted, accInitTime, accFrequency, "accChartContainer");
};
})(f);
r.readAsText(f);
}
document.getElementById('selectedFile').innerHTML = '<ul>' + output.join('') + '</ul>';
document.getElementById('adjustTimeMode').style.display = "block";
}
var photoTypeDataPoints= [];
var graphData = [];
var currentEdaChart;
function drawEdaGraphics(splitted, initTime, frequency, chartSelection){
var contentsAsDataPoint = [];
// Increment in ms
increment = (1/frequency);
var interval = document.getElementById('interval').value;
var pointsInInterval = frequency * (interval / 1000);
console.log('Freq: ' + frequency + ', PointsForOneImage' + pointsInInterval);
for (var i = 2, time=initTime; i < splitted.length; i++, time+=increment) {
//contentsAsDataPoint.push({ x: time, y: parseFloat(splitted[i]) } );
contentsAsDataPoint.push({ y: parseFloat(splitted[i]) } );
}
for (var i = 0; i < numberOfImages; i++) {
var pathRegexp = /\/categories\/(.*?)\//g;
var categoryGroup = [];
var path = imageSequence[i];
var match = pathRegexp.exec(path);
while(i < numberOfImages){
var currentPathRegexp = /\/categories\/(.*?)\//g;
var currentPath = imageSequence[i];
var currentMatch = currentPathRegexp.exec(currentPath);
if(currentMatch[1] == match[1]){
for (var j = 0; j < pointsInInterval; j++) {
categoryGroup.push({x: pointsInInterval * i + j + categoryShift, y: -0.01});
}
i++;
}else{
i--;
break;
}
}
photoTypeDataPoints.push({type: match[1], categoryGroup});
}
console.log(photoTypeDataPoints);
graphData = [
{
showInLegend: false,
click: onClick,
type: "line",
dataPoints: contentsAsDataPoint
}
];
for (var i = 0; i < photoTypeDataPoints.length; i++) {
graphData.push(
{
showInLegend: true,
name: photoTypeDataPoints[i].type,
type: "line",
dataPoints: photoTypeDataPoints[i].categoryGroup
}
);
}
// Chart
var chart = new CanvasJS.Chart(chartSelection, {
zoomEnabled: true,
panEnabled: true,
legend: {
horizontalAlign: "right",
verticalAlign: "center"
},
axisY:{
includeZero: false
},
title:{
text: "EDA"
},
data: graphData,
legend: {
cursor: "pointer",
itemclick: function (e) {
//console.log("legend click: " + e.dataPointIndex);
//console.log(e);
if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
} else {
e.dataSeries.visible = true;
}
e.chart.render();
}
}
});
chart.render();
currentEdaChart = chart;
}
function myTrim(x) {
return x.replace(/^\s+|\s+$/gm,'');
}
function drawAccGraphics(splitted, initTime, frequency, chartSelection){
var xContentsAsDataPoint = [];
var yContentsAsDataPoint = [];
var zContentsAsDataPoint = [];
// Increment in ms
accIncrement = (1/frequency);
for (var i = 2, time=initTime; i < splitted.length; i++, time+=accIncrement) {
//contentsAsDataPoint.push({ x: time, y: parseFloat(splitted[i]) } );
// Check empty line
if(!splitted[i]){
break;
}
var xyzValues = splitted[i].split(',');
xContentsAsDataPoint.push({ y: parseFloat(myTrim(xyzValues[0])) } );
yContentsAsDataPoint.push({ y: parseFloat(myTrim(xyzValues[1])) } );
zContentsAsDataPoint.push({ y: parseFloat(myTrim(xyzValues[2])) } );
}
// Chart
var chart = new CanvasJS.Chart(chartSelection, {
zoomEnabled: true,
panEnabled: true,
legend: {
horizontalAlign: "right",
verticalAlign: "center"
},
axisY:{
includeZero: false
},
title:{
text: "ACC"
},
data: [
{
click: onAccClick,
type: "line",
dataPoints: xContentsAsDataPoint
},
{
click: onAccClick,
type: "line",
dataPoints: yContentsAsDataPoint
},
{
click: onAccClick,
type: "line",
dataPoints: zContentsAsDataPoint
}
]
});
chart.render();
}
function onClick(e){
var timeOfPoint = edaInitTime + (e.dataPoint.x * increment);
setImageAtTime(timeOfPoint);
}
function onAccClick(e){
var timeOfPoint = accInitTime + (e.dataPoint.x * accIncrement);
isAdjustTimeModeOn = document.getElementById("adjustTimeModeOn").checked;
if(isAdjustTimeModeOn){
adjustmentInMs = parseInt(imagesShownTimeList.times[0].date) - (timeOfPoint * 1000);
console.log("Adjustment: " + adjustmentInMs);
categoryShift = (e.dataPoint.x / accFrequency) * edaFrequency;
shiftCategoryIndexes();
} else {
setImageAtTime(timeOfPoint);
}
}
function shiftCategoryIndexes(){
//var interval = document.getElementById('interval').value;
//var pointsInInterval = edaFrequency * (interval / 1000);
for (var i = 1, count = 0; i < graphData.length; i++) {
for (var j = 0; j < graphData[i].dataPoints.length; j++) {
graphData[i].dataPoints[j].x = count + categoryShift;
count++;
}
}
currentEdaChart.render();
}
function setImageAtTime(timeOfPoint){
timeOfPointInMs = timeOfPoint * 1000;
timeOfPointInMs += adjustmentInMs;
console.log("setImageAtTime:" + timeOfPointInMs);
console.log("First image is shown at: " + JSON.stringify(imagesShownTimeList.times[0].date));
if(imagesShownTimeList != [] && parseInt(imagesShownTimeList.times[0].date) <= timeOfPointInMs){
for (var i = 1; i < imagesShownTimeList.times.length; i++) {
//console.log(JSON.stringify(imagesShownTimeList.times[i].dateImageShown));
if(parseInt(imagesShownTimeList.times[i].date) >= timeOfPointInMs)
{
image.src = JSON.stringify(imagesShownTimeList.times[i-1].imageFile).split('"').join('');
console.log(image.src);
break;
} else if(i == imagesShownTimeList.times.length - 1 && timeOfPointInMs > parseInt(imagesShownTimeList.times[i].date) ){
// Last image
image.src = JSON.stringify(imagesShownTimeList.times[i].imageFile).split('"').join('');
console.log(image.src);
break;
}
}
}else{
console.log("Image not found!");
image.src = "resources/images/imageNotFound.png";
}
}
</script>
</body>
</html>