forked from goblin45/squid_game_2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
325 lines (254 loc) · 9.08 KB
/
main.py
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
import numpy as np
import cv2 as cv
import threading
import time
import audio
import light
import countdown
import distance
def startGame():
# time.sleep(3)
timeThread = threading.Thread(target=countdown.countTime, args=())
timeThread.start()
hog = cv.HOGDescriptor()
hog.setSVMDetector(cv.HOGDescriptor_getDefaultPeopleDetector())
prev_gray = None
movement_threshold = 5000
player_count = 0
motion_detected = [False] * 10
min_area_threshold = 1500
def preprocess_frame(frame):
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
gray = cv.GaussianBlur(gray, (21, 21), 0)
return gray
def detect_bodies(frame):
(rects, _) = hog.detectMultiScale(
frame, winStride=(4, 4), padding=(8, 8), scale=1.05
)
return rects
capture = cv.VideoCapture(0)
faceCascade = cv.CascadeClassifier(
cv.data.haarcascades + "haarcascade_frontalface_default.xml"
)
# sizing game window
cv.namedWindow("Game Window", cv.WINDOW_NORMAL)
cv.setWindowProperty("Game Window", cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN)
TOLERANCE = 5
global winner
winner = ""
winFlag = False
players = []
colors = [
(0, 255, 255), # Yellow
(147, 20, 255), # Pink
(0, 100, 0), # Dark Green
(0, 0, 128), # Maroon
(128, 128, 128), # Gray
]
FINISH = 25
while True:
ret, frame = capture.read()
if not ret:
break
frame_gray = preprocess_frame(frame)
if prev_gray is not None:
frame_diff = cv.absdiff(prev_gray, frame_gray)
_, thresh = cv.threshold(frame_diff, 30, 255, cv.THRESH_BINARY)
contours, _ = cv.findContours(
thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE
)
for i in range(player_count):
if motion_detected[i]:
cv.putText(
frame,
f"",
(10, 50 + i * 30),
cv.FONT_HERSHEY_SIMPLEX,
1,
(0, 0, 255),
2,
)
prev_gray = frame_gray.copy()
bodies = detect_bodies(frame_gray)
grayFrame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
grayFrame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)
)
# get distance from finishing line
distances = distance.get_distances(faces)
disq_players = 0
for i, (x, y, w, h) in enumerate(faces):
cv.rectangle(frame, (x, y), (x + w, y + h), colors[i % 5], 2)
# positioning of player id
text = "Player " + str(i + 1)
text_size, _ = cv.getTextSize(text, cv.FONT_HERSHEY_SIMPLEX, 0.5, 2)
text_x = x + (w - text_size[0]) // 2
text_y = y - 10
cv.putText(
frame,
text,
(text_x, text_y),
cv.FONT_HERSHEY_SIMPLEX,
0.5,
colors[i % 5],
2,
)
if distances[i] <= 25:
winner = str(i + 1)
winFlag = True
break
# positioning of distance measurement
text = "Distance: " + str(distances[i].round(2))
text_size, _ = cv.getTextSize(text, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
text_x = x + (w - text_size[0]) // 2
text_y = y + h + 20
cv.putText(
frame,
text,
(text_x, text_y),
cv.FONT_HERSHEY_SIMPLEX,
0.5,
colors[i % 5],
1,
)
motion_detected[i] = True
print(f"Player {i+1} Top-Left Coordinate: ({x}, {y})")
if i + 1 > len(players):
while len(players) < i + 1:
players.append([])
players[i].append((x, y))
# we are taking past 20 frames into consideration to detect a movement
if len(players[i]) > 20: # this parameter should be tuned
players[i].pop()
if light.currLight == (0, 255, 0): # green; player can move
for player in players:
player.clear()
else:
for i in range(player_count):
if motion_detected[i]:
text = "Player " + str(i + 1) + " is out."
text_size, _ = cv.getTextSize(
text, cv.FONT_HERSHEY_SIMPLEX, 1.5, 2
)
disq_players += 1
if disq_players == player_count:
break
# bottom center position for player cancel notification
text_x = (frame.shape[1] - text_size[0]) // 2
text_y = frame.shape[0] - 30
cv.putText(
frame,
text,
(text_x, text_y),
cv.FONT_HERSHEY_SIMPLEX,
1.5,
(0, 0, 255),
2,
)
player_count = len(bodies)
text = countdown.currTime
text_size, _ = cv.getTextSize(text, cv.FONT_HERSHEY_SIMPLEX, 1, 2)
padding = 20
# top left position for time display
text_x = padding
text_y = text_size[1] + padding
cv.putText(
frame,
countdown.currTime,
(text_x, text_y),
cv.FONT_HERSHEY_SIMPLEX,
1,
(255, 0, 0),
2,
)
# positioning of light circle
lightRadius = 20
center_x = frame.shape[1] - text_size[0] - padding // 2
center_y = text_size[1] + padding
cv.circle(frame, (center_x, center_y), lightRadius, light.currLight, -1)
cv.imshow("Game Window", frame)
if winFlag:
audio.gameOn = False
light.gameOn = False
showResults()
break
if (
(cv.waitKey(1) & 0xFF == ord("q"))
or countdown.timeOver
or disq_players == len(players)
):
audio.gameOn = False
light.gameOn = False
showGameOver()
break
capture.release()
cv.destroyAllWindows()
def showResults():
# sizing results window
cv.namedWindow("Results Window", cv.WINDOW_NORMAL)
cv.setWindowProperty("Results Window", cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN)
resultsImage = np.uint8(np.full((800, 1600, 3), 255))
winnerStr = "Winner: Player " + winner
text = winnerStr
text_size, _ = cv.getTextSize(text, cv.FONT_HERSHEY_SIMPLEX, 2, 2)
# center position for menu prompt
text_x = (resultsImage.shape[1] - text_size[0]) // 2
text_y = (resultsImage.shape[0] + text_size[1]) // 2
cv.putText(
resultsImage, text, (text_x, text_y), cv.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 2
)
cv.imshow("Results Window", resultsImage)
cv.waitKey(0)
def showGameOver():
# sizing game over window
cv.namedWindow("Game Over Window", cv.WINDOW_NORMAL)
cv.setWindowProperty(
"Game Over Window", cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN
)
gameOverImage = np.uint8(np.full((800, 1600, 3), 255))
text = "GAME OVER: Everyone lost!"
text_size, _ = cv.getTextSize(text, cv.FONT_HERSHEY_SIMPLEX, 1, 2)
# center position for menu prompt
text_x = (gameOverImage.shape[1] - text_size[0]) // 2
text_y = (gameOverImage.shape[0] + text_size[1]) // 2
cv.putText(
gameOverImage,
text,
(text_x, text_y),
cv.FONT_HERSHEY_SIMPLEX,
1,
(100, 100, 100),
2,
)
cv.imshow("Game Over Window", gameOverImage)
cv.waitKey(0)
def showmenu():
# sizing menu window
cv.namedWindow("Menu Window", cv.WINDOW_NORMAL)
cv.setWindowProperty("Menu Window", cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN)
menuImage = np.uint8(np.full((800, 1600, 3), 255))
text = "Press Enter to Start Game"
text_size, _ = cv.getTextSize(text, cv.FONT_HERSHEY_SIMPLEX, 2, 2)
# center position for menu prompt
text_x = (menuImage.shape[1] - text_size[0]) // 2
text_y = (menuImage.shape[0] + text_size[1]) // 2
cv.putText(
menuImage, text, (text_x, text_y), cv.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 2
)
cv.imshow("Menu Window", menuImage)
key = cv.waitKey(0)
if key == 13:
time.sleep(0.2)
cv.destroyAllWindows()
return
if __name__ == "__main__":
showmenu()
videoThread = threading.Thread(target=startGame, args=())
videoThread.start()
audio.gameOn = True
audioThread = threading.Thread(target=audio.loopAudio, args=())
audioThread.start()
light.gameOn = True
lightThread = threading.Thread(target=light.loopLight, args=())
lightThread.start()
audioThread.join()