Skip to content

Commit e494e8a

Browse files
authored
Merge pull request #176 from bessszilard/add_video_output_and_dont_show_flag
Add video output and dont show flag
2 parents c277c09 + 0b0e543 commit e494e8a

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

detectvideo.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
flags.DEFINE_string('video', './data/road.mp4', 'path to input video')
2424
flags.DEFINE_float('iou', 0.45, 'iou threshold')
2525
flags.DEFINE_float('score', 0.25, 'score threshold')
26+
flags.DEFINE_string('output', None, 'path to output video')
27+
flags.DEFINE_string('output_format', 'XVID', 'codec used in VideoWriter when saving video to file')
28+
flags.DEFINE_boolean('dis_cv2_window', False, 'disable cv2 window during the process') # this is good for the .ipynb
2629

2730
def main(_argv):
2831
config = ConfigProto()
@@ -45,14 +48,27 @@ def main(_argv):
4548
else:
4649
saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
4750
infer = saved_model_loaded.signatures['serving_default']
51+
52+
if FLAGS.output:
53+
# by default VideoCapture returns float instead of int
54+
width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
55+
height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
56+
fps = int(vid.get(cv2.CAP_PROP_FPS))
57+
codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
58+
out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height))
4859

60+
frame_id = 0
4961
while True:
5062
return_value, frame = vid.read()
5163
if return_value:
5264
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
5365
image = Image.fromarray(frame)
5466
else:
67+
if frame_id == vid.get(cv2.CAP_PROP_FRAME_COUNT):
68+
print("Video processing complete")
69+
break
5570
raise ValueError("No image! Try with another video format")
71+
5672
frame_size = frame.shape[:2]
5773
image_data = cv2.resize(frame, (input_size, input_size))
5874
image_data = image_data / 255.
@@ -92,10 +108,18 @@ def main(_argv):
92108
result = np.asarray(image)
93109
info = "time: %.2f ms" %(1000*exec_time)
94110
print(info)
95-
cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
96-
result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
97-
cv2.imshow("result", result)
98-
if cv2.waitKey(1) & 0xFF == ord('q'): break
111+
112+
if not FLAGS.dis_cv2_window:
113+
cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
114+
result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
115+
cv2.imshow("result", result)
116+
if cv2.waitKey(1) & 0xFF == ord('q'): break
117+
118+
if FLAGS.output:
119+
result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
120+
out.write(result)
121+
122+
frame_id += 1
99123

100124
if __name__ == '__main__':
101125
try:

0 commit comments

Comments
 (0)