forked from jakesgordon/javascript-racer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
115 lines (106 loc) · 2.78 KB
/
helper.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
//Car control
const Car = {
goLeft: () => {
keyRight = false;
keyLeft = true;
},
goRight: () => {
keyLeft = false;
keyRight = true;
},
goStraight: () => {
keyLeft = false;
keyRight = false;
},
goFaster: () => {
keySlower = false;
keyFaster = true;
},
goSlower: () => {
keyFaster = false;
keySlower = true;
}
};
//Hyperparameters
const Hyperparameters = {
get learningRate() {
return Number($('#learning-rate').val());
},
get epochs() {
return Number($('#epochs').val());
},
get batchSize() {
return Number($('#batch-size').val());
},
get hiddenUnits() {
return Number($('#hidden-units').val());
}
};
//UI
const UI = {
exampleHandler: () => {},
learnHandler: async () => {},
updateCarDirection: async () => {}
};
//Webcam
const Webcam = {
get webcamImage() {
return document.getElementById('webcam');
}
};
$(document).ready(async () => {
const videoElement = document.getElementById('webcam');
if (navigator.getUserMedia) {
const stream = await navigator.mediaDevices.getUserMedia({video: true});
videoElement.height = MOBILE_NET_HEIGHT;
videoElement.width = stream.getVideoTracks()[0].getSettings().aspectRatio * MOBILE_NET_WIDTH;
videoElement.srcObject = stream;
}
});
//Util
const TFUtil = {
cropImage: (tfImage) => {
const size = Math.min(tfImage.shape[0], tfImage.shape[1]);
const centerHeight = tfImage.shape[0] / 2;
const beginHeight = centerHeight - (size / 2);
const centerWidth = tfImage.shape[1] / 2;
const beginWidth = centerWidth - (size / 2);
return tfImage.slice([beginHeight, beginWidth, 0], [size, size, 3]);
}
};
//Listeners
$(document).ready(() => {
$('#start-game-button').one('click', async function () {
this.disabled = true;
Game.start();
Car.goFaster();
while (true) {
UI.updateCarDirection();
await tf.nextFrame();
}
});
$('.example-button').click(function () {
$('.example-button').each((i,e) => e.disabled = true);
UI.exampleHandler($(this).data('label'));
$(this).closest('div').find('.nb').each((i,e) => $(e).text(Number($(e).text()) + 1));
$('.example-button').each(async (i,e) => e.disabled = false);
if (!$('.example-button').closest('div').find('.nb').map((i,e) => Number($(e).text())).is((i,e) => e === 0)) {
$('#learn-button').each((i,e) => e.disabled = false);
}
});
$('#learn-button').click(async function () {
$('.example-button').each((i,e) => e.disabled = true);
this.disabled = true;
const visor = tfvis.visor();
visor.open();
visor.unbindKeys();
const surface = visor.surface({
name: 'Training'
});
const fitCallbacks = tfvis.show.fitCallbacks(surface, ['loss'], {height: 300, zoomToFitAccuracy: true});
await UI.learnHandler(fitCallbacks);
this.disabled = false;
$('.example-button').each(async (i,e) => e.disabled = false);
$(this).text('ReLearn');
});
});