-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.js
More file actions
162 lines (146 loc) · 3.7 KB
/
final.js
File metadata and controls
162 lines (146 loc) · 3.7 KB
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
let mobilenet;
let classifier;
let video;
let direction = 'right';
let noOfUpImages=0;
let noOfDownImages=0;
let noOfLeftImages=0;
let noOfRightImages=0;
let Score =0;
let videoElement=document.getElementById("videoElement");
let canv=document.getElementById("random");
ctx=canv.getContext("2d");
setInterval(game,1000/15);
let numClasses=4;
let numLabels=4;
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then(function(stream) {
videoElement.srcObject = stream;
videoElement.play();
mobilenet = ml5.featureExtractor('MobileNet',{numClasses:4,numLabels:4}, modelReady);
// mobilenet.numClasses=numClasses;
// mobilenet.numLabels=numLabels;
console.log(mobilenet);
classifier = mobilenet.classification(videoElement,4, videoReady);
})
.catch(function(err) {
console.log("An error occurred while trying to use the webcam " + err);
});
function modelReady() {
console.log('Model is ready!');
}
function videoReady() {
console.log('Video is ready!!!');
}
function whileTraining(loss) {
if (loss == null) {
document.getElementById("loading-sprite").classList.remove("active");
console.log('Training Complete');
classifier.classify(gotResults);
} else {
console.log(loss);
}
}
function gotResults(error, result) {
if (error) {
console.error(error);
} else {
direction = result[0].label;
console.log(direction);
keyPresses();
classifier.classify(gotResults);
}
}
function upDir(){
noOfUpImages++;
classifier.addImage('up');
document.getElementById("upBtn").innerHTML= "Up "+noOfUpImages;
}
function downDir(){
noOfDownImages++;
classifier.addImage('down');
document.getElementById("downBtn").innerHTML= "Down "+noOfDownImages;
}
function leftDir(){
noOfLeftImages++;
classifier.addImage('left');
document.getElementById("leftBtn").innerHTML= "Left "+noOfLeftImages;
}
function rightDir(){
noOfRightImages++;
classifier.addImage('right');
document.getElementById("rightBtn").innerHTML= "Right "+noOfRightImages;
}
function trainmdl(){
document.getElementById("loading-sprite").classList.add("active");
classifier.train(whileTraining);
}
/*xv = X Velocity,yv = Y Velocity
px = Player X, py = Player Y
gs = Grid Size,tc = Tile Count
ax = Apple X, Apple Y
Trail Stores Previous Position Upto Tail Size*/
/*Snake Game base attributed to https://www.youtube.com/watch?v=xGmXxpIj6vs
with some modifications for controls with CNN and Score*/
px=py=10;
gs=tc=20;//20*20 = 400size
ax=ay=15;
xv=yv=0;
trail=[];
tail = 5;
function game() {
px+=xv;
py+=yv;
if(px<0) {
px= tc-1;
}
if(px>tc-1) {
px= 0;
}
if(py<0) {
py= tc-1;
}
if(py>tc-1) {
py= 0;
}
ctx.fillStyle="black";
ctx.fillRect(0,0,canv.width,canv.height);
ctx.font = "2em Arial";
ctx.fillStyle="white";
ctx.fillText("Score: "+Score,5,40);
ctx.fillStyle="lime";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) {
tail = 5;
}
}
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
if(ax==px && ay==py) {
tail++;
ax=Math.floor(Math.random()*tc);
ay=Math.floor(Math.random()*tc);
Score++;
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
}
function keyPresses() {
switch(direction) {
case "left":
xv=-1;yv=0;
break;
case "up":
xv=0;yv=-1;
break;
case "right":
xv=1;yv=0;
break;
case "down":
xv=0;yv=1;
break;
}
}