-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHand_Detection.py
More file actions
113 lines (73 loc) · 2.63 KB
/
Hand_Detection.py
File metadata and controls
113 lines (73 loc) · 2.63 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
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
import cv2 as cv
import mediapipe as mp
import time
def handtracking():
capture = cv.VideoCapture(0)
mphands = mp.solutions.hands
hands = mphands.Hands()
mpdraw = mp.solutions.drawing_utils
preTime = 0
curTime = 0
while True:
_, frame = capture.read()
#Convert frame into RGB and pass into hands.process()
frameRGB = cv.cvtColor(frame,cv.COLOR_BGR2RGB)
results = hands.process(frameRGB)
#Check if any hand discovered
if results.multi_hand_landmarks:
#Extract the hands
for handlms in results.multi_hand_landmarks:
# getting id, landmarkInfo(x,y) from handlms
for id, lm in enumerate(handlms.landmark):
h, w, c = frame.shape
# location of the landmarks(x,y) per id(point)
cx ,cy, = int(lm.x*w), int(lm.y*h)
# draw indivitual landmarks
# tip of the pointer finger
if id == 8:
cv.circle(frame,(cx,cy),5,(156,118,237),10)
# Drawing the landmarks and connections(lines)
mpdraw.draw_landmarks(frame,handlms, mphands.HAND_CONNECTIONS)
# Frame rate
frame = cv.flip(frame, 1)
curTime = time.time()
fps = 1 / (curTime - preTime)
preTime = curTime
cv.putText(frame,str(int(fps)),(10,70),cv.FONT_HERSHEY_PLAIN,3,(255,0,255),2)
cv.imshow("image",frame)
if cv.waitKey(1) == ord('q'):
break
capture.release()
cv.destroyAllWindows()
# capture = cv.VideoCapture(0)
#
# mpHands = mp.solutions.hands
# hands = mpHands.Hands()
# mpDraw = mp.solutions.drawing_utils
#
# pTime = 0
# cTime = 0
#
# while True:
# success, img = capture.read()
# imgRGB = cv.cvtColor(img,cv.COLOR_BGR2RGB)
# results = hands.process(imgRGB)
# # print(results.multi_hand_landmarks)
#
# if results.multi_hand_landmarks:
# for handLms in results.multi_hand_landmarks:
# mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
#
# cTime = time.time()
# fps = 1 / (cTime - pTime)
# pTime = cTime
#
# cv.putText(img, str(int(fps)), (10, 70), cv.FONT_HERSHEY_PLAIN, 3,(255, 0, 255), 3)
# img = cv.flip(img,1)
# cv.imshow("Image", img)
#
# if cv.waitKey(45) == ord('q'):
# break
#
# capture.release()
# cv.destroyAllWindows()