-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPresentationHandControl.py
106 lines (87 loc) · 2.93 KB
/
PresentationHandControl.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
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
import cv2
import time
import HandTrackingModule as htm
import KeyboardFunctions as kbf
import HandGestureDetection as hgt
import mediapipe
# Test the ports and returns a tuple with the available ports and the ones that are working.
dev_port = 0
working_ports = []
while True:
camera = cv2.VideoCapture(dev_port)
if not camera.isOpened():
print("Port %s is not working." % dev_port)
break
else:
is_reading, img = camera.read()
w, h = camera.get(3), camera.get(4)
if is_reading:
print("Port %s is working and reads images (%s x %s)" % (dev_port, h, w))
working_ports.append(dev_port)
else:
print("Port %s for camera (%s x %s) is present but does not read." % (dev_port, h, w))
dev_port += 1
##########
wCam, hCam = 640, 480
##########
cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)
pTime = 0
pointingGesture = []
prevAction = "None"
nRepeats = 0
detector = htm.HandDetector(detectionCon=0.8)
while True:
success, img = cap.read()
img = cv2.flip(img, 1)
img = detector.findHands(img)
lmList = detector.findPosition(img, draw=False)
pointingGesture = []
if len(lmList) != 0:
for hand in lmList:
pointingGesture.append(hgt.inPointingGesture(hand))
cTime = time.time()
fps = 1/(cTime - pTime)
pTime = cTime
cv2.putText(img, f'FPS: {int(fps)}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
.7, (255, 0, 0), 2)
cv2.putText(img, 'Press \'q\' to quit', (10, 460), cv2.FONT_HERSHEY_SIMPLEX,
.7, (255, 0, 0), 2)
if len(pointingGesture) != 0:
cv2.putText(img, f'Orientation: {pointingGesture[0][0]}', (10, 60), cv2.FONT_HERSHEY_SIMPLEX,
.7, (255, 0, 0), 2)
cv2.putText(img, f'Pointer Extended: {pointingGesture[0][1]}', (10, 90), cv2.FONT_HERSHEY_SIMPLEX,
.7, (255, 0, 0), 2)
cv2.putText(img, f'Thumb and Pointer Right Angle: {pointingGesture[0][2]}', (10, 120), cv2.FONT_HERSHEY_SIMPLEX,
.7, (255, 0, 0), 2)
cv2.putText(img, f'Middle, Ring, Pinky Closed: {pointingGesture[0][3]}', (10, 150), cv2.FONT_HERSHEY_SIMPLEX,
.7, (255, 0, 0), 2)
action = "None"
vote = 0
for idx, hand in enumerate(pointingGesture):
if hand == "Left":
vote -= 1
elif hand == "Right":
vote += 1
if vote > 0:
action = "Right"
elif vote < 0:
action = "Left"
else:
action = "None"
if action == prevAction and prevAction != "None":
nRepeats += 1
else:
prevAction = action
nRepeats = 0
if nRepeats == 2:
kbf.arrowKeyPress(action)
elif nRepeats % 6 == 0 and nRepeats != 0:
kbf.arrowKeyPress(action)
detector.drawHands(img, pointingGesture)
if cv2.waitKey(1) == ord('q'):
break
cv2.imshow("Img", img)
cv2.waitKey(1)
cv2.destroyAllWindows()