Repository navigation
Expand file tree
/
Copy pathstep4.py
More file actions
79 lines (56 loc) · 2.15 KB
/
Copy pathstep4.py
File metadata and controls
79 lines (56 loc) · 2.15 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
import random
import numpy as np
import pygame
import cv2
from cvzone.HandTrackingModule import HandDetector
height, width = 480, 640
capture = cv2.VideoCapture(0)
capture.set(3, height)
capture.set(4, width)
def get_image_info():
image_name = 'ballon.png'
image = pygame.image.load(image_name).convert_alpha()
image_rect = image.get_rect()
# here we are declaring position of ballon (W, H)
image_rect.x = random.randint(0, width)
image_rect.y = height
return image_name, image, image_rect
def main():
window = pygame.display.set_mode((width, height))
hand_detector = HandDetector(detectionCon=0.25, maxHands=2)
image_name, img_ballon, rect_ballon = get_image_info()
speed = 5
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
success, image = capture.read()
image = cv2.flip(image, 1)
hands, image = hand_detector.findHands(image, flipType=False)
image = cv2.flip(image, 1)
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img_rgb = np.rot90(img_rgb)
frame = pygame.surfarray.make_surface(img_rgb).convert()
window.blit(frame, (0, 0))
# move ballon up
rect_ballon.y -= speed
# if ballon is out of screen, create new ballon
if rect_ballon.y < 0:
image_name, img_ballon, rect_ballon = get_image_info()
# if hand is touching a ballon, pop it
if hands:
for hand in hands:
# check if hand is touching the ballon
hand_bbox = hand['bbox']
rect_hand = pygame.Rect(hand_bbox[0], hand_bbox[1], hand_bbox[2], hand_bbox[3])
if rect_hand.colliderect(rect_ballon):
image_name, img_ballon, rect_ballon = get_image_info()
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img_rgb = np.rot90(img_rgb)
frame = pygame.surfarray.make_surface(img_rgb).convert()
window.blit(frame, (0, 0))
# show ballon
window.blit(img_ballon, rect_ballon)
pygame.display.update()
if __name__ == "__main__":
main()