-
Notifications
You must be signed in to change notification settings - Fork 0
/
end.py
121 lines (121 loc) · 4.11 KB
/
end.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import cvzone
from cvzone.HandTrackingModule import HandDetector
import cv2
import os
import numpy as np
# Parameters
width, height = 1280, 720
gestureThreshold = 400
folderPath = "Presentation"
pTime = 0
cTime = 0
# Camera Setup
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FPS,60)
fpsReader = cvzone.FPS()
cap.set(3, width)
cap.set(4, height)
# windowWidth = cap.shape[1]
# windowHeight = cap.shape[0]
# print(windowWidth)
# print(windowHeight)
# Hand Detector
detectorHand = HandDetector(detectionCon=0.8, maxHands=1)
# Variables
imgList = []
delay = 10
buttonPressed = False
counter = 0
drawMode = False
imgNumber = 0
delayCounter = 0
annotations = [[]]
annotationNumber = -1
annotationStart = False
hs, ws = int(120 * 1), int(213 * 1) # width and height of small image
# Get list of presentation images
pathImages = sorted(os.listdir(folderPath), key=len)
print(pathImages)
while True:
# Get image frame
success, img = cap.read()
img = cv2.flip(img, 1)
pathFullImage = os.path.join(folderPath, pathImages[imgNumber])
imgCurrent = cv2.imread(pathFullImage)
# Find the hand and its landmarks
hands, img = detectorHand.findHands(img) # with draw
# Draw Gesture Threshold line
cv2.line(img, (0, gestureThreshold), (width, gestureThreshold), (0, 255, 0), 10)
if hands and buttonPressed is False: # If hand is detected
hand = hands[0]
cx, cy = hand["center"]
lmList = hand["lmList"] # List of 21 Landmark points
fingers = detectorHand.fingersUp(hand) # List of which fingers are up
# Constrain values for easier drawing
xVal = int(np.interp(lmList[8][0], [width // 2, width], [0, width]))
yVal = int(np.interp(lmList[8][1], [150, height - 150], [0, height]))
indexFinger = xVal, yVal
if cy <= gestureThreshold: # If hand is at the height of the face
if fingers == [1, 0, 0, 0, 0]:
print("Left")
buttonPressed = True
if imgNumber > 0:
imgNumber -= 1
annotations = [[]]
annotationNumber = -1
annotationStart = False
if fingers == [0, 0, 0, 0, 1]:
print("Right")
buttonPressed = True
if imgNumber < len(pathImages) - 1:
imgNumber += 1
annotations = [[]]
annotationNumber = -1
annotationStart = False
if fingers == [0, 1, 1, 0, 0]:
cv2.circle(imgCurrent, indexFinger, 12, (0, 0, 255), cv2.FILLED)
if fingers == [0, 1, 0, 0, 1]:
break
if fingers == [0, 1, 0, 0, 0]:
if annotationStart is False:
annotationStart = True
annotationNumber += 1
annotations.append([])
print(annotationNumber)
annotations[annotationNumber].append(indexFinger)
cv2.circle(imgCurrent, indexFinger, 12, (0, 0, 255), cv2.FILLED)
else:
annotationStart = False
if fingers == [0, 1, 1, 1, 0]:
if annotations:
annotations.pop(-1)
annotationNumber -= 1
buttonPressed = True
else:
annotationStart = False
if buttonPressed:
counter += 1
if counter > delay:
counter = 0
buttonPressed = False
for i, annotation in enumerate(annotations):
for j in range(len(annotation)):
if j != 0:
cv2.line(imgCurrent, annotation[j - 1], annotation[j], (0, 0, 200), 12)
imgSmall = cv2.resize(img, (ws, hs))
h, w, _ = imgCurrent.shape
h1 , w1, l1 = img.shape
# print(h1,w1)
imgCurrent[0:hs, w - ws: w] = imgSmall
# cTime = time.time()
# fps = 1 / (cTime - pTime)
# pTime = cTime
# cv2.putText(imgCurrent, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
fps , imgCurrent = fpsReader.update(imgCurrent)
cv2.imshow("Slides", imgCurrent)
# cv2.imshow("Image", img)
key = cv2.waitKey(1)
# if fingers ==[0,0,0,0,0]:
# break
if key == ord('q'):
break