-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_processor.py
executable file
·696 lines (500 loc) · 21.8 KB
/
video_processor.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
#!/usr/bin/env python3
import os.path
import math
import cv2
import csv
import numpy as np
import geopy.distance
from configparser import ConfigParser
import sortedcollection
from sortedcollection import SortedCollection
ACCESS_TOKEN = ''
TILES_ZOOM = 15
#GREEN_COLOR = (155, 255, 155, 165)
#GREEN_COLOR_LIGHT = (155, 255, 155, 90)
GREEN_COLOR = (155, 255, 155, 210)
GREEN_COLOR_LIGHT = (155, 255, 155, 155)
SCALE_FACTOR = 1.0
class MyParser(ConfigParser):
def as_dict(self):
d = dict(self._sections)
for k in d:
d[k] = dict(self._defaults, **d[k])
d[k].pop('__name__', None)
return d
def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)
return (xtile, ytile)
def num2deg(xtile, ytile, zoom):
n = 2.0 ** zoom
lon_deg = xtile / n * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
lat_deg = math.degrees(lat_rad)
return (lat_deg, lon_deg)
tiles_cache = set()
tiles_cache_cv = {} # tile_key => opencv img
def precache_single_tile(x, y, z):
key = f'{z}_{x}_{y}'
if key in tiles_cache:
return
if not os.path.exists('tiles_cache'):
os.mkdir('tiles_cache')
img_file_path = os.path.join('tiles_cache', key + '.jpg')
if os.path.exists(img_file_path):
tiles_cache.add(key)
return
url = f'https://api.mapbox.com/styles/v1/mapbox/dark-v11/tiles/{z}/{x}/{y}?access_token={ACCESS_TOKEN}'
from urllib.request import urlopen
print('downloading file', img_file_path)
with urlopen(url) as file:
content = file.read()
with open(img_file_path, 'wb') as download:
download.write(content)
tiles_cache.add(key)
def precache_tiles(lat, lon):
x_tile, y_tile = deg2num(lat, lon, TILES_ZOOM)
for x in range(x_tile - 1, x_tile + 2):
for y in range(y_tile - 1, y_tile + 2):
precache_single_tile(x, y, TILES_ZOOM)
def get_tile(lat, lon):
x_tile, y_tile = deg2num(lat, lon, TILES_ZOOM)
key = f'{TILES_ZOOM}_{x_tile}_{y_tile}'
result = tiles_cache_cv.get(key)
cords_top_left = num2deg(x_tile, y_tile, TILES_ZOOM)
cords_bottom_right = num2deg(x_tile + 1, y_tile + 1, TILES_ZOOM)
if result is not None:
return (result, cords_top_left, cords_bottom_right)
img_file_path = os.path.join('tiles_cache', key + '.jpg')
map_img = cv2.imread(img_file_path)
map_img = cv2.cvtColor(map_img, cv2.COLOR_RGB2RGBA)
map_img[:, :, 3] = (200,)
map_img = cv2.resize(map_img, (0, 0), fx=0.4, fy=0.4)
tiles_cache_cv[key] = map_img
return (map_img, cords_top_left, cords_bottom_right)
def draw_img(dst_img, src_img, pos_x, pos_y):
h = src_img.shape[0]
w = src_img.shape[1]
px = pos_x
py = pos_y
src_px = 0
src_py = 0
#print('px', px)
if py < 0:
h += py
src_py -= py
py = 0
if px < 0:
w += px
src_px -= px
px = 0
if px >= dst_img.shape[1]:
return
if py >= dst_img.shape[0]:
return
if px + w >= dst_img.shape[1]:
w -= (px + w - dst_img.shape[1])
if py + h >= dst_img.shape[0]:
h -= (py + h - dst_img.shape[0])
#print('src_px', src_px)
dst_img[py:py + h, px:px + w] = src_img[src_py:src_py + h, src_px:src_px + w]
def get_big_tile(lat, lon):
img_center, cords_top_left, cords_bottom_right = get_tile(lat, lon)
lat_delta = cords_bottom_right[0] - cords_top_left[0]
lon_delta = cords_bottom_right[1] - cords_top_left[1]
img_heigh = img_center.shape[0]
img_width = img_center.shape[1]
result = np.zeros((img_heigh*3, img_width*3, 4), np.uint8)
# result_top = cords_top_left[0]
# result_left = cords_top_left[1]
# result_bottom = cords_bottom_right[0]
# result_right = cords_bottom_right[1]
for curr_y_idx, curr_lat in enumerate([lat - lat_delta, lat, lat + lat_delta]):
for curr_x_idx, curr_lon in enumerate([lon - lon_delta, lon, lon + lon_delta]):
curr_y = int(curr_y_idx * img_center.shape[0])
curr_x = int(curr_x_idx * img_center.shape[1])
curr_img, curr_cords_top_left, curr_cords_bottom_right = get_tile(curr_lat, curr_lon)
#print(curr_y, curr_x, img_heigh, img_width)
#print(result.shape, curr_img.shape)
result[curr_y:curr_y + img_heigh, curr_x: curr_x + img_width] = curr_img
#result[0:205, 0:205] = curr_img
# result_top = max(result_top, curr_cords_top_left[0])
# result_left = max(result_left, curr_cords_top_left[1])
# result_bottom = min(result_bottom, curr_cords_bottom_right[0])
# result_right = min(result_right, curr_cords_bottom_right[1])
return result, get_tile(lat-lat_delta, lon-lon_delta)[1], get_tile(lat+lat_delta, lon+lon_delta)[2]
#return result, (result_top, result_left), (result_bottom, result_right)
PLANE_ICON = cv2.imread('plane_icon.png', cv2.IMREAD_UNCHANGED)
BATTERY_ICON = cv2.imread('battery-icon.png', cv2.IMREAD_UNCHANGED)
def rotate_image(image, angle):
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result
def get_centered_tile(lat, lon, angle):
#todo: generate centered tile
#img, cords_top_left, cords_bottom_right = get_tile(lat, lon)
img, cords_top_left, cords_bottom_right = get_big_tile(lat, lon)
#return img
lat_delta = cords_bottom_right[0] - cords_top_left[0]
lon_delta = cords_bottom_right[1] - cords_top_left[1]
lat_pos_factor = (lat - cords_top_left[0]) / lat_delta
lon_pos_factor = (lon - cords_top_left[1]) / lon_delta
img_width = int(img.shape[1] / 3)
img_heigh = int(img.shape[0] / 3)
pos_x = int((img_width * 0.5) - (lon_pos_factor * img_width * 3))
pos_y = int((img_heigh * 0.5) - (lat_pos_factor * img_heigh * 3))
#print(pos_x)
# pos left right delta factor img_w
# 10 5 40 45 0.22 20
# 15 5 40 45 0.33 20
# 20 5 40 45 0.44 20
result = np.zeros((img_heigh, img_width, 4), np.uint8)
draw_img(result, img, pos_x, pos_y)
#result = cv2.circle(result, (int(img_heigh * 0.5), int(img_width * 0.5)), 1, (255, 255, 255, 210), 2)
#draw_img(result, PLANE_ICON, int(img_width * 0.5), int(img_heigh * 0.5))
#angle_deg = math.degrees(angle * 0.1)
#print(angle * 0.1, angle_deg)
if angle is not None:
#print(angle)
plane_icon = rotate_image(PLANE_ICON, angle - 90)
add_transparent_image(
result, plane_icon, 0, 0,
int(img_width * 0.5 - plane_icon.shape[1] * 0.5),
int(img_heigh * 0.5 - plane_icon.shape[0] * 0.5),
)
#result[pos_y:pos_y + img.shape[0], pos_x:pos_x + img.shape[1]] = img
return result
def test_map():
curr_lat = 41.582467
curr_lon = 41.571244
lat_step = 0.000
lon_step = 0.0001
while True:
curr_lat += lat_step
curr_lon += lon_step
map_img = get_centered_tile(curr_lat, curr_lon)
cv2.imshow('Map', map_img)
key_in = cv2.waitKey(100) & 0xFF
if key_in == ord('q'):
break
cv2.destroyAllWindows()
def add_transparent_image(background, foreground, x_offset=None, y_offset=None, shift_x=0, shift_y=0):
bg_h, bg_w, bg_channels = background.shape
fg_h, fg_w, fg_channels = foreground.shape
#assert bg_channels == 3, f'background image should have exactly 3 channels (RGB). found:{bg_channels}'
assert fg_channels == 4, f'foreground image should have exactly 4 channels (RGBA). found:{fg_channels}'
# center by default
if x_offset is None: x_offset = (bg_w - fg_w) // 2
if y_offset is None: y_offset = (bg_h - fg_h) // 2
w = min(fg_w, bg_w, fg_w + x_offset, bg_w - x_offset)
h = min(fg_h, bg_h, fg_h + y_offset, bg_h - y_offset)
if w < 1 or h < 1: return
# clip foreground and background images to the overlapping regions
bg_x = max(0, x_offset) + shift_x
bg_y = max(0, y_offset) + shift_y
fg_x = max(0, x_offset * -1)
fg_y = max(0, y_offset * -1)
h = min(h, bg_h - bg_y)
w = min(w, bg_w - bg_x)
#print('sizes:', w, h)
foreground = foreground[fg_y:fg_y + h, fg_x:fg_x + w]
background_subsection = background[bg_y:bg_y + h, bg_x:bg_x + w][:, :, :3]
# separate alpha and color channels from the foreground image
foreground_colors = foreground[:, :, :3]
alpha_channel = foreground[:, :, 3] / 255 # 0-255 => 0.0-1.0
# construct an alpha_mask that matches the image shape
alpha_mask = np.dstack((alpha_channel, alpha_channel, alpha_channel))
# combine the background with the overlay image weighted by alpha
try:
composite = background_subsection * (1 - alpha_mask) + foreground_colors * alpha_mask
except ValueError:
return
# overwrite the section of the background image that has been updated
background[bg_y:bg_y + h, bg_x:bg_x + w, :3] = composite
def load_settings(settings_file):
parser = MyParser()
parser.read(settings_file)
return parser.as_dict()['main']
def telemetry_time_to_seconds(telemetry_time):
vals = telemetry_time.split(':')
seconds = float(vals[-1])
minutes = 0
hours = 0
if len(vals) > 1:
minutes = int(vals[-2])
if len(vals) > 2:
hours = int(vals[-3])
return hours * 60 * 60 + minutes * 60 + seconds
def format_distance(distance_meters):
if distance_meters < 1000:
m = int(distance_meters)
return f'{m}m'
km = distance_meters / 1000
if km < 10:
return f'{km:.2f}km'
return f'{km:.1f}km'
class Telemetry:
def __init__(self, telemetry_file, modes):
with open(telemetry_file) as f:
data = [{k: v for k, v in row.items()}
for row in csv.DictReader(f, skipinitialspace=True)]
for element in data:
#print(element['Time'], telemetry_time_to_seconds(element['Time']))
element['time_seconds'] = telemetry_time_to_seconds(element['Time'])
gps = element['GPS'].split()
if len(gps) == 2:
element['lat'] = float(gps[0])
element['lon'] = float(gps[1])
# erase duplicates
new_data = []
prev_vals = None
for element in data:
curr_vals = (
element['GPS'],
element['GSpd(kmh)'],
element['Alt(m)'],
)
if curr_vals == prev_vals:
continue
prev_vals = curr_vals
new_data.append(element)
data = new_data
# calculate distance
total_distance = 0
min_median_bat = 100.0
prev_cords = None
bat_vals = []
for element in data:
if 'lat' not in element:
continue
if float(element['GSpd(kmh)']) < 3.0 and abs(float(element['Alt(m)'])) < 3.0:
total_distance = 0
min_median_bat = 100.0
prev_cords = None
curr_cords = (element['lat'], element['lon'])
if prev_cords is not None:
curr_distance = geopy.distance.geodesic(prev_cords, curr_cords).m
total_distance += curr_distance
element['total_distance'] = total_distance
if prev_cords:
element['direction_y'] = curr_cords[0] - prev_cords[0]
element['direction_x'] = curr_cords[1] - prev_cords[1]
bat_vals.append(float(element['Bat_(%)']))
if len(bat_vals) > 45:
bat_vals = bat_vals[1:]
median_bat = sorted(bat_vals[:])[int(len(bat_vals) * 0.5)]
min_median_bat = min(median_bat, min_median_bat)
element['median_bat'] = median_bat
element['min_median_bat'] = min_median_bat
#element['angle'] = math.degrees(math.atan2(element['direction'][1], element['direction'][0]))
prev_cords = curr_cords
precache_tiles(element['lat'], element['lon'])
self.modes = modes
self.data = sortedcollection.SortedCollection(data, key=lambda x: x['time_seconds'])
def get_row(self, video_time, sync_video_start, sync_video_finish, sync_telemetry_start, sync_telemetry_finish):
if video_time < sync_video_start:
return None
if video_time >= sync_telemetry_finish:
return None
video_delta = sync_video_finish - sync_video_start
telemetry_delta = sync_telemetry_finish - sync_telemetry_start
passed_percent = (video_time - sync_video_start) / video_delta
target_telemetry = sync_telemetry_start + passed_percent * telemetry_delta
if target_telemetry < sync_telemetry_start:
return None
if target_telemetry >= sync_telemetry_finish:
return None
idx = self.data.find_ge_idx(target_telemetry)
prev = max(0, idx - 1)
curr_frame = self.data[idx]
prev_frame = self.data[prev]
curr_frame_time = curr_frame['time_seconds']
prev_frame_time = prev_frame['time_seconds']
time_left = (target_telemetry - prev_frame_time)
time_delta = max(time_left, curr_frame_time - prev_frame_time)
factor = time_left / time_delta
result = {}
for field_name in (
'Alt(m)', 'GSpd(kmh)', 'lat', 'lon', 'total_distance', 'direction_x', 'direction_y',
'Capa(mAh)'
):
if field_name not in curr_frame or field_name not in prev_frame:
continue
curr_value = float(curr_frame[field_name])
prev_value = float(prev_frame[field_name])
result[field_name] = prev_value + factor * (curr_value - prev_value)
for field_name in ('Hdg(@)', 'Bat_(%)', 'median_bat', 'min_median_bat'):
result[field_name] = float(curr_frame[field_name])
for mode in self.modes:
mode_switch_value = curr_frame[mode['field']]
if mode_switch_value == mode['value']:
result['mode'] = mode['name']
return result
def draw_text(img, txt, pos, color, size):
font = cv2.FONT_HERSHEY_SIMPLEX
pos = (int(pos[0] * img.shape[1]), int(pos[1] * img.shape[0]))
img_txt = np.zeros((int(122 * SCALE_FACTOR), int(600 * SCALE_FACTOR), 4), np.uint8)
img_txt = cv2.putText(img_txt, txt, (0, int(56 * SCALE_FACTOR)), font, size[0] * SCALE_FACTOR, color, int(size[1] * SCALE_FACTOR), cv2.LINE_AA)
img_txt = cv2.resize(img_txt, (0, 0), fx=0.5, fy=0.5)
#cv2.imshow('txt', img_txt)
#print(pos)
#add_transparent_image(img, img_txt, pos[0], pos[1])
add_transparent_image(img, img_txt, 0, 0, pos[0], pos[1]-30)
return img
#cv2.putText(image, "Test String", (10, 50), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255, 255))
#return cv2.putText(img, txt, pos, font, size[0], color, size[1], cv2.LINE_AA)
def draw_side_indicator(frame, altitude, factor=10, step=0.09, pos_x=0.85, scale=1.0, start=-4, end=4):
altitude_rounded = int(altitude / factor)
diff = altitude - (altitude_rounded * factor)
diff_factor = diff / factor
for i in range(start, end+1):
curr_altitude = int(scale * (altitude_rounded - i) * factor)
curr_text = f'{curr_altitude} -'
if pos_x < 0.5:
curr_text = f'- {curr_altitude}'
frame = draw_text(
frame,
curr_text,
(pos_x, 0.44 + i * step + diff_factor * step),
GREEN_COLOR_LIGHT,
(1.1, 3),
)
return frame
def get_modes_settings(settings):
modes = []
for i in range(10):
curr_name = f'mode_{i}_name'
if curr_name not in settings:
continue
modes.append({
'name': settings[curr_name],
'field': settings[f'mode_{i}_field'],
'value': settings[f'mode_{i}_value'],
})
return modes
def main():
global SCALE_FACTOR, ACCESS_TOKEN
settings = load_settings('settings.ini')
ACCESS_TOKEN = settings['mapbox_token']
modes = get_modes_settings(settings)
telemetry = Telemetry(settings['telemetry_file'], modes)
# test_map()
# return
sync_video_start = telemetry_time_to_seconds(settings['sync_video_start'])
sync_video_finish = telemetry_time_to_seconds(settings['sync_video_finish'])
sync_telemetry_start = telemetry_time_to_seconds(settings['sync_telemetry_start'])
sync_telemetry_finish = telemetry_time_to_seconds(settings['sync_telemetry_finish'])
cap = cv2.VideoCapture(settings['video_file'])
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
fourcc = cv2.VideoWriter_fourcc(*'H264')
out = cv2.VideoWriter(
settings['video_file'] + '_out.mp4',
fourcc, fps, (width, height),
)
# map = cv2.imread('map.jpg')
# map = cv2.cvtColor(map, cv2.COLOR_RGB2RGBA)
#
# map[:, :, 3] = (128,)
#
# map = cv2.resize(map, (0, 0), fx=0.4, fy=0.4)
if not cap.isOpened():
print("Error opening video file")
return
# Read until video is completed
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
#print(frame.shape)
SCALE_FACTOR = frame.shape[0] / 720.0
frame_num = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
#total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)
frame_time = frame_num / fps
curr_telemetry = telemetry.get_row(
video_time=frame_time,
sync_video_start=sync_video_start,
sync_video_finish=sync_video_finish,
sync_telemetry_start=sync_telemetry_start,
sync_telemetry_finish=sync_telemetry_finish,
)
if curr_telemetry is not None:
altitude = int(curr_telemetry['Alt(m)'])
frame = draw_text(frame, f'm {altitude}', (0.75, 0.45), GREEN_COLOR, (1.5, 6))
frame = draw_side_indicator(
frame, curr_telemetry['Alt(m)'], pos_x=0.92, scale=0.1,
step=0.12, start=-3, end=3,
)
velocity = int(curr_telemetry['GSpd(kmh)'])
frame = draw_text(frame, f'{velocity} km/h', (0.14, 0.45), GREEN_COLOR, (1.5, 6))
frame = draw_side_indicator(
frame, curr_telemetry['GSpd(kmh)'], pos_x = 0.02, factor=5, scale=1.0,
step=0.08, start=-4, end=5,
)
battery = int(curr_telemetry['median_bat'])
mah = int(curr_telemetry['Capa(mAh)'])
frame = draw_text(frame, f'{battery}%', (0.83, 0.853), GREEN_COLOR, (2.6, 5))
frame = draw_text(frame, f'{mah} mah', (0.81, 0.895), GREEN_COLOR, (1.25, 3))
battery_img = cv2.resize(BATTERY_ICON, (
int(BATTERY_ICON.shape[0] * SCALE_FACTOR),
int(BATTERY_ICON.shape[1] * SCALE_FACTOR)
))
b_px = 0.81
b_py = 0.83
if SCALE_FACTOR > 1.25:
b_py = 0.84
add_transparent_image(
frame, battery_img, 0, 0,
int(b_px * frame.shape[1] - 0.50 * battery_img.shape[1]),
int(b_py * frame.shape[0] - 0.50 * battery_img.shape[0]),
)
mode = curr_telemetry.get('mode')
if mode:
frame = draw_text(frame, mode, (0.47, 0.15), GREEN_COLOR, (1.4, 4))
total_distance = curr_telemetry.get('total_distance', 0)
frame = draw_text(frame, format_distance(total_distance), (0.47, 0.2), GREEN_COLOR, (1.4, 4))
#out_str += f' Alt: {altitude}m Vel: {velocity}kmh'
if 'lat' in curr_telemetry:
angle = None
if 'direction_y' in curr_telemetry:
angle = math.degrees(math.atan2(curr_telemetry['direction_y'], curr_telemetry['direction_x']))
map_img = get_centered_tile(curr_telemetry['lat'], curr_telemetry['lon'], angle)
map_img = cv2.resize(map_img, (
int(map_img.shape[0] * SCALE_FACTOR),
int(map_img.shape[1] * SCALE_FACTOR)
))
add_transparent_image(frame, map_img, 0, 0, int(100 * SCALE_FACTOR), int(410 * SCALE_FACTOR))
# font = cv2.FONT_HERSHEY_SIMPLEX
# pos = (50, int(0.8*frame.shape[0]))
# fontScale = 1
# fontScale = 1
# color = (255, 255, 255)
# thickness = 2
# frame = cv2.putText(frame, out_str, pos, font, fontScale, color, thickness, cv2.LINE_AA)
#frame = draw_text(frame, out_str, (0.05, 0.9), (255, 255, 255, 255), (1, 2))
out.write(frame)
cv2.imshow('Frame', frame)
key_in = cv2.waitKey(10) & 0xFF
if key_in == ord('q'):
break
if key_in == ord('d'):
cap.set(cv2.CAP_PROP_POS_FRAMES, int(frame_num + fps * 5))
if key_in == ord('a'):
cap.set(cv2.CAP_PROP_POS_FRAMES, max(0, int(frame_num - fps * 5)))
if key_in == ord('c'):
cap.set(cv2.CAP_PROP_POS_FRAMES, int(frame_num + fps * 30))
if key_in == ord('z'):
cap.set(cv2.CAP_PROP_POS_FRAMES, max(0, int(frame_num - fps * 30)))
# When everything done, release
# the video capture object
cap.release()
out.release()
# Closes all the frames
cv2.destroyAllWindows()
if __name__ == '__main__':
main()