forked from ajinkya110001/ERC-HandDetectionAssignment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (41 loc) · 1.74 KB
/
main.py
File metadata and controls
58 lines (41 loc) · 1.74 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
import cv2
import mediapipe as mp
import numpy as np
from google.protobuf.json_format import MessageToDict
cap=cv2.VideoCapture(0)
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
with mp_hands.Hands() as hands:
while True:
success, frame=cap.read()
image = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
image.flags.writeable = False
image = cv2.flip(image,1)
results = hands.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
print(results)
if results.multi_hand_landmarks:
for num, hand in enumerate(results.multi_hand_landmarks):
mp_drawing.draw_landmarks(image, hand, mp_hands.HAND_CONNECTIONS)
if len(results.multi_handedness) == 2:
cv2.putText(image, 'Both Hands', (250, 50),
cv2.FONT_HERSHEY_COMPLEX, 0.9,
(255, 255, 0), 2)
else:
for i in results.multi_handedness:
label = MessageToDict(i)[
'classification'][0]['label']
if label == 'Left':
cv2.putText(image, label+' Hand', (20, 50),
cv2.FONT_HERSHEY_COMPLEX, 0.9,
(0, 255, 0), 2)
if label == 'Right':
cv2.putText(image, label+' Hand', (460, 50),
cv2.FONT_HERSHEY_COMPLEX,
0.9, (0, 255, 255), 2)
cv2.imshow("Hand Detection",image)
if cv2.waitKey(1) & 0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows()