-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathog pygame hand tracking.py
77 lines (55 loc) · 1.84 KB
/
og pygame hand tracking.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
import cv2 as c
import pygame as py
import mediapipe as mp
import time, sys
width = 1200
height = 400
py.init()
width, height = 1200, 600
screen = py.display.set_mode((width, height))
clock = py.time.Clock()
fps = 60
# ======================================================
cap = c.VideoCapture(1)
ctime = 0
ptime = 0
cap.set(3, 2400)
cap.set(4, 1200)
mpHands = mp.solutions.hands
mpDraw = mp.solutions.drawing_utils
hands = mpHands.Hands(max_num_hands = 1)
# =======================================================
quit = False
while not quit:
screen.fill((100, 100, 100))
for event in py.event.get():
if event.type == py.QUIT:
quit = True
box = py.Rect(200, 200, 50, 50)
# =================================================================================
success, img = cap.read()
img = c.flip(img, 1)
imgRGB = c.cvtColor(img, c.COLOR_BGR2RGB)
result = hands.process(imgRGB)
if result.multi_hand_landmarks:
for handLms in result.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
cx, cy = int(lm.x * width), int(lm.y * height)
py.draw.ellipse(screen, 'green', (cx, cy, 20, 20))
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
# ctime = time.time()
# fps = 1/(ctime-ptime)
# ptime = ctime
# c.putText(img, str(int(fps)), (10, 70), c.FONT_HERSHEY_PLAIN, 3, (200, 0, 0), 3)
c.imshow("Image", img)
if c.waitKey(1) == ord('q'):
quit = True
# ============================================================================================
py.draw.rect(screen, 'red', box)
clock.tick(fps)
py.display.set_caption(str(clock))
py.display.update()
cap.release()
c.destroyAllWindows()
py.quit()
sys.exit()