23
23
flags .DEFINE_string ('video' , './data/road.mp4' , 'path to input video' )
24
24
flags .DEFINE_float ('iou' , 0.45 , 'iou threshold' )
25
25
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
26
29
27
30
def main (_argv ):
28
31
config = ConfigProto ()
@@ -45,14 +48,27 @@ def main(_argv):
45
48
else :
46
49
saved_model_loaded = tf .saved_model .load (FLAGS .weights , tags = [tag_constants .SERVING ])
47
50
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 ))
48
59
60
+ frame_id = 0
49
61
while True :
50
62
return_value , frame = vid .read ()
51
63
if return_value :
52
64
frame = cv2 .cvtColor (frame , cv2 .COLOR_BGR2RGB )
53
65
image = Image .fromarray (frame )
54
66
else :
67
+ if frame_id == vid .get (cv2 .CAP_PROP_FRAME_COUNT ):
68
+ print ("Video processing complete" )
69
+ break
55
70
raise ValueError ("No image! Try with another video format" )
71
+
56
72
frame_size = frame .shape [:2 ]
57
73
image_data = cv2 .resize (frame , (input_size , input_size ))
58
74
image_data = image_data / 255.
@@ -92,10 +108,18 @@ def main(_argv):
92
108
result = np .asarray (image )
93
109
info = "time: %.2f ms" % (1000 * exec_time )
94
110
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
99
123
100
124
if __name__ == '__main__' :
101
125
try :
0 commit comments