-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
176 lines (141 loc) · 4.94 KB
/
script.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
const videoElement = document.getElementsByClassName('input_video')[0];
const canvasElement = document.getElementsByClassName('output_canvas')[0];
const rectangleElement = document.getElementsByClassName('rectangle')[0];
const canvasCtx = canvasElement.getContext('2d');
const rectCtx = rectangleElement.getContext('2d');
var automatic = true;
var indexX = 0;
var indexY = 0;
var leftX = 100 + Math.floor(Math.random() * 800);
var leftY = Math.floor(Math.random() * 500);
var checkTime = 0;
var good = 0;
var bad = 0;
var correct = false;
var newRect = true;
var buffer = 0;
function pressZ(opacityString){
var pressZ = document.getElementById("pressZ");
pressZ.style.opacity = opacityString
}
function draw(color){
rectCtx.clearRect(0, 0, 1280, 720);
const rectWidth = 50
const rectHeight = 30
leftX = 100 + Math.floor(Math.random() * 800);
leftY = 100 + Math.floor(Math.random() * 400);
rectCtx.beginPath();
rectCtx.lineWidth = "6";
rectCtx.strokeStyle = color;
rectCtx.rect(leftX, leftY, rectWidth, rectHeight);
rectCtx.stroke();
return leftX, leftY
}
function check(indexX,indexY){
const rectWidth = 50
const rectHeight = 30
if(indexX >= leftX && indexX <= leftX + rectWidth && indexY >= leftY && indexY <= leftY + rectHeight){
rectCtx.clearRect(0, 0, 1280, 720);
rectCtx.beginPath();
rectCtx.lineWidth = "6";
var color = 'red'
if(checkTime >= 50){
if(good/checkTime >= 0.4) {color = 'green'; correct = true;}
checkTime = 0
good = 0
bad = 0
}
rectCtx.strokeStyle = color;
rectCtx.rect(leftX, leftY, rectWidth, rectHeight);
rectCtx.stroke();
return true;
}else{
rectCtx.clearRect(0, 0, 1280, 720);
rectCtx.beginPath();
rectCtx.lineWidth = "6";
rectCtx.strokeStyle = 'red';
rectCtx.rect(leftX, leftY, rectWidth, rectHeight);
rectCtx.stroke();
return false;
}
}
function debugHand(){
var checkBox = document.getElementById("debugHand");
return checkBox.checked
}
function automaticCheck(){
var checkBox = document.getElementById("automatic");
automatic = checkBox.checked
return automatic
}
function onResults(results) {
canvasCtx.save();
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
canvasCtx.drawImage(
results.image, 0, 0, canvasElement.width, canvasElement.height);
if (results.multiHandLandmarks) {
for (const landmarks of results.multiHandLandmarks) {
indexX = landmarks[8].x * 1280
indexY = landmarks[8].y * 720
if(debugHand()){
drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS,
{color: '#00FF00', lineWidth: 5});
drawLandmarks(canvasCtx, landmarks, {color: '#FF0000', lineWidth: 2});
}
if(newRect) checkTime = 0
if(correct){
if(automaticCheck()){
buffer++;
if(buffer >= 20){
draw("red")
newRect = true;
correct = false;
pressZ("0")
buffer = 0
}
}else{
pressZ("1");
}
continue
}
if(check(indexX,indexY)){
console.log(good + " " + bad + " " + checkTime + " " + newRect)
good++;
checkTime++;
newRect = false
}else {
if(!newRect){bad++; checkTime++;}
}
}
}
canvasCtx.restore();
}
const hands = new Hands({locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
}});
hands.setOptions({
maxNumHands: 2,
modelComplexity: 1,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5
});
hands.onResults(onResults);
const camera = new Camera(videoElement, {
onFrame: async () => {
await hands.send({image: videoElement});
},
width: 1280,
height: 720
});
camera.start();
draw('red') //need to call it once at the start
window.addEventListener('keydown', (e) => {
switch (e.key) {
case 'z':
draw("red")
newRect = true;
correct = false;
pressZ("0")
break
}
})