-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandLandMarkModule.py
More file actions
145 lines (113 loc) · 5.62 KB
/
HandLandMarkModule.py
File metadata and controls
145 lines (113 loc) · 5.62 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Coded by Shreyas Sharma
# DJI Tello VR Control 1/2 of code
from socket import AI_PASSIVE
import mediapipe as mp
import time
import cv2
class handLandmarkDetector:
def __init__(self, mode=False, maxHands=2, detectionConf=0.5, trackConf=0.5):
self.mode = mode
self.maxHands = maxHands
self.detectionConf = detectionConf
self.trackConf = trackConf
self.mp_hands = mp.solutions.hands
self.hands = self.mp_hands.Hands(self.mode, self.maxHands, self.detectionConf, self.trackConf)
self.mp_draw = mp.solutions.drawing_utils
def findHands(self, img, draw=True):
img = cv2.flip(img,1)
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
if self.results.multi_hand_landmarks:
# Draw hand to image
for hand_landmarks in self.results.multi_hand_landmarks:
if draw:
self.mp_draw.draw_landmarks(img, hand_landmarks, self.mp_hands.HAND_CONNECTIONS)
return img
def drawFingerPoint(self, img, drawLeft=True, drawRight=True, finger= 8): # finger id 8 is index finger
# get handedness
if self.results.multi_hand_landmarks:
for id_hnd, hnd in enumerate(self.results.multi_handedness):
hnd_name = hnd.classification[0].label
hand = self.results.multi_hand_landmarks[id_hnd]
h, w, c = img.shape
for id, lm in enumerate(hand.landmark):
# draw left finger
if drawLeft:
if id == finger and hnd_name =='Left':
ind_finger_l_x = int(lm.x * w)
ind_finger_l_y = int(lm.y * h)
cv2.circle(img, (int(ind_finger_l_x), int(ind_finger_l_y)), 25, (0,255,0), cv2.FILLED)
cv2.putText(img, hnd_name, (int(w*0.25),int(h*0.05)), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,255,0), 3) # draws left to img if left hand is detected
# draw right finger
if drawRight:
if id == finger and hnd_name =='Right':
ind_finger_r_x = int(lm.x * w)
ind_finger_r_y = int(lm.y * h)
cv2.circle(img, (int(ind_finger_r_x), int(ind_finger_r_y)), 25, (0,0,255), cv2.FILLED)
cv2.putText(img, hnd_name, (int(w*0.75),int(h*0.05)), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255), 3) # draws left to img if left hand is detected
try:
return [(ind_finger_l_x, ind_finger_l_y),(ind_finger_r_x, ind_finger_r_y)]
except:
pass
def in_circle(center_x, center_y, radius, coords):
x, y = coords
square_dist = (center_x - x) ** 2 + (center_y - y) ** 2
return square_dist <= radius ** 2
# def main():
# width = 1200
# height = 700
# fControl = True
# ptime = 0
# ctime = 0
# cap = cv2.VideoCapture(0)
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# counter = 0
# detector = handLandmarkDetector()
# while True:
# success, img = cap.read()
# img = detector.findHands(img)
# fingerLs = detector.drawFringerPoint(img)
# # print(counter)
# ctime = time.time()
# fps = 1/(ctime - ptime)
# ptime = ctime
# cv2.putText(img, str(int(fps)),(10,30), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,0,255), 2)
# cv2.circle(img, (int(width * 0.1), int(height*0.1)), 25, (0,255,0), 2)
# cv2.circle(img, (int(width * 0.9), int(height*0.1)), 25, (0,255,0), 2)
# try:
# if in_circle(int(width * 0.1), int(height*0.1), 25, fingerLs[0] ) and in_circle(int(width * 0.9), int(height*0.1), 25, fingerLs[1]):
# counter +=1
# if counter == 30:
# fControl = not fControl
# print('Control activated', fControl)
# else:
# counter = 0
# except:
# pass
# if fControl:
# cv2.putText(img, 'Control activated',(500,30), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,0), 3)
# # left joystick
# cv2.circle(img, (int(width * 0.3), int(height*0.5)), 125, (0,255,0), 7)
# # cv2.circle(img, (int(width * 0.3), int(height*0.5)), 25, (0,255,0), cv2.FILLED)
# # righ joystick
# cv2.circle(img, (int(width * 0.7), int(height*0.5)), 125, (0,255,0), 7)
# # cv2.circle(img, (int(width * 0.7), int(height*0.5)), 25, (0,255,0), cv2.FILLED)
# try:
# if in_circle(int(width * 0.3), int(height*0.5), 125, fingerLs[0]) and in_circle(int(width * 0.7), int(height*0.5), 125, fingerLs[1]):
# print('left idx F: ', fingerLs[0], 'right idx F: ', fingerLs[1])
# else:
# cv2.circle(img, (int(width * 0.3), int(height*0.5)), 25, (0,255,0), cv2.FILLED)
# cv2.circle(img, (int(width * 0.7), int(height*0.5)), 25, (0,255,0), cv2.FILLED)
# except:
# cv2.circle(img, (int(width * 0.3), int(height*0.5)), 25, (0,255,0), cv2.FILLED)
# cv2.circle(img, (int(width * 0.7), int(height*0.5)), 25, (0,255,0), cv2.FILLED)
# pass
# else:
# cv2.putText(img, 'Control deactivated',(500,30), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,0), 3)
# cv2.imshow('MediaPipe Hands', img)
# if cv2.waitKey(5) & 0xFF == 27:
# break
# cap.release()
# if __name__ == main():
# main()