-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualDraw.py
More file actions
127 lines (105 loc) · 3.72 KB
/
VirtualDraw.py
File metadata and controls
127 lines (105 loc) · 3.72 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
import cv2 as cv
import os
from HandTrackingModule import handDetector
import numpy as np
width, height = 1280,720
cap = cv.VideoCapture(0)
cap.set(3, width)
cap.set(4, height)
# get the list of presentation images
# print(pathImages)
# variable
imgNumber = 0
hs, ws = int(120 * 1), int(213 * 1)
gestureThreshold = 300
buttonPressed = False
buttonCounter = 0
buttonDelay = 10
annotations = [[]]
annotationNumber = 0
annotationStart = False
# HandDetector
detector = handDetector(detectionCon=0.8, maxHands=1)
folderPath = "icon"
while True:
# import images
success, img = cap.read()
img = cv.flip(img, 1)
pathFullImage = os.path.join(folderPath, pathImages[imgNumber])
imgCurrent = cv.imread(pathFullImage)
imgCurrent = cv.resize(imgCurrent, (1280, 720))
hands, img = detector.findHands(img)
cv.line(img, (0, gestureThreshold), (width, gestureThreshold), (0, 255, 0), 10)
if hands and buttonPressed is False:
hand = hands[0]
fingers = detector.fingersUp(hand)
cx, cy = hand['center']
lmList = hand['lmList']
# Contrain Values for easier Drawing
xVal = int(np.interp(lmList[8][0], [width // 2, w], [0, width]))
yVal = int(np.interp(lmList[8][1], [175, height - 175], [0, height]))
indexFinger = xVal, yVal
if cy <= gestureThreshold:
annotationStart = False
# gesture - 1 left
if fingers == [1, 0, 0, 0, 0]:
print("Left")
annotationStart = False
if imgNumber > 0:
buttonPressed = True
annotations = [[]]
annotationNumber = 0
imgNumber -= 1
# gesture - 2 right
if fingers == [0, 0, 0, 0, 1]:
print("Right")
annotationStart = False
if imgNumber < len(pathImages) - 1:
buttonPressed = True
annotations = [[]]
annotationNumber = 0
imgNumber += 1
# gesture - 3 Show Pointer
if fingers == [0, 1, 1, 0, 0]:
cv.circle(imgCurrent, indexFinger, 12, (0, 0, 255), cv.FILLED)
annotationStart = False
# gesture - 4 Draw Pointer
if fingers == [0, 1, 0, 0, 0]:
if annotationStart is False:
annotationStart = True
annotationNumber += 1
annotations.append([])
cv.circle(imgCurrent, indexFinger, 12, (0, 0, 255), cv.FILLED)
annotations[annotationNumber].append(indexFinger)
else:
annotationStart = False
# Gesture 5 - Erase
if fingers == [0, 1, 1, 1, 0]:
if annotations:
if annotationNumber >= 0:
annotations.pop(-1)
annotationNumber -= 1
buttonPressed = True
else:
annotationStart = False
# Button Pressed Iterations
if buttonPressed:
buttonCounter += 1
if buttonCounter > buttonDelay:
buttonCounter = 0
buttonPressed = False
for i in range(len(annotations)):
for j in range(len(annotations[i])):
if j != 0:
cv.line(imgCurrent, annotations[i][j - 1], annotations[i][j], (0, 0, 200), 12)
# Adding webcam image on slides
imgSmall = cv.resize(img, (ws, hs))
h, w, _ = imgCurrent.shape
imgCurrent[0:hs, w - ws:w] = imgSmall
cv.imshow("Image", img)
cv.imshow("Slides", imgCurrent)
key = cv.waitKey(1)
if key == ord('q'):
break
cap.release()
cv.destroyAllWindows()