-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender.py
67 lines (59 loc) · 2.39 KB
/
render.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
import argparse
import cv2
import json
from utils import mask_frame
from xml.etree import ElementTree
import os
def parse_args():
parser = argparse.ArgumentParser(description='Example rendering script')
parser.add_argument('-v', '--video', required=True,
help='Input video')
parser.add_argument('-d', '--data', required=True,
help='Data for rendering detected and tracking results')
parser.add_argument('-m', '--mask', required=True,
help='Mask for the video')
return parser.parse_args()
def main(args):
tree = ElementTree.parse(args.mask)
mask = tree.getroot()
cap = cv2.VideoCapture(os.path.expanduser(args.video))
with open(args.rendering) as fp:
rendering = json.load(fp)
frame_index = 0
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
out = None
while cap.isOpened():
print('Parsing frame {:d} / {:d}...'.format(frame_index, frame_count))
success, frame = cap.read()
if not success:
break
masked_frame = mask_frame(frame, mask)
tracked_objects = rendering['{:d}'.format(frame_index)]
for tracked_object in tracked_objects:
object_index = int(tracked_object[0])
tl = (int(tracked_object[1]), int(tracked_object[2]))
br = (int(tracked_object[3]), int(tracked_object[4]))
cv2.rectangle(masked_frame, tl, br, (255, 0, 0), 2)
cv2.putText(
masked_frame, '{:d}'.format(object_index), (br[0]+10, br[1]),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
display_width = int(masked_frame.shape[1] * 0.5)
display_height = int(masked_frame.shape[0] * 0.5)
resized_frame = cv2.resize(
masked_frame, (display_width, display_height))
if out is None:
scenario = args.video.replace('videos/', '').replace('.mp4', '')
out = cv2.VideoWriter(
'output/{}.mp4'.format(scenario),
cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), 30,
(display_width,display_height))
out.write(resized_frame)
# cv2.imshow('Frame', resized_frame)
frame_index = frame_index + 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main(parse_args())