-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
228 lines (200 loc) · 8.93 KB
/
draw.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import numpy as np
import cv2
from collections import deque
# Define the upper and lower boundaries for a color to be considered "Blue"
blueLower = np.array([100, 60, 60])
blueUpper = np.array([140, 255, 255])
# Define a 5x5 kernel for erosion and dilation
kernel = np.ones((5, 5), np.uint8)
# Setup deques to store separate colors in separate arrays
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]
bindex = 0
gindex = 0
rindex = 0
yindex = 0
colors = [(0, 0, 0), (0, 255, 0), (0, 0, 255), (255, 0, 0)]
colorIndex = 0
# Setup the Paint interface
paintWindow = np.zeros((471,636,3)) + 255
paintWindow = cv2.rectangle(paintWindow, (40,1), (140,65), (0,0,0), 2)
paintWindow = cv2.rectangle(paintWindow, (160,1), (255,65), colors[0], -1)
paintWindow = cv2.rectangle(paintWindow, (275,1), (370,65), colors[1], -1)
paintWindow = cv2.rectangle(paintWindow, (390,1), (485,65), colors[2], -1)
paintWindow = cv2.rectangle(paintWindow, (505,1), (600,65), colors[3], -1)
cv2.putText(paintWindow, "CLEAR ALL", (49, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "BLACK", (185, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "GREEN", (298, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "RED", (420, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "BLUE", (520, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2, cv2.LINE_AA)
cv2.namedWindow('Paint', cv2.WINDOW_AUTOSIZE)
# Load the video
camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
letterToDraw = 97
correct = False
incorrect = False
# Keep looping
while True:
# Grab the current paintWindow
(grabbed, frame) = camera.read()
frame = cv2.flip(frame, 1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Add the coloring options to the frame
frame = cv2.rectangle(frame, (40,1), (140,65), (122,122,122), -1)
frame = cv2.rectangle(frame, (160,1), (255,65), colors[0], -1)
frame = cv2.rectangle(frame, (275,1), (370,65), colors[1], -1)
frame = cv2.rectangle(frame, (390,1), (485,65), colors[2], -1)
frame = cv2.rectangle(frame, (505,1), (600,65), colors[3], -1)
cv2.putText(frame, "CLEAR ALL", (49, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "BLACK", (185, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "GREEN", (298, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "RED", (420, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "BLUE", (520, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2, cv2.LINE_AA)
if correct:
cv2.putText(frame, "hello", (70, 320), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 10, (0,255,0), 20, cv2.LINE_AA)
elif incorrect:
cv2.putText(frame, "hello", (70, 320), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 10, (0,0,255), 20, cv2.LINE_AA)
elif not correct and not incorrect:
cv2.putText(frame, "hello", (70, 320), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 10, (150,150,150), 20, cv2.LINE_AA)
# for x in range(0, 3):
# cv2.putText(frame, chr(letterToDraw), (90+(x*170), 320), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 10, (150,150,150), 20, cv2.LINE_AA)
# Check to see if we have reached the end of the video
if not grabbed:
break
# Determine which pixels fall within the blue boundaries and then blur the binary image
blueMask = cv2.inRange(hsv, blueLower, blueUpper)
blueMask = cv2.erode(blueMask, kernel, iterations=2)
blueMask = cv2.morphologyEx(blueMask, cv2.MORPH_OPEN, kernel)
blueMask = cv2.dilate(blueMask, kernel, iterations=1)
# Find contours in the image
(_, cnts, _) = cv2.findContours(blueMask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
center = None
# Check to see if any contours were found
if len(cnts) > 0:
# Sort the contours and find the largest one -- we
# will assume this contour correspondes to the area of the bottle cap
cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
# Get the radius of the enclosing circle around the found contour
((x, y), radius) = cv2.minEnclosingCircle(cnt)
# Draw the circle around the contour
cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
# Get the moments to calculate the center of the contour (in this case Circle)
M = cv2.moments(cnt)
center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00']))
if center[1] <= 65:
if 40 <= center[0] <= 140: # Clear All
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]
bindex = 0
gindex = 0
rindex = 0
yindex = 0
paintWindow[67:,:,:] = 255
elif 160 <= center[0] <= 255:
colorIndex = 0 # Blue
elif 275 <= center[0] <= 370:
colorIndex = 1 # Green
elif 390 <= center[0] <= 485:
colorIndex = 2 # Red
elif 505 <= center[0] <= 600:
colorIndex = 3 # Yellow
else :
if colorIndex == 0:
bpoints[bindex].appendleft(center)
elif colorIndex == 1:
gpoints[gindex].appendleft(center)
elif colorIndex == 2:
rpoints[rindex].appendleft(center)
elif colorIndex == 3:
ypoints[yindex].appendleft(center)
# Append the next deque when no contours are detected (i.e., bottle cap reversed)
else:
bpoints.append(deque(maxlen=512))
bindex += 1
gpoints.append(deque(maxlen=512))
gindex += 1
rpoints.append(deque(maxlen=512))
rindex += 1
ypoints.append(deque(maxlen=512))
yindex += 1
# Draw lines of all the colors (Blue, Green, Red and Yellow)
points = [bpoints, gpoints, rpoints, ypoints]
for i in range(len(points)):
for j in range(len(points[i])):
for k in range(1, len(points[i][j])):
if points[i][j][k - 1] is None or points[i][j][k] is None:
continue
cv2.line(frame, points[i][j][k - 1], points[i][j][k], colors[i], 10)
cv2.line(paintWindow, points[i][j][k - 1], points[i][j][k], colors[i], 10)
# Show the frame and the paintWindow image
cv2.imshow("Tracking", frame)
cv2.imshow("Paint", paintWindow)
k = cv2.waitKey(1)
if k == ord("z"):
crop_img = paintWindow[70:480, 0:640] # Crop from {x, y, w, h } => {0, 0, 300, 400}
rsz_img = cv2.resize(crop_img, (1218, 768))
cv2.imwrite("test_" + chr(letterToDraw) + ".png", rsz_img)
# cv2.namedWindow('test', cv2.WINDOW_AUTOSIZE)
correctIMG = cv2.imread("C:\dev\hth\HackTheHammer2019\check.png")
#cv2.putText(frame, "hello", (70, 320), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 10, (0,255,0), 20, cv2.LINE_AA)
correct = True
# cv2.imshow("Paint", correctIMG)
# if letterToDraw == 99:
# break
# else:
# letterToDraw+=1
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]
bindex = 0
gindex = 0
rindex = 0
yindex = 0
paintWindow[67:,:,:] = 255
elif k == ord("x"):
crop_img = paintWindow[70:480, 0:640] # Crop from {x, y, w, h } => {0, 0, 300, 400}
rsz_img = cv2.resize(crop_img, (1218, 768))
cv2.imwrite("test_" + chr(letterToDraw) + ".png", rsz_img)
# cv2.namedWindow('test', cv2.WINDOW_AUTOSIZE)
incorrectIMG = cv2.imread("C:\dev\hth\HackTheHammer2019\\x.png")
#cv2.putText(frame, "hello", (70, 320), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 10, (0,255,0), 20, cv2.LINE_AA)
incorrect = True
# cv2.imshow("Paint", incorrectIMG)
if letterToDraw == 99:
break
else:
letterToDraw+=1
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]
bindex = 0
gindex = 0
rindex = 0
yindex = 0
paintWindow[67:,:,:] = 255
elif k == ord(" "):
correct = False
incorrect = False
elif k == ord("w"):
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]
bindex = 0
gindex = 0
rindex = 0
yindex = 0
paintWindow[67:,:,:] = 255
# If the 'q' key is pressed, stop the loop
elif k == ord("q"):
break
# Cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()