-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
420 lines (294 loc) · 15.5 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import os
import cv2
import numpy as np
import ar
IMG_DIR = "input_images"
VID_DIR = "input_videos"
OUT_DIR = "./"
if not os.path.isdir(OUT_DIR):
os.makedirs(OUT_DIR)
ds_w, ds_h = 800, 600
def test_1():
"""
"""
print("\nTest Step 1: Extract markers in pictures.")
input_images = ['sim_clear_scene.jpg', 'sim_noisy_scene_1.jpg',
'sim_noisy_scene_2.jpg']
output_images = ['ar-1-a-1.png', 'ar-1-a-2.png', 'ar-1-a-3.png']
# Optional template image
template = cv2.imread(os.path.join(IMG_DIR, "template.jpg"))
for img_in, img_out in zip(input_images, output_images):
print("Input image: {}".format(img_in))
# Open image and identify the four marker positions
scene = cv2.imread(os.path.join(IMG_DIR, img_in))
marker_positions = ar.find_markers(scene, template)
for marker in marker_positions:
mark_location(scene, marker)
save_image(img_out, scene)
def test_2():
print("\nTest Step 2: Draw Box around Detected Marker Zone.")
input_images = ['ar-2-a_base.jpg', 'ar-2-b_base.jpg',
'ar-2-c_base.jpg', 'ar-2-d_base.jpg', 'ar-2-e_base.jpg']
output_images = ['ar-2-a-1.png', 'ar-2-a-2.png', 'ar-2-a-3.png',
'ar-2-a-4.png', 'ar-2-a-5.png']
# Optional template image
template = cv2.imread(os.path.join(IMG_DIR, "template.jpg"))
for img_in, img_out in zip(input_images, output_images):
print("Input image: {}".format(img_in))
# Open image and identify the four marker positions
scene = cv2.imread(os.path.join(IMG_DIR, img_in))
scene = cv2.resize(scene, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
markers = ar.find_markers(scene, template)
image_with_box = ar.draw_box(scene, markers, 1)
save_image(img_out, image_with_box)
def test_3():
print("\nTest Step 3: Project a Regular Image into the Detected Zone.")
input_images = ['ar-3-a_base.jpg', 'ar-3-b_base.jpg', 'ar-3-c_base.jpg']
output_images = ['ar-3-a-1.png', 'ar-3-a-2.png', 'ar-3-a-3.png']
# Advertisement image
advert = cv2.imread(os.path.join(IMG_DIR, "img-3-a-1.png"))
advert = cv2.resize(advert, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
src_points = ar.get_corners_list(advert)
# Optional template image
template = cv2.imread(os.path.join(IMG_DIR, "template.jpg"))
for img_in, img_out in zip(input_images, output_images):
print("Input image: {}".format(img_in))
# Open image and identify the four marker positions
scene = cv2.imread(os.path.join(IMG_DIR, img_in))
scene = cv2.resize(scene, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
markers = ar.find_markers(scene, template)
# homography = ar.find_four_point_transform(src_points, markers)
homography,_ = cv2.findHomography(np.float32(src_points), np.float32(markers))
projected_img = ar.project_imageA_onto_imageB(advert, scene, homography)
projected_img = ar.draw_box(projected_img, markers, 1)
save_image(img_out, projected_img)
def test_3_mirror():
print("\nTest Step 3M: Project a Mirror Image in the Right Perspective.")
input_images = ['ar-2-a_base.jpg', 'ar-2-b_base.jpg','ar-2-c_base.jpg', 'ar-2-d_base.jpg', 'ar-2-e_base.jpg']
output_images = ['ar-3-m-1.png', 'ar-3-m-2.png', 'ar-3-m-3.png', 'ar-3-m-4.png', 'ar-3-m-5.png']
output_images_mirror = ['ar-3-m-1-mirror.png', 'ar-3-m-2-mirror.png', 'ar-3-m-3-mirror.png', 'ar-3-m-4-mirror.png', 'ar-3-m-5-mirror.png']
# Advertisement image
advert = cv2.imread(os.path.join(IMG_DIR, "static_subj.jpg"))
advert = cv2.resize(advert, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
src_points = ar.get_corners_list(advert)
# Optional template image
template = cv2.imread(os.path.join(IMG_DIR, "template.jpg"))
# for img_in, img_out in zip(input_images, output_images):
L = len(input_images)
# L = 3
for i in range(L):
img_in = input_images[i]
img_out = output_images[i]
img_mirror = output_images_mirror[i]
print("Input image: {}".format(img_in))
# Open image and identify the four marker positions
scene = cv2.imread(os.path.join(IMG_DIR, img_in))
scene = cv2.resize(scene, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
markers = ar.find_markers(scene, template)
# homography = ar.find_four_point_transform(src_points, markers)
homography,_ = cv2.findHomography(np.float32(src_points), np.float32(markers))
projected_img = ar.project_imageA_onto_imageB(advert, scene, homography)
projected_img = ar.draw_box(projected_img, markers, 1)
save_image(img_out, projected_img)
# m_homology = np.linalg.inv(homography)
# scale = np.diag([0.1,0.1,0.1])
# m_homology = scale * m_homology * np.linalg.inv(scale)
src_pts = np.float32(src_points)
dst_pts = np.float32(markers)
len_src = np.sum([np.linalg.norm([src_pts[3],src_pts[0]]),np.linalg.norm(src_pts[0:2]),np.linalg.norm(src_pts[1:3]),np.linalg.norm(src_pts[2:])])
len_dst = np.sum([np.linalg.norm([dst_pts[3],dst_pts[0]]),np.linalg.norm(dst_pts[0:2]),np.linalg.norm(dst_pts[1:3]),np.linalg.norm(dst_pts[2:])])
scale = 1.5*len_src / len_dst
dst_pts = np.mean(dst_pts,0) + (dst_pts-np.mean(dst_pts,0))*scale
# print(markers,src_points,scale,advert.shape)
m_homography,_ = cv2.findHomography(src_pts,dst_pts)
m_homography = np.linalg.inv(m_homography)
projected_img_mirror = ar.project_mirror_onto_imageB(advert, scene, homography, m_homography)
projected_img_mirror = ar.draw_box(projected_img_mirror, markers, 1)
save_image(img_mirror, projected_img_mirror)
def test_4():
print("\nTest Step 4: Real-Time Marker Location by Feature Detection.")
frame_ids = [50, 100, 200, 300]
fps = 40
unit_for_test_4_and_5("scene_1.mov", fps, frame_ids, "scene_1a", 1, False)
unit_for_test_4_and_5("scene_2.mov", fps, frame_ids, "scene_2a", 1, False)
unit_for_test_4_and_5("scene_3.mov", fps, frame_ids, "scene_3a", 1, False)
unit_for_test_4_and_5("scene_4.mov", fps, frame_ids, "scene_4a", 1, False)
unit_for_test_4_and_5("scene_5.mov", fps, frame_ids, "scene_5a", 1, False)
unit_for_test_4_and_5("scene_6.mov", fps, frame_ids, "scene_6a", 1, False)
unit_for_test_4_and_5("scene_7.mp4", fps, frame_ids, "scene_7a", 1, False)
unit_for_test_4_and_5("scene_8.mp4", fps, frame_ids, "scene_8a", 1, False)
unit_for_test_4_and_5("scene_9.mp4", fps, frame_ids, "scene_9a", 1, False)
unit_for_test_4_and_5("scene_x.mp4", fps, frame_ids, "scene_xa", 1, False)
def test_5():
print("\nTest Step 5: Real-Time Box Drawing on Videos.")
frame_ids = [50, 100, 200, 300]
fps = 40
unit_for_test_4_and_5("scene_1.mov", fps, frame_ids, "scene_1b", 1, True)
unit_for_test_4_and_5("scene_2.mov", fps, frame_ids, "scene_2b", 1, True)
unit_for_test_4_and_5("scene_3.mov", fps, frame_ids, "scene_3b", 1, True)
unit_for_test_4_and_5("scene_4.mov", fps, frame_ids, "scene_4b", 1, True)
unit_for_test_4_and_5("scene_5.mov", fps, frame_ids, "scene_5b", 1, True)
unit_for_test_4_and_5("scene_6.mov", fps, frame_ids, "scene_6b", 1, True)
unit_for_test_4_and_5("scene_7.mp4", fps, frame_ids, "scene_7b", 1, True)
unit_for_test_4_and_5("scene_8.mp4", fps, frame_ids, "scene_8b", 1, True)
unit_for_test_4_and_5("scene_9.mp4", fps, frame_ids, "scene_9b", 1, True)
unit_for_test_4_and_5("scene_x.mp4", fps, frame_ids, "scene_xb", 1, True)
def test_6():
print("\nTest Step 6: Real-Time Mirror Projection on Videos.")
my_video = "dyn_subj_1.mov"
frame_ids = [50, 100, 200, 300]
fps = 40
unit_for_test_6("scene_1.mov", fps, frame_ids, "scene_1c", 1, my_video)
unit_for_test_6("scene_2.mov", fps, frame_ids, "scene_2c", 1, my_video)
unit_for_test_6("scene_3.mov", fps, frame_ids, "scene_3c", 1, my_video)
unit_for_test_6("scene_4.mov", fps, frame_ids, "scene_4c", 1, my_video)
unit_for_test_6("scene_5.mov", fps, frame_ids, "scene_5c", 1, my_video)
unit_for_test_6("scene_6.mov", fps, frame_ids, "scene_6c", 1, my_video)
unit_for_test_6("scene_7.mp4", fps, frame_ids, "scene_7c", 1, my_video)
unit_for_test_6("scene_8.mp4", fps, frame_ids, "scene_8c", 1, my_video)
unit_for_test_6("scene_9.mp4", fps, frame_ids, "scene_9c", 1, my_video)
unit_for_test_6("scene_x.mp4", fps, frame_ids, "scene_xc", 1, my_video)
def test_7():
print("\nTest Step 7: Real-Time Webcam Mirror Projection on Videos.")
my_video = 0
frame_ids = [50, 100, 200, 300]
fps = 40
unit_for_test_6("scene_1.mov", fps, frame_ids, "scene_1d", 1, my_video,True)
unit_for_test_6("scene_2.mov", fps, frame_ids, "scene_2d", 1, my_video,True)
unit_for_test_6("scene_3.mov", fps, frame_ids, "scene_3d", 1, my_video,True)
unit_for_test_6("scene_4.mov", fps, frame_ids, "scene_4d", 1, my_video,True)
unit_for_test_6("scene_5.mov", fps, frame_ids, "scene_5d", 1, my_video,True)
unit_for_test_6("scene_6.mov", fps, frame_ids, "scene_6d", 1, my_video,True)
unit_for_test_6("scene_7.mp4", fps, frame_ids, "scene_7d", 1, my_video,True)
unit_for_test_6("scene_8.mp4", fps, frame_ids, "scene_8d", 1, my_video,True)
unit_for_test_6("scene_9.mp4", fps, frame_ids, "scene_9d", 1, my_video,True)
unit_for_test_6("scene_x.mp4", fps, frame_ids, "scene_xd", 1, my_video,True)
def unit_for_test_6(video_name, fps, frame_ids, output_prefix,counter_init,my_video,isWebcam=False):
video = os.path.join(VID_DIR, video_name)
image_gen = ar.video_frame_generator(video)
if isWebcam:
image_gen2 = ar.video_frame_generator(0)
else:
video2 = os.path.join(VID_DIR, my_video)
image_gen2 = ar.video_frame_generator(video2)
image = image_gen.__next__()
image = cv2.resize(image, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
h, w, d = image.shape
image2 = image_gen2.__next__()
image2 = cv2.resize(image2, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
h2, w2, d2 = image2.shape
out_path = "ar_{}-{}".format(output_prefix[4:], video_name)
video_out = mp4_video_writer(out_path, (w, h), fps)
# Optional template image
template = cv2.imread(os.path.join(IMG_DIR, "template.jpg"))
# advert = cv2.imread(os.path.join(IMG_DIR, "img-3-a-1.png"))
src_points = ar.get_corners_list(image2)
output_counter = counter_init
frame_num = 1
while image is not None and image2 is not None:
print("Processing fame {}".format(frame_num))
markers = ar.find_markers(image, template)
# homography = ar.find_four_point_transform(src_points, markers)
homography,_ = cv2.findHomography(np.float32(src_points), np.float32(markers))
# image = ar.project_imageA_onto_imageB(image2, image, homography)
src_pts = np.float32(src_points)
dst_pts = np.float32(markers)
len_src = np.sum([np.linalg.norm([src_pts[3],src_pts[0]]),np.linalg.norm(src_pts[0:2]),np.linalg.norm(src_pts[1:3]),np.linalg.norm(src_pts[2:])])
len_dst = np.sum([np.linalg.norm([dst_pts[3],dst_pts[0]]),np.linalg.norm(dst_pts[0:2]),np.linalg.norm(dst_pts[1:3]),np.linalg.norm(dst_pts[2:])])
scale = len_src / len_dst
dst_pts = np.mean(dst_pts,0) + (dst_pts-np.mean(dst_pts,0))*scale
# print(markers,src_points,scale,advert.shape)
m_homography,_ = cv2.findHomography(src_pts,dst_pts)
m_homography = np.linalg.inv(m_homography)
projected_img_mirror = ar.project_mirror_onto_imageB(image2, image, homography, m_homography)
image = ar.draw_box(projected_img_mirror, markers, 2)
# save_image(img_mirror, projected_img_mirror)
frame_id = frame_ids[(output_counter - 1) % 3]
if frame_num == frame_id:
out_str = output_prefix + "-{}.png".format(output_counter)
save_image(out_str, image)
output_counter += 1
video_out.write(image)
if isWebcam:
# Display the resulting frame
cv2.imshow('frame',image)
if cv2.waitKey(1) & 0xFF == ord('q'): break
image = image_gen.__next__()
image2 = image_gen2.__next__()
if image is not None: image = cv2.resize(image, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
if image2 is not None: image2 = cv2.resize(image2, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
frame_num += 1
video_out.release()
def unit_for_test_4_and_5(video_name, fps, frame_ids, output_prefix,
counter_init, is_part5):
video = os.path.join(VID_DIR, video_name)
image_gen = ar.video_frame_generator(video)
image = image_gen.__next__()
image = cv2.resize(image, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
h, w, d = image.shape
out_path = "ar_{}-{}".format(output_prefix[4:], video_name)
video_out = mp4_video_writer(out_path, (w, h), fps)
# Optional template image
template = cv2.imread(os.path.join(IMG_DIR, "template.jpg"))
if is_part5:
advert = cv2.imread(os.path.join(IMG_DIR, "img-3-a-1.png"))
src_points = ar.get_corners_list(advert)
output_counter = counter_init
frame_num = 1
while image is not None:
print("Processing fame {}".format(frame_num))
markers = ar.find_markers(image, template)
if is_part5:
# homography = ar.find_four_point_transform(src_points, markers)
homography,_ = cv2.findHomography(np.float32(src_points), np.float32(markers))
image = ar.project_imageA_onto_imageB(advert, image, homography)
else:
for marker in markers:
mark_location(image, marker)
frame_id = frame_ids[(output_counter - 1) % 3]
if frame_num == frame_id:
out_str = output_prefix + "-{}.png".format(output_counter)
save_image(out_str, image)
output_counter += 1
video_out.write(image)
image = image_gen.__next__()
if image is not None: image = cv2.resize(image, (ds_w,ds_h), interpolation = cv2.INTER_AREA)
frame_num += 1
video_out.release()
def mp4_video_writer(filename, frame_size, fps=20):
"""Opens and returns a video for writing.
Use the VideoWriter's `write` method to save images.
Remember to 'release' when finished.
Args:
filename (string): Filename for saved video
frame_size (tuple): Width, height tuple of output video
fps (int): Frames per second
Returns:
VideoWriter: Instance of VideoWriter ready for writing
"""
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
return cv2.VideoWriter(filename, fourcc, fps, frame_size)
def save_image(filename, image):
"""Convenient wrapper for writing images to the output directory."""
cv2.imwrite(os.path.join(OUT_DIR, filename), image)
def mark_location(image, pt):
"""Draws a dot on the marker center and writes the location as text nearby.
Args:
image (numpy.array): Image to draw on
pt (tuple): (x, y) coordinate of marker center
"""
color = (0, 50, 255)
cv2.circle(image, pt, 3, color, -1)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image, "(x:{}, y:{})".format(*pt), (pt[0]+15, pt[1]), font, 0.5, color, 1)
if __name__ == '__main__':
print("--- Real Time Projective Mirror Effect ---")
# Comment out the sections you want to skip
test_1()
test_2()
test_3()
test_3_mirror()
test_4()
test_5()
test_6()
test_7()