forked from regression1607/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHand.py
More file actions
47 lines (44 loc) · 1.45 KB
/
Hand.py
File metadata and controls
47 lines (44 loc) · 1.45 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
import cv2
import mediapipe as mp
mphands = mp.solutions.hands
hands = mphands.Hands()
mp_drawing = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
h, w, c = frame.shape
framergb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(framergb)
hand_landmarks = result.multi_hand_landmarks
if hand_landmarks:
for handLMs in hand_landmarks:
x_max = 0
y_max = 0
x_min = w
y_min = h
for lm in handLMs.landmark:
x, y = int(lm.x * w), int(lm.y * h)
if x > x_max:
x_max = x
if x < x_min:
x_min = x
if y > y_max:
y_max = y
if y < y_min:
y_min = y
diffx = x_max-x_min
diffy = y_max-y_min
#print(diffx, diffy)
x_min = int(x_min - 0.02*diffx)
x_max = int(x_max + 0.02*diffx)
y_max =int( y_max + 0.02*diffy)
y_min = int(y_min - 0.02*diffy)
#cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
img = frame[x_min:x_max,y_min:y_max]
mp_drawing.draw_landmarks(frame, handLMs, mphands.HAND_CONNECTIONS)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()