1
1
import argparse
2
+ import json
2
3
import random
3
4
from collections import deque
4
5
from operator import itemgetter
@@ -32,7 +33,7 @@ def parse_args():
32
33
parser .add_argument ('checkpoint' , help = 'checkpoint file/url' )
33
34
parser .add_argument ('video_path' , help = 'video file/url' )
34
35
parser .add_argument ('label' , help = 'label file' )
35
- parser .add_argument ('out_file' , help = 'output filename ' )
36
+ parser .add_argument ('out_file' , help = 'output result file in video/json ' )
36
37
parser .add_argument (
37
38
'--input-step' ,
38
39
type = int ,
@@ -58,6 +59,47 @@ def parse_args():
58
59
return args
59
60
60
61
62
+ def show_results_video (result_queue , text_info , thr , msg , frame , video_writer ):
63
+ if len (result_queue ) != 0 :
64
+ text_info = {}
65
+ results = result_queue .popleft ()
66
+ for i , result in enumerate (results ):
67
+ selected_label , score = result
68
+ if score < thr :
69
+ break
70
+ location = (0 , 40 + i * 20 )
71
+ text = selected_label + ': ' + str (round (score , 2 ))
72
+ text_info [location ] = text
73
+ cv2 .putText (frame , text , location , FONTFACE , FONTSCALE , FONTCOLOR ,
74
+ THICKNESS , LINETYPE )
75
+ elif len (text_info ):
76
+ for location , text in text_info .items ():
77
+ cv2 .putText (frame , text , location , FONTFACE , FONTSCALE , FONTCOLOR ,
78
+ THICKNESS , LINETYPE )
79
+ else :
80
+ cv2 .putText (frame , msg , (0 , 40 ), FONTFACE , FONTSCALE , MSGCOLOR ,
81
+ THICKNESS , LINETYPE )
82
+ video_writer .write (frame )
83
+ return text_info
84
+
85
+
86
+ def get_results_json (result_queue , text_info , thr , msg , ind , out_json ):
87
+ if len (result_queue ) != 0 :
88
+ text_info = {}
89
+ results = result_queue .popleft ()
90
+ for i , result in enumerate (results ):
91
+ selected_label , score = result
92
+ if score < thr :
93
+ break
94
+ text_info [i + 1 ] = selected_label + ': ' + str (round (score , 2 ))
95
+ out_json [ind ] = text_info
96
+ elif len (text_info ):
97
+ out_json [ind ] = text_info
98
+ else :
99
+ out_json [ind ] = msg
100
+ return text_info , out_json
101
+
102
+
61
103
def show_results (model , data , label , args ):
62
104
frame_queue = deque (maxlen = args .sample_length )
63
105
result_queue = deque (maxlen = 1 )
@@ -70,11 +112,13 @@ def show_results(model, data, label, args):
70
112
71
113
msg = 'Preparing action recognition ...'
72
114
text_info = {}
115
+ out_json = {}
73
116
fourcc = cv2 .VideoWriter_fourcc (* 'mp4v' )
74
117
frame_size = (frame_width , frame_height )
75
118
76
119
ind = 0
77
- video_writer = cv2 .VideoWriter (args .out_file , fourcc , fps , frame_size )
120
+ video_writer = None if args .out_file .endswith ('.json' ) \
121
+ else cv2 .VideoWriter (args .out_file , fourcc , fps , frame_size )
78
122
prog_bar = mmcv .ProgressBar (num_frames )
79
123
backup_frames = []
80
124
@@ -108,28 +152,20 @@ def show_results(model, data, label, args):
108
152
results = scores_sorted [:num_selected_labels ]
109
153
result_queue .append (results )
110
154
111
- if len (result_queue ) != 0 :
112
- text_info = {}
113
- results = result_queue .popleft ()
114
- for i , result in enumerate (results ):
115
- selected_label , score = result
116
- if score < args .threshold :
117
- break
118
- location = (0 , 40 + i * 20 )
119
- text = selected_label + ': ' + str (round (score , 2 ))
120
- text_info [location ] = text
121
- cv2 .putText (frame , text , location , FONTFACE , FONTSCALE ,
122
- FONTCOLOR , THICKNESS , LINETYPE )
123
- elif len (text_info ):
124
- for location , text in text_info .items ():
125
- cv2 .putText (frame , text , location , FONTFACE , FONTSCALE ,
126
- FONTCOLOR , THICKNESS , LINETYPE )
155
+ if args .out_file .endswith ('.json' ):
156
+ text_info , out_json = get_results_json (result_queue , text_info ,
157
+ args .threshold , msg , ind ,
158
+ out_json )
127
159
else :
128
- cv2 .putText (frame , msg , (0 , 40 ), FONTFACE , FONTSCALE , MSGCOLOR ,
129
- THICKNESS , LINETYPE )
130
- video_writer .write (frame )
160
+ text_info = show_results_video (result_queue , text_info ,
161
+ args .threshold , msg , frame ,
162
+ video_writer )
163
+
131
164
cap .release ()
132
165
cv2 .destroyAllWindows ()
166
+ if args .out_file .endswith ('.json' ):
167
+ with open (args .out_file , 'w' ) as js :
168
+ json .dump (out_json , js )
133
169
134
170
135
171
def inference (model , data , args , frame_queue ):
0 commit comments