-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounce.py
447 lines (271 loc) · 12.6 KB
/
bounce.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import cv2
import mediapipe as mp
import numpy as np
from tkinter import *
import random
import time
speed_level = 2 # you can increase the speed of the ball
#Constraints
head_size = 100
neck_size = 10
eye_size = 10
sholder_length = 100
body_height = 200
fingure_size = 10
Body_parts = []
draw_man = FALSE
# ------------------ CANVAS SETTINGS ------------------
tk = Tk()
WIDTH = 800
HEIGHT = 500
canvas = Canvas(tk, width = WIDTH, height = HEIGHT)
canvas.pack()
# ------------------ POSE DETECTION ------------------
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
cap = cv2.VideoCapture(0)
# ------------------ GAME PRE-REQUISITES ------------------
score = 0
stick_height = 20
stick_length = 120
#creating ball
ball = canvas.create_oval(10,10,50,50,fill="red",outline="black")
#creating boundary
boundary = canvas.create_rectangle(stick_height, stick_height, WIDTH - stick_height, HEIGHT - stick_height,outline="red")
if speed_level == 1:
x_speed = 10
y_speed = 10
if speed_level == 2:
x_speed = 15
y_speed = 15
if speed_level > 2:
x_speed = 20
y_speed = 20
# ------------------ SETUP MEDIAPIPE INSTANCE ------------------
with mp_pose.Pose(min_detection_confidence = 0.5, min_tracking_confidence = 0.5) as pose:
while cap.isOpened():
ret, frame = cap.read()
# Recolor image to RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Make detection
result = pose.process(image)
# Recolor back to BGR
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
head = [-300, -300]
R_hand_mid = [-300, -300]
R_hand_end = [-300, -300]
L_hand_mid = [-300, -300]
L_hand_end = [-300, -300]
R_leg_mid = [-300, -300]
R_leg_end = [-300, -300]
L_leg_mid = [-300, -300]
L_leg_end = [-300, -300]
# ------------------ EXTRACT LANDMARK ------------------
try:
landmarks = result.pose_landmarks.landmark
head = [WIDTH * (1-landmarks[mp_pose.PoseLandmark.NOSE.value].x), HEIGHT * landmarks[mp_pose.PoseLandmark.NOSE.value].y]
L_hand_mid = [WIDTH *(1- landmarks[mp_pose.PoseLandmark.LEFT_ELBOW].x), HEIGHT * landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y]
L_hand_end = [WIDTH * (1-landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x), HEIGHT * landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y]
R_hand_mid = [WIDTH * (1-landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW].x), HEIGHT * landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y]
R_hand_end = [WIDTH * (1-landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x), HEIGHT * landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y]
L_leg_mid = [WIDTH * (1-landmarks[mp_pose.PoseLandmark.LEFT_KNEE].x), HEIGHT * landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y]
L_leg_end = [WIDTH * (1-landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x), HEIGHT * landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].y]
R_leg_mid = [WIDTH * (1-landmarks[mp_pose.PoseLandmark.RIGHT_KNEE].x), HEIGHT * landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y]
R_leg_end = [WIDTH * (1-landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x), HEIGHT * landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].y]
except:
pass
# ------------------ RENDER DETECTIONS ------------------
mp_drawing.draw_landmarks(image, result.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245, 117, 66), thickness=2, circle_radius=2),
mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=2)
)
# ------------------ HEAD ------------------
head_r = head_size / 2
head_x1 = head[0] - head_r
head_y1 = head[1] - head_r
head_x2 = head_x1 + head_size
head_y2 = head_y1 + head_size
head_center_x = head_x1 + head_r
head_center_y = head_y1 + head_r
# ------------------ SMILE ------------------
smile_x1 = head_center_x - 10
smile_y1 = head_center_y + head_r/2
smile_x2 = head_center_x + 10
smile_y2 = head_center_y + head_r/2 + 10
# ------------------ NECK ------------------
neck_mid_x = head_center_x
neck_mid_y = head_center_y + head_r
neck_x1 = neck_mid_x - 5
neck_y1 = neck_mid_y
neck_x2 = neck_mid_x + 5
neck_y2 = neck_mid_y + neck_size
# ------------------ EYES ------------------
L_eye_x2 = head_center_x - head_r / 2
L_eye_y2 = head_center_y
L_eye_x1 = L_eye_x2 - eye_size
L_eye_y1 =L_eye_y2 - eye_size
R_eye_x2 = head_center_x + head_r / 2
R_eye_y2 = head_center_y
R_eye_x1 = R_eye_x2 + eye_size
R_eye_y1 = R_eye_y2 - eye_size
# ------------------ SHOLDER ------------------
sholder_mid_x = head_center_x
sholder_mid_y = head_center_y + head_r + neck_size
sholder_x1 = sholder_mid_x - (sholder_length / 2)
sholder_y1 = sholder_mid_y
sholder_x2 = sholder_mid_x + (sholder_length / 2)
sholder_y2 = sholder_mid_y
# ------------------ BODY ------------------
body_y2 = sholder_y2 + body_height
# ------------------ HANDS ------------------
l_hand_x = sholder_x1
L_hand_y = sholder_y1
R_hand_x = sholder_x2
R_hand_y = sholder_y2
L_hand_mid_x = L_hand_mid[0]
L_hand_mid_y = L_hand_mid[1]
L_hand_end_x = L_hand_end[0]
L_hand_end_y = L_hand_end[1]
R_hand_mid_x = R_hand_mid[0]
R_hand_mid_y = R_hand_mid[1]
R_hand_end_x = R_hand_end[0]
R_hand_end_y = R_hand_end[1]
# ------------------ HAND FINGERS ------------------
Lh_fingure_x1 = L_hand_end_x - fingure_size
Lh_fingure_y1 = L_hand_end_y - fingure_size
Lh_fingure_x2 = L_hand_end_x + fingure_size
Lh_fingure_y2 = L_hand_end_y + fingure_size
Rh_fingure_x1 = R_hand_end_x - fingure_size
Rh_fingure_y1 = R_hand_end_y - fingure_size
Rh_fingure_x2 = R_hand_end_x + fingure_size
Rh_fingure_y2 = R_hand_end_y + fingure_size
# ------------------ LEGS ------------------
L_leg_x = sholder_x1
L_leg_y = sholder_y1 + body_height
R_leg_x = sholder_x2
R_leg_y = sholder_y2 + body_height
L_leg_mid_x = L_leg_mid[0]
L_leg_mid_y = L_leg_mid[1]
L_leg_end_x = L_leg_end[0]
L_leg_end_y = L_leg_end[1]
R_leg_mid_x = R_leg_mid[0]
R_leg_mid_y = R_leg_mid[1]
R_leg_end_x = R_leg_end[0]
R_leg_end_y = R_leg_end[1]
# ------------------ LEG FINGERS ------------------
Ll_fingure_x1 = L_leg_end_x - fingure_size
Ll_fingure_y1 = L_leg_end_y - fingure_size
Ll_fingure_x2 = L_leg_end_x + fingure_size
Ll_fingure_y2 = L_leg_end_y + fingure_size
Rl_fingure_x1 = R_leg_end_x - fingure_size
Rl_fingure_y1 = R_leg_end_y - fingure_size
Rl_fingure_x2 = R_leg_end_x + fingure_size
Rl_fingure_y2 = R_leg_end_y + fingure_size
if draw_man == TRUE:
head = canvas.create_oval(head_x1, head_y1, head_x2, head_y2, fill="pink")
hairs = canvas.create_arc(head_x1, head_y1, head_x2, head_y2 - 3 * eye_size, start=0, extent=180, fill="black")
smile = canvas.create_arc(smile_x1, smile_y1, smile_x2, smile_y2, start=0, extent=-180, fill="black")
neck = canvas.create_rectangle(neck_x1, neck_y1, neck_x2, neck_y2)
L_eye = canvas.create_oval(L_eye_x1, L_eye_y1, L_eye_x2, L_eye_y2, fill="blue")
R_eye = canvas.create_oval(R_eye_x1, R_eye_y1, R_eye_x2, R_eye_y2, fill="blue")
body = canvas.create_rectangle(sholder_x1, sholder_y1, sholder_x2, body_y2)
Left_hand_mid = canvas.create_line(L_hand_x, L_hand_y, L_hand_mid_x, L_hand_mid_y)
Left_hand_end = canvas.create_line(L_hand_mid_x, L_hand_mid_y, L_hand_end_x, L_hand_end_y)
Right_hand_mid = canvas.create_line(R_hand_x, R_hand_y, R_hand_mid_x, R_hand_mid_y)
Right_hand_end = canvas.create_line(R_hand_mid_x, R_hand_mid_y, R_hand_end_x, R_hand_end_y)
Lh_fingures = canvas.create_oval(Lh_fingure_x1, Lh_fingure_y1, Lh_fingure_x2, Lh_fingure_y2, fill="black")
Rh_fingures = canvas.create_oval(Rh_fingure_x1, Rh_fingure_y1, Rh_fingure_x2, Rh_fingure_y2, fill="black")
Left_leg_mid = canvas.create_line(L_leg_x, L_leg_y, L_leg_mid_x, L_leg_mid_y)
Left_leg_end = canvas.create_line(L_leg_mid_x, L_leg_mid_y, L_leg_end_x, L_leg_end_y)
Right_leg_mid = canvas.create_line(R_leg_x, R_leg_y, R_leg_mid_x, R_leg_mid_y)
Right_leg_end = canvas.create_line(R_leg_mid_x, R_leg_mid_y, R_leg_end_x, R_leg_end_y)
Ll_fingures = canvas.create_oval(Ll_fingure_x1, Ll_fingure_y1, Ll_fingure_x2, Ll_fingure_y2, fill="black")
Rl_fingures = canvas.create_oval(Rl_fingure_x1, Rl_fingure_y1, Rl_fingure_x2, Rl_fingure_y2, fill="black")
# ------------------ BODY PARTS ------------------
Body_parts = [head, hairs, smile, neck, L_eye, R_eye, body, Left_hand_mid, Left_hand_end, Right_hand_mid,
Right_hand_end, Lh_fingures, Rh_fingures, Left_leg_mid, Left_leg_end, Right_leg_mid,
Right_leg_end, Ll_fingures, Rl_fingures]
# ------------------ HORIZONTAL STICKS ------------------
stick_1_x1 = head_x1
stick_1_y1 = HEIGHT - stick_height
stick_1_x2 = stick_1_x1 + stick_length
stick_1_y2 = HEIGHT
stick_1 = canvas.create_rectangle(stick_1_x1, stick_1_y1, stick_1_x2, stick_1_y2, fill="black",outline="red") # BOTTOM
stick_2 = canvas.create_rectangle(stick_1_x1, 0, stick_1_x2, stick_height, fill="black",outline="red") # TOP
# ------------------ VERTICAL STICKS ------------------
stick_3_x1 = WIDTH - stick_height
stick_3_y1 = R_hand_end_y
stick_3_x2 = WIDTH
stick_3_y2 = stick_3_y1 + stick_length
stick_4_x1 = 0
stick_4_y1 = L_hand_end_y
stick_4_x2 = stick_height
stick_4_y2 = stick_4_y1 + stick_length
stick_3 = canvas.create_rectangle(stick_3_x1, stick_3_y1, stick_3_x2, stick_3_y2, fill="black",outline="red") # RIGHT
stick_4 = canvas.create_rectangle(stick_4_x1, stick_4_y1, stick_4_x2, stick_4_y2, fill="black",outline="red") # LEFT
# ------------------ BALL ------------------
canvas.move(ball, x_speed, y_speed)
pos_stick_1 = canvas.coords(stick_1)
pos_stick_2 = canvas.coords(stick_2)
pos_stick_3 = canvas.coords(stick_3)
pos_stick_4 = canvas.coords(stick_4)
pos_ball = canvas.coords(ball)
# ------------------ HORIZONTAL STICKS CONTROL ------------------
if pos_stick_1[1] <= pos_ball[3]:
if pos_stick_1[0] <= pos_ball[2] and pos_stick_1[2] >= pos_ball[0]:
y_speed = -(y_speed + 1)
score += 1
else:
y_speed = -(y_speed - 1)
score -= 1
if pos_stick_2[3] >= pos_ball[1]:
if pos_stick_2[0] <= pos_ball[2] and pos_stick_2[2] >= pos_ball[0]:
y_speed = -(y_speed + 1)
score += 1
else:
y_speed = -(y_speed - 1)
score -= 1
# ------------------ VERTICAL STICKS CONTROL ------------------
if pos_stick_3[0] <= pos_ball[2]:
if pos_stick_3[1] <= pos_ball[1] and pos_stick_3[3] >= pos_ball[3]:
x_speed = -(x_speed + 1)
score += 1
else:
x_speed = -(x_speed - 1)
score -= 1
if pos_stick_4[2] >= pos_ball[0]:
if pos_stick_4[1] <= pos_ball[1] and pos_stick_4[3] >= pos_ball[3]:
x_speed = -(x_speed + 1)
score += 1
else:
x_speed = -(x_speed - 1)
score -= 1
# ------------------ IF BALL MOVES OUT OF CANVAS ------------------
if pos_ball[1] <= -200 or pos_ball[3] >= HEIGHT + 200:
canvas.delete(ball)
ball = canvas.create_oval(50, 50, 100, 100, fill = "black")
if pos_ball[0] <= -200 or pos_ball[2] >= WIDTH + 200:
canvas.delete(ball)
ball = canvas.create_oval(50, 50, 100, 100, fill = "black")
text = canvas.create_text(100, 10, fill="darkblue", font="serif 15 bold", text="Score = {}".format(score))
tk.update()
time.sleep(0.01)
sticks = [stick_1, stick_2, stick_3, stick_4]
# ------------------ DELETE PARTS ------------------
for i in Body_parts:
canvas.delete(i)
for j in sticks:
canvas.delete(j)
canvas.delete(text)
# ------------------ FLIP AND SHOW IMAGE ------------------
image = cv2.flip(image, 1)
cv2.imshow(' ', image)
# ------------------ CLOSE ------------------
if cv2.waitKey(10) & 0xFF == ord('q'):
break
print("Your score is : ",text)
canvas.mainloop()
cap.release()
cv2.destroyAllWindows()