-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
206 lines (186 loc) · 6.8 KB
/
main.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
// Imports and DOM
import * as tf from '@tensorflow/tfjs'
import * as handpose from '@tensorflow-models/handpose'
import * as fp from 'fingerpose'
import { rockGesture, paperGesture, scissorsGesture } from './poses'
const video = document.querySelector('video')
const canvas = document.querySelector('canvas')
const gestureText = document.getElementById('gesture-text')
const loadingStatus = document.getElementById('loading-status')
const autoButton = document.getElementById('auto-button')
const manualButton = document.getElementById('manual-button')
const mode = document.getElementById('mode')
const tutorialOpenBtn = document.getElementById('open-how-to-play')
const tutorialCloseBtn = document.getElementById('close-how-to-play')
const tutorial = document.getElementById('how-to-play')
const playerChoiceText = document.getElementById('player-choice')
const computerChoiceText = document.getElementById('computer-choice')
const resultDiv = document.getElementById('result-div')
const resultElement = document.getElementById('result')
const playerScore = document.getElementById('player-score')
const computerScore = document.getElementById('computer-score')
const computerChoices = ['rock', 'paper', 'scissors']
let auto = false
let gameLoop
function playerWins() {
resultElement.innerText = 'You Win!'
resultDiv.style.backgroundColor = 'green'
playerScore.innerText++
}
function computerWins() {
resultElement.innerText = 'Computer Wins!'
resultDiv.style.backgroundColor = 'red'
computerScore.innerText++
}
function draw() {
resultElement.innerText = 'Draw!'
resultDiv.style.backgroundColor = '#cc9900'
}
function outCome(playerChoice, computerChoice) {
switch(playerChoice) {
case 'rock':
switch(computerChoice) {
case 'rock':
draw()
break
case 'paper':
computerWins()
break
case 'scissors':
playerWins()
break
}
break
case 'paper':
switch(computerChoice) {
case 'rock':
playerWins()
break
case 'paper':
draw()
break
case 'scissors':
computerWins()
break
}
break
case 'scissors':
switch(computerChoice) {
case 'rock':
computerWins()
break
case 'paper':
playerWins()
break
case 'scissors':
draw()
break
}
}
}
// open/close tutoria
tutorialOpenBtn.onclick = ()=> {
tutorial.style.display = 'grid'
}
tutorialCloseBtn.onclick = ()=> {
tutorial.style.display = 'none'
}
// Set video stream to webcam
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({video: true})
.then(steam => {
video.srcObject = steam
}).catch(error => {
console.log('not working')
})
}
// Main function for entire app
async function handPoseInit() {
const net = await handpose.load()
console.log('Model loaded')
setInterval(() => {
detectHand(net)
}, 100);
}
// hand detection
async function detectHand(net) {
if (video.readyState === 4) {
loadingStatus.innerText = 'Ready'
loadingStatus.style.animation = 'none'
const videoWidth = video.videoWidth
const videoHeight = video.videoHeight
canvas.width = videoWidth
canvas.height = videoHeight
const hand = await net.estimateHands(video)
// console.log(hand)
drawhand(hand, canvas.getContext('2d'))
if (hand.length > 0) {
const GE = new fp.GestureEstimator([rockGesture, paperGesture, scissorsGesture])
const gestureEstimate = await GE.estimate(hand[0].landmarks, 8)
console.log(gestureEstimate.gestures[0].name)
gestureText.innerText = gestureEstimate.gestures[0].name
}
autoButton.onclick = ()=> {
mode.innerText = 'Automatic'
async function game() {
let computerChoice = computerChoices[Math.floor(Math.random()*3)]
if (hand.length > 0) {
const GE = new fp.GestureEstimator([rockGesture, paperGesture, scissorsGesture])
const gestureEstimate = await GE.estimate(hand[0].landmarks, 8)
const playerChoice = gestureEstimate.gestures[0].name
console.log('You choose: ' + playerChoice)
playerChoiceText.innerText = playerChoice
computerChoiceText.innerText = computerChoice
console.log('comp choice: ' + computerChoice)
outCome(playerChoice, computerChoice)
}
}
auto = !auto
console.log(auto)
if (auto === true) {
clearInterval(gameLoop)
gameLoop = setInterval(game, 500)
console.log('loop started')
} else {
clearInterval(gameLoop)
console.log(gameLoop)
console.log('loop cleared')
mode.innerText = 'None'
}
}
manualButton.onclick = async()=> {
clearInterval(gameLoop)
mode.innerText = 'Click to play'
let computerChoice = computerChoices[Math.floor(Math.random()*3)]
if (hand.length > 0) {
const GE = new fp.GestureEstimator([rockGesture, paperGesture, scissorsGesture])
const gestureEstimate = await GE.estimate(hand[0].landmarks, 8)
const playerChoice = gestureEstimate.gestures[0].name
console.log('You choose: ' + playerChoice)
playerChoiceText.innerText = playerChoice
computerChoiceText.innerText = computerChoice
console.log('comp choice: ' + computerChoice)
outCome(playerChoice, computerChoice)
}
}
}
}
// Drawing the hand overlay
function drawhand(predictions, ctx) {
console.log(predictions)
if (predictions.length > 0) {
predictions.forEach(prediction => {
const landmarks = prediction.landmarks
for (let i = 0; i < landmarks.length; i++) {
const x = landmarks[i][0]
const y = landmarks[i][1]
ctx.beginPath()
ctx.arc(x, y, 5, 0, (2*Math.PI))
ctx.fillStyle = 'blue'
ctx.fill()
}
});
}
}
// Calling the main function
handPoseInit()