-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
328 lines (266 loc) · 13.9 KB
/
main.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import cv2
import numpy as np
def add_subtitle(clip, text, height):
cv2.putText(clip, text, (0, height - 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 255), 2, cv2.LINE_AA)
# helper function to change what you do based on video seconds
def between(cap, lower: int, upper: int) -> bool:
return lower <= int(cap.get(cv2.CAP_PROP_POS_MSEC)) < upper
def main(input_video_file: str, output_video_file: str) -> None:
# OpenCV video objects to work with)
cap = cv2.VideoCapture(input_video_file)
fps = int(cap.get(5))
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# saving output video as .mp4
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_video_file, fourcc, fps,
(frame_width, frame_height))
# while loop where the real work happens
while cap.isOpened():
ret, frame = cap.read()
if ret:
if cv2.waitKey(28) & 0xFF == ord('q'):
break
if between(cap, 0, 1000):
add_subtitle(frame, "Original", frame_height)
elif between(cap, 1000, 2000):
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
add_subtitle(frame, "Grayscale", frame_height)
elif between(cap, 2000, 3000):
add_subtitle(frame, "Original", frame_height)
elif between(cap, 3000, 4000):
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
add_subtitle(frame, "Grayscale", frame_height)
elif between(cap, 4000, 6000):
frame = cv2.GaussianBlur(frame, (5, 5), 5)
add_subtitle(
frame, "Gaussian kernel size 5, makes blurry", frame_height)
elif between(cap, 6000, 8000):
frame = cv2.GaussianBlur(frame, (15, 15), 5)
add_subtitle(
frame, "Gaussian kernel size 15, makes more blurry", frame_height)
elif between(cap, 8000, 10000):
frame = cv2.bilateralFilter(frame, 9, 75, 75)
add_subtitle(
frame, "Bilateral size 9, blurs the image, preserves edges", frame_height)
elif between(cap, 10000, 12000):
frame = cv2.bilateralFilter(frame, 18, 75, 75)
add_subtitle(
frame, "Bilateral size 18, blurs the image more, preserves edges", frame_height)
elif between(cap, 12000, 13000):
add_subtitle(frame, "Original", frame_height)
elif between(cap, 13000, 15000):
frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_orange = np.array([0, 100, 100]) # H-10, 100, 100
upper_orange = np.array([19, 255, 255]) # H+10, 255, 255
frame = cv2.inRange(frame_hsv, lower_orange, upper_orange)
frame = np.stack([frame] * 3, axis=-1)
add_subtitle(frame, "Detect the object", frame_height)
elif between(cap, 15000, 17500):
frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_orange = np.array([0, 100, 100]) # H-10, 100, 100
upper_orange = np.array([19, 255, 255]) # H+10, 255, 255
frame = cv2.inRange(frame_hsv, lower_orange, upper_orange)
frame = cv2.morphologyEx(
frame, cv2.MORPH_CLOSE, np.ones((5, 5)))
frame = np.stack([frame] * 3, axis=-1)
add_subtitle(
frame, "Closing operation to remove reflection line", frame_height)
elif between(cap, 17500, 20100):
frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_orange = np.array([0, 100, 100]) # H-10, 100, 100
upper_orange = np.array([19, 255, 255]) # H+10, 255, 255
frame = cv2.inRange(frame_hsv, lower_orange, upper_orange)
frame = cv2.morphologyEx(
frame, cv2.MORPH_CLOSE, np.ones((9, 9)))
frame = cv2.erode(frame, np.ones((3, 3)), iterations=1)
frame = np.stack([frame] * 3, axis=-1)
add_subtitle(
frame, "Erosion", frame_height)
elif between(cap, 20100, 21000):
add_subtitle(frame, "Original", frame_height)
elif between(cap, 21000, 22500):
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
grad_y = cv2.Sobel(frame, cv2.CV_8U, 0, 1,
ksize=3) # horizontal
frame = cv2.convertScaleAbs(grad_y)
frame = np.stack([frame] * 3, axis=-1).astype("uint8")
# MAKE DIFFERENT COLOR
add_subtitle(
frame, "Horizontal Sobel ksize=3", frame_height)
elif between(cap, 22500, 24000):
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
grad_y = cv2.Sobel(frame, cv2.CV_8U, 0, 1,
ksize=5) # horizontal
frame = cv2.convertScaleAbs(grad_y)
frame = np.stack([frame] * 3, axis=-1).astype("uint8")
add_subtitle(
frame, "Horizontal Sobel ksize=5", frame_height)
elif between(cap, 24000, 25000):
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
grad_x = cv2.Sobel(frame, cv2.CV_8U, 1,
0, ksize=3) # vertical
frame = cv2.convertScaleAbs(grad_x)
frame = np.stack([frame] * 3, axis=-1).astype("uint8")
add_subtitle(
frame, "Vertical Sobel ksize=3", frame_height)
elif between(cap, 25000, 27100):
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
grad_x = cv2.Sobel(frame, cv2.CV_8U, 1, 0, ksize=3)
grad_y = cv2.Sobel(frame, cv2.CV_8U, 0, 1, ksize=3)
abs_grad_x = cv2.convertScaleAbs(grad_x)
abs_grad_y = cv2.convertScaleAbs(grad_y)
frame = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
frame = np.stack([frame] * 3, axis=-1).astype("uint8")
add_subtitle(
frame, "All edges", frame_height)
elif between(cap, 27100, 29500):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rows = gray.shape[0]
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1,
rows / 8, param1=300, param2=15, minRadius=1, maxRadius=100)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
center = (i[0], i[1])
# circle center
cv2.circle(frame, center, 1, (77, 137, 255), 3)
# circle outline
radius = i[2]
cv2.circle(frame, center, radius, (255, 0, 255), 2)
add_subtitle(
frame, "All circles with Hough", frame_height)
elif between(cap, 29500, 32000):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rows = gray.shape[0]
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1,
rows / 8, param1=300, param2=15, minRadius=80, maxRadius=100)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
center = (i[0], i[1])
# circle center
cv2.circle(frame, center, 1, (77, 137, 255), 3)
# circle outline
radius = i[2]
cv2.circle(frame, center, radius, (255, 0, 255), 2)
add_subtitle(
frame, "Only big circles with Hough", frame_height)
elif between(cap, 32000, 35200):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rows = gray.shape[0]
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1,
rows / 8, param1=300, param2=15, minRadius=1, maxRadius=60)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
center = (i[0], i[1])
# circle center
cv2.circle(frame, center, 1, (77, 137, 255), 3)
# circle outline
radius = i[2]
cv2.circle(frame, center, radius, (255, 0, 255), 2)
add_subtitle(
frame, "Only small circles with Hough", frame_height)
elif between(cap, 35200, 36500):
add_subtitle(frame, "Original", frame_height)
elif between(cap, 36200, 39000):
template = cv2.imread('black.png')
h, w = template.shape[:2]
template = cv2.resize(template, (int(w * 0.6), int(h * 0.6)))
h, w = template.shape[:2]
method = eval("cv2.TM_CCOEFF")
original_shape = frame.shape[:2]
res = cv2.matchTemplate(frame, template, method)
res = res - res.min()
res = res / res.max()
res = (res * 255).astype("uint8")
frame = cv2.cvtColor(res, cv2.COLOR_GRAY2BGR)
frame = cv2.resize(frame, original_shape[::-1])
add_subtitle(frame, "Template matching", frame_height)
elif between(cap, 39000, 40000):
add_subtitle(frame, "Original", frame_height)
elif between(cap, 40000, 40100):
replace_image = frame # live image to replace with
replace_image[:, :, :] = replace_image[:, :, :] + 3
add_subtitle(frame, "Original", frame_height)
elif between(cap, 40100, 40700):
add_subtitle(frame, "Original", frame_height)
elif between(cap, 40700, 43300):
kernel = np.ones((3, 3), np.uint8)
frame_hsv = cv2.cvtColor(
frame, cv2.COLOR_BGR2HSV) # BGR to HSV
lb = np.array([90, 50, 38]) # blue mask
ub = np.array([110, 255, 255])
mask = cv2.inRange(frame_hsv, lb, ub) # Create Mask
opening = cv2.morphologyEx(
mask, cv2.MORPH_CLOSE, kernel) # Morphology
contours, _ = cv2.findContours(opening, cv2.RETR_TREE, # Find contours
cv2.CHAIN_APPROX_NONE)
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
if w > 2 and h > 20:
cv2.rectangle(frame, (x, y), (x+w, y+h),
(42, 255, 100), 4)
add_subtitle(frame, "Object Tracking", frame_height)
elif between(cap, 43300, 48000):
kernel = np.ones((3, 3), np.uint8)
frame_hsv = cv2.cvtColor(
frame, cv2.COLOR_BGR2HSV) # BGR to HSV
lb = np.array([90, 50, 38])
ub = np.array([110, 255, 255])
mask = cv2.inRange(frame_hsv, lb, ub) # Create a blue mask
opening = cv2.morphologyEx(
mask, cv2.MORPH_CLOSE, kernel) # Morphology
dilate = cv2.dilate(opening, kernel, iterations=2)
midResult1 = cv2.bitwise_and(
replace_image, replace_image, mask=dilate)
invert = cv2.bitwise_not(
dilate, dilate, mask=None) # Invert the mask
midResult2 = cv2.bitwise_and(frame, frame, mask=invert)
frame = midResult1 + midResult2
add_subtitle(
frame, "Make the pen invisible", frame_height)
elif between(cap, 48000, 54300):
add_subtitle(
frame, "Make the pen visible", frame_height)
elif between(cap, 54300, 58300):
kernel = np.ones((3, 3), np.uint8)
frame_hsv = cv2.cvtColor(
frame, cv2.COLOR_BGR2HSV) # BGR to HSV
lb = np.array([90, 50, 38])
ub = np.array([110, 255, 255])
mask = cv2.inRange(frame_hsv, lb, ub) # Create a blue mask
opening = cv2.morphologyEx(
mask, cv2.MORPH_CLOSE, kernel) # Morphology
dilate = cv2.dilate(opening, kernel, iterations=2)
midResult1 = cv2.bitwise_and(
replace_image, replace_image, mask=dilate)
invert = cv2.bitwise_not(
dilate, dilate, mask=None) # Invert the mask
midResult2 = cv2.bitwise_and(frame, frame, mask=invert)
frame = midResult1 + midResult2
add_subtitle(
frame, "Make the pen invisible", frame_height)
elif between(cap, 58300, 60000):
add_subtitle(
frame, "Original", frame_height)
# write frame that you processed to output
out.write(frame)
cv2.imshow('Video', frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture and writing object
cap.release()
out.release()
# Closes all the frames
cv2.destroyAllWindows()
if __name__ == '__main__':
main("input.mp4", "output.mp4")