-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deploy.py
160 lines (120 loc) · 5.33 KB
/
Deploy.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from turtle import home
import cv2
import os
import numpy as np
from PIL import Image
import HandTracking_GestureRecognition_Module as hgm
colorsPath = "NavBar/Colors"
homepagePath = "NavBar/Homepage"
sizesPath = "NavBar/Sizes"
imListColors = os.listdir(colorsPath)
imListHomepage = os.listdir(homepagePath)
imListSizes = os.listdir(sizesPath)
colors = []
homepage = []
sizes = []
for imPath in imListColors:
image = cv2.imread(f'{colorsPath}/{imPath}')
colors.append(image)
for imPath in imListHomepage:
image = cv2.imread(f'{homepagePath}/{imPath}')
homepage.append(image)
for imPath in imListSizes:
image = cv2.imread(f'{sizesPath}/{imPath}')
sizes.append(image)
def drawOnFeed(frame, canvas):
gray = cv2.cvtColor(canvas, cv2.COLOR_BGR2GRAY)
_, ImgInv = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV)
ImgInv = cv2.cvtColor(ImgInv, cv2.COLOR_GRAY2BGR)
frame = cv2.bitwise_and(frame, ImgInv)
frame = cv2.bitwise_or(frame, canvas)
return frame
def main():
width, height = 1280, 720
brushColor = [(0, 0, 255), (0, 255, 0), (255, 0, 80)]
brushSize = [10, 20, 30]
eraserSize = [25, 45, 60]
currNavBar, currNavBarid, currColor, currBrushsize, currEraserSize = homepage[0], 0, brushColor[2], brushSize[1], eraserSize[1]
canvas = np.zeros((height, width, 3), dtype = 'uint8')
cap = cv2.VideoCapture(0)
cap.set(3, width)
cap.set(4, height)
xp, yp = 0, 0
detector = hgm.HandDetector()
while True:
success, frame = cap.read()
frame = cv2.flip(frame, 1)
frame = detector.FindHands(frame, True)
lm_list = detector.FindPositions(frame, 0)
if len(lm_list):
fingers = detector.FindGesture()
xi, yi = lm_list[8][1:]
xm, ym = lm_list[12][1:]
# index finger
if fingers[0] == 1 and fingers[1] == 0 and fingers[2] == 0 and fingers[3] == 0:
if xp == 0 and yp == 0:
xp, yp = xi, yi
cv2.line(canvas, (xp, yp), (xi, yi), currColor, currBrushsize)
xp, yp = xi, yi
# index + middle fingers
elif fingers[0] == 1 and fingers[1] == 1 and fingers[2] == 0 and fingers[3] == 0:
xp, yp = 0, 0
if currNavBarid == 0:
if ym < 100:
if xm > 100 and xm < 280:
currNavBar, currNavBarid = colors[0], 1
elif xm > 400 and xm < 620:
currNavBar, currNavBarid = sizes[1], 2
elif xm > 780 and xm < 940:
currNavBar, currNavBarid = sizes[0], 3
elif currNavBarid == 1:
if ym < 100:
if xm > 100 and xm < 280:
currNavBar, currColor = colors[0], brushColor[2]
elif xm > 400 and xm < 620:
currNavBar, currColor = colors[2], brushColor[0]
elif xm > 780 and xm < 940:
currNavBar, currColor = colors[1], brushColor[1]
elif xm > 1080 and xm < 1200:
currNavBar, currNavBarid = homepage[0], 0
elif currNavBarid == 2:
if ym < 100:
if xm > 100 and xm < 280:
currNavBar, currBrushsize = sizes[2], brushSize[0]
elif xm > 400 and xm < 620:
currNavBar, currBrushsize = sizes[1], brushSize[1]
elif xm > 780 and xm < 940:
currNavBar, currBrushsize = sizes[0], brushSize[2]
elif xm > 1080 and xm < 1200:
currNavBar, currNavBarid = homepage[0], 0
elif currNavBarid == 3:
if ym < 100:
if xm > 100 and xm < 280:
currNavBar, currEraserSize = sizes[2], eraserSize[0]
elif xm > 400 and xm < 620:
currNavBar, currEraserSize = sizes[1], eraserSize[1]
elif xm > 780 and xm < 940:
currNavBar, currEraserSize = sizes[0], eraserSize[2]
elif xm > 1080 and xm < 1200:
currNavBar, currNavBarid = homepage[0], 0
# index + middle + ring fingers
elif fingers[0] == 1 and fingers[1] == 1 and fingers[2] == 1 and fingers[3] == 0:
xp, yp = 0, 0
# img = Image.fromarray(canvas)
# img.save("drawing.png")
# index + middle + ring + pbrushColory fingers
elif fingers[0] == 1 and fingers[1] == 1 and fingers[2] == 1 and fingers[3] == 1:
cv2.circle(frame, (xm, ym), currEraserSize, (0, 0, 0), -1)
cv2.circle(canvas, (xm, ym), currEraserSize, (0, 0, 0), -1)
xp, yp = 0, 0
else:
xp, yp = 0, 0
frame = drawOnFeed(frame, canvas)
frame[0:100, 0:1280] = currNavBar
cv2.imshow('Live', frame)
if cv2.waitKey(20) & 0xFF == ord('x'):
break
cv2.destroyAllWindows()
cap.release()
if __name__ == "__main__":
main()