-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpaint.py
304 lines (249 loc) · 10.7 KB
/
paint.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
Painting With Identified Colored Object : Step 2
"""
"""
This project tracks the object whose color is defined from webcam.
First of all, the color values of the object to be tracked are set.
Second, the drawing is made by following this object.
Python use 3 channels BGR colors that equals RGB.
HSV coloring is used to set the color of the object.
Setting up with HSV is easier than others.
"""
# Libraries
import cv2
import numpy as np
# 2) Painting with identified object's color
# Load saved file that contain hsv ranges ('hsvVal.npy')
hsvVal = np.load('hsvVal.npy')
# Show shortcuts information
print("\n 2) Painting with identified object's color \n")
print("'hsvVal.npy' loaded")
print("Painting ready\n")
print("#####################################")
print("# Press 'q' to exit the program #")
print("#####################################")
print("\n")
# Get minimum and maximum ranges from file
minRange = hsvVal[0]
maxRange = hsvVal[1]
# Initializing the webcam
cap = cv2.VideoCapture(1) # 0, 1, 2, ... webcam port number
cap.set(3,720) # width of camera
cap.set(4,480) # height of camera
# BGR colors to painting
colorBlue = (255, 134, 58)
colorYellow = (11, 190, 255)
colorPink = (110, 0, 255)
colorGreen = (0 ,255, 36)
colorPurple = (236, 56, 131)
colorScreenCleaner = (7, 86, 251)
colorEraser = (7, 86, 251)
# Thickness of colors's boxes
thicknessBlue = 6
thicknessYellow = 2
thicknessPink = 2
thicknessGreen = 2
thicknessPurple = 2
# Thickness of marker's thicknesses boxes
thicknessSmall = 4
thicknessMedium = 1
thicknessLarge = 1
# Thickness of eraser
thicknessEraser = 1
# Default values for marker
color = colorBlue # Set default marker's color is blue
thickness = 3 # Set default marker's thickness is small
# Create x, y top coordinates of marker that identified first
# Top and middle coordinates of tracking object area is tip of marker
# like a pencil tip to painting
x1 = None
y1 = None
# Create canvas window to see painting without webcam
canvas = None
# Get width and height to put painting tools
ret, frame = cap.read()
h, w, c = frame.shape
# Showing webcam and painting on it in a loop
while True:
# Read from webcam frame by frame.
ret, frame = cap.read()
if not ret:
break
# Flip the frame horizontally (That makes mirror effect).
frame = cv2.flip(frame, 1)
# Convert to BGR image to HSV image.
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# If canvas empty show black screen
if canvas is None:
canvas = np.zeros(frame.shape, dtype=np.uint8)
# -------------------------- <top-section> --------------------------
# Put screen cleaner box
# Uncomment if want to make visible rectange of cleaner
# cv2.rectangle(frame, (0,0), (100,50), colorScreenCleaner, 0)
cv2.putText(frame, 'Clear', (15,35), 5, 1, colorScreenCleaner, 1,
cv2.LINE_AA)
# Put eraser
# Uncomment if want to make visible rectange of eraser
# cv2.rectangle(frame, (100,0), (200,50), colorEraser, 0)
cv2.putText(frame, 'Eraser', (110,35), 5, 1, colorEraser, thicknessEraser,
cv2.LINE_AA)
# Put colors boxes
cv2.rectangle(frame, (w-3,5), (w-53,55), colorYellow, thicknessYellow)
cv2.rectangle(frame, (w-58,5),(w-108,55), colorPink, thicknessPink)
cv2.rectangle(frame, (w-113,5), (w-163,55), colorGreen, thicknessGreen)
cv2.rectangle(frame, (w-168,5), (w-218,55), colorBlue, thicknessBlue)
cv2.rectangle(frame, (w-223,5), (w-273,55), colorPurple, thicknessPurple)
# Put thickness boxes
cv2.rectangle(frame, (w-352,5), (w-402,55), (0,0,0), thicknessLarge)
cv2.circle(frame, (w-377,30), 11, (0,0,0), -1)
cv2.rectangle(frame, (w-402,5), (w-452,55), (0,0,0), thicknessMedium)
cv2.circle(frame, (w-427,30), 7, (0,0,0), -1)
cv2.rectangle(frame, (w-452,5), (w-502,55), (0,0,0), thicknessSmall)
cv2.circle(frame, (w-477,30), 3, (0,0,0), -1)
# ------------------------- </top-section> --------------------------
# Create mask with adjusted ranges
mask = cv2.inRange(hsv, minRange, maxRange)
# Perform filter operations to get rid of the noise
mask = cv2.erode(mask, None, iterations=1)
mask = cv2.dilate(mask, None, iterations=2)
mask = cv2.medianBlur(mask, 13)
# Find contours
contours, hierarchy = cv2.findContours(mask,
cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# Sort contours
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
for c in contours:
# Assign contour area to area
area = cv2.contourArea(c)
# Check contour area size
# 1000 is enough used object, can be changed to draw more smoothly
if area > 1000:
# Get x, y coordinate and width, height rate of contour
# y2 gives y coordinate of marker's tip
x, y2, wc, hc = cv2.boundingRect(c)
# x coordinate of marker's tip
x2 = x + wc//2
# Show area lines
cv2.rectangle(frame, (x,y2), (x+wc,y2+hc), color, 2)
# If object trackable
if x1 is not None:
# If marker's tip coordinates on yellow box set color yellow
if w-53 < x2 < w-3 and 5 < y2 < 55:
color = colorYellow
thicknessBlue = 2
thicknessYellow = 6
thicknessPink = 2
thicknessGreen = 2
thicknessPurple = 2
thicknessEraser = 1
# If marker's tip coordinates on pink box set color pink
if w-108 < x2 < w-58 and 5 < y2 < 55:
color = colorPink
thicknessBlue = 2
thicknessYellow = 2
thicknessPink = 6
thicknessGreen = 2
thicknessPurple = 2
thicknessEraser = 1
# If marker's tip coordinates on green box set color green
if w-163 < x2 < w-113 and 5 < y2 < 55:
color = colorGreen
thicknessBlue = 2
thicknessYellow = 2
thicknessPink = 2
thicknessGreen = 6
thicknessPurple = 2
thicknessEraser = 1
# If marker's tip coordinates on blue box set color blue
if w-218 < x2 < w-168 and 5 < y2 < 55:
color = colorBlue
thicknessBlue = 6
thicknessYellow = 2
thicknessPink = 2
thicknessGreen = 2
thicknessPurple = 2
thicknessEraser = 1
# If marker's tip coordinates on purple box set color blue
if w-268 < x2 < w-218 and 5 < y2 < 55:
color = colorPurple
thicknessBlue = 2
thicknessYellow = 2
thicknessPink = 2
thicknessGreen = 2
thicknessPurple = 6
thicknessEraser = 1
# If marker's tip coordinates on eraser
if 100 < x2 < 200 and 5 < y2 < 55:
# Painting with black means delete
# Because canvas created with np.zeros
color = (0,0,0)
thicknessBlue = 2
thicknessYellow = 2
thicknessPink = 2
thicknessGreen = 2
thicknessEraser = 2
# If marker's tip coordinates on large thickness box
if w-402 < x2 < w-352 and 5 < y2 < 55:
thickness = 11
thicknessSmall = 1
thicknessMedium = 1
thicknessLarge = 4
# If marker's tip coordinates on medium thickness box
if w-452 < x2 < w-402 and 5 < y2 < 55:
thickness = 7
thicknessSmall = 1
thicknessMedium = 4
thicknessLarge = 1
# If marker's tip coordinates on small thickness box
if w-502 < x2 < w-452 and 5 < y2 < 55:
thickness = 3
thicknessSmall = 4
thicknessMedium = 1
thicknessLarge = 1
# If marker's tip coordinates on clear box
if 0 < x2 < 100 and 5 < y2 < 55:
cv2.putText(frame, 'Clear', (15,35), 5, 1,
colorScreenCleaner, 2, cv2.LINE_AA)
# Clear canvas
canvas = np.zeros(frame.shape, dtype=np.uint8)
# Show canvas if marker's tip not on top section area
if y2 < 65 or y1 < 65:
canvas = canvas
# Otherwise, assign new values
else:
# If eraser selected, make thickness larger
if thicknessEraser == 2:
canvas = cv2.line(canvas, (x1,y1), (x2,y2), color,
thickness+15)
# Otherwise, make thickness normal values
else:
canvas = cv2.line(canvas, (x1,y1), (x2,y2), color,
thickness)
# Set marker's tip that set options
cv2.circle(frame, (x2,y2), thickness, color, 2)
# Set painting coordinates
x1 = x2
y1 = y2
# If object not trackable
else:
# Set painting coordinates none to don't paint
x1 = None
y1 = None
# Just for smooth drawing (Optional)
canvasGray = cv2.cvtColor(canvas, cv2.COLOR_BGR2GRAY)
_, th = cv2.threshold(canvasGray, 10, 255, cv2.THRESH_BINARY)
thInv = cv2.bitwise_not(th)
# Add mask to frame to show on webcam frame
frame = cv2.bitwise_and(frame, frame, mask=thInv)
frame = cv2.add(frame, canvas)
# Stack canvas and frame to show same window
stacked = np.hstack((canvas, frame))
# Show stacked frames
cv2.imshow("Painting", cv2.resize(stacked, None, fx=.8, fy=.8))
# If 'q' pressed then exit the program
k = cv2.waitKey(1)
if (k == ord('q') or k == ord('Q')):
break
# Release the camera and destroy all windows
cap.release()
cv2.destroyAllWindows()