1111import json
1212import copy
1313import logging
14+ import time
1415from collections import defaultdict
1516
16-
17+ import numpy as np
1718import torch
1819import PIL
1920import openpifpaf
2021from openpifpaf import datasets
21- from openpifpaf import decoder , network , visualizer , show , logger
22+ from openpifpaf import decoder , network , visualizer , show , logger , Predictor
2223from openpifpaf .predict import out_name
2324
2425try :
2728except ImportError :
2829 DOWNLOAD = None
2930from .visuals .printer import Printer
30- from .network import Loco
31- from .network .process import factory_for_gt , preprocess_pifpaf
31+ from .network import Loco , factory_for_gt , load_calibration , preprocess_pifpaf
3232from .activity import show_activities
3333
3434LOG = logging .getLogger (__name__ )
@@ -83,7 +83,6 @@ def download_checkpoints(args):
8383 else :
8484 path = MONOLOCO_MODEL_KI
8585 name = 'monoloco_pp-201203-1424.pkl'
86-
8786 model = os .path .join (torch_dir , name )
8887 dic_models [args .mode ] = model
8988 if not os .path .exists (model ):
@@ -92,6 +91,7 @@ def download_checkpoints(args):
9291 "pip install gdown to download a monoloco model, or pass the model path as --model"
9392 LOG .info ('Downloading model in %s' , torch_dir )
9493 DOWNLOAD (path , model , quiet = False )
94+ print (f"Using model: { name } " )
9595 return dic_models
9696
9797
@@ -121,6 +121,8 @@ def factory_from_args(args):
121121 LOG .debug ('neural network device: %s' , args .device )
122122
123123 # Add visualization defaults
124+ if not args .output_types and args .mode != 'keypoints' :
125+ args .output_types = ['multi' ]
124126 args .figure_width = 10
125127 args .dpi_factor = 1.0
126128
@@ -141,11 +143,12 @@ def factory_from_args(args):
141143
142144 if args .mode != 'keypoints' :
143145 assert any ((xx in args .output_types for xx in ['front' , 'bird' , 'multi' , 'json' ])), \
144- "No output type specified, please select one among front, bird, multi, json, or choose mode=keypoints"
146+ "No output type specified, please select one among front, bird, multi, json, or choose mode=keypoints"
145147
146148 # Configure
147149 decoder .configure (args )
148150 network .Factory .configure (args )
151+ Predictor .configure (args )
149152 show .configure (args )
150153 visualizer .configure (args )
151154
@@ -157,7 +160,6 @@ def predict(args):
157160 cnt = 0
158161 assert args .mode in ('keypoints' , 'mono' , 'stereo' )
159162 args , dic_models = factory_from_args (args )
160-
161163 # Load Models
162164 if args .mode in ('mono' , 'stereo' ):
163165 net = Loco (
@@ -167,18 +169,20 @@ def predict(args):
167169 n_dropout = args .n_dropout ,
168170 p_dropout = args .dropout )
169171
170- # for openpifpaf predicitons
171- predictor = openpifpaf . Predictor (checkpoint = args .checkpoint )
172+ # for openpifpaf predictions
173+ predictor = Predictor (checkpoint = args .checkpoint )
172174
173175 # data
174176 data = datasets .ImageList (args .images , preprocess = predictor .preprocess )
175177 if args .mode == 'stereo' :
176178 assert len (data .image_paths ) % 2 == 0 , "Odd number of images in a stereo setting"
177179
178180 pifpaf_outs = {}
181+ start = time .time ()
182+ timing = []
179183 for idx , (pred , _ , meta ) in enumerate (predictor .images (args .images , batch_size = args .batch_size )):
180184
181- if idx % args .batch_size != 0 : # Only for MonStereo
185+ if idx % args .batch_size != 0 : # Only for MonStereo
182186 pifpaf_outs ['right' ] = [ann .json_data () for ann in pred ]
183187 else :
184188 if args .json_output is not None :
@@ -187,11 +191,10 @@ def predict(args):
187191 with open (json_out_name , 'w' ) as f :
188192 json .dump ([ann .json_data () for ann in pred ], f )
189193
190- with open (meta ['file_name' ], 'rb' ) as f :
191- cpu_image = PIL .Image .open (f ).convert ('RGB' )
192194 pifpaf_outs ['pred' ] = pred
193195 pifpaf_outs ['left' ] = [ann .json_data () for ann in pred ]
194- pifpaf_outs ['image' ] = cpu_image
196+ pifpaf_outs ['file_name' ] = meta ['file_name' ]
197+ pifpaf_outs ['width_height' ] = meta ['width_height' ]
195198
196199 # Set output image name
197200 if args .output_directory is None :
@@ -207,18 +210,27 @@ def predict(args):
207210
208211 if (args .mode == 'mono' ) or (args .mode == 'stereo' and idx % args .batch_size != 0 ):
209212 # 3D Predictions
210- if args .mode != 'keypoints' :
211- im_size = (cpu_image .size [0 ], cpu_image .size [1 ]) # Original
212- kk , dic_gt = factory_for_gt (
213- im_size , focal_length = args .focal , name = im_name , path_gt = args .path_gt )
213+ if args .mode == 'keypoints' :
214+ dic_out = defaultdict (list )
215+ kk = None
216+ else :
217+ im_size = (float (pifpaf_outs ['width_height' ][0 ]), float (pifpaf_outs ['width_height' ][1 ]))
214218
219+ if args .path_gt is not None :
220+ dic_gt , kk = factory_for_gt (args .path_gt , im_name )
221+ else :
222+ kk = load_calibration (args .calibration , im_size , focal_length = args .focal_length )
223+ dic_gt = None
215224 # Preprocess pifpaf outputs and run monoloco
216225 boxes , keypoints = preprocess_pifpaf (
217226 pifpaf_outs ['left' ], im_size , enlarge_boxes = False )
218227
219228 if args .mode == 'mono' :
220229 LOG .info ("Prediction with MonoLoco++" )
221230 dic_out = net .forward (keypoints , kk )
231+ fwd_time = (time .time ()- start )* 1000
232+ timing .append (fwd_time ) # Skip Reordering and saving images
233+ print (f"Forward time: { fwd_time :.0f} ms" )
222234 dic_out = net .post_process (
223235 dic_out , boxes , keypoints , kk , dic_gt )
224236 if 'social_distance' in args .activities :
@@ -230,41 +242,46 @@ def predict(args):
230242 LOG .info ("Prediction with MonStereo" )
231243 _ , keypoints_r = preprocess_pifpaf (pifpaf_outs ['right' ], im_size )
232244 dic_out = net .forward (keypoints , kk , keypoints_r = keypoints_r )
245+ fwd_time = (time .time ()- start )* 1000
246+ timing .append (fwd_time )
233247 dic_out = net .post_process (
234248 dic_out , boxes , keypoints , kk , dic_gt )
235249
236- else :
237- dic_out = defaultdict (list )
238- kk = None
239-
240- # Outputs
250+ # Output
241251 factory_outputs (args , pifpaf_outs , dic_out , output_path , kk = kk )
242252 print (f'Image { cnt } \n ' + '-' * 120 )
243253 cnt += 1
254+ start = time .time ()
255+ timing = np .array (timing )
256+ avg_time = int (np .mean (timing ))
257+ std_time = int (np .std (timing ))
258+ print (f'Processed { idx * args .batch_size } images with an average time of { avg_time } ms and a std of { std_time } ms' )
244259
245260
246261def factory_outputs (args , pifpaf_outs , dic_out , output_path , kk = None ):
247- """Output json files or images according to the choice"""
248-
249- if 'social_distance' in args .activities :
250- assert args .mode == 'mono' , "Social distancing only works with monocular network"
262+ """
263+ Output json files or images according to the choice
264+ """
265+ if 'json' in args .output_types :
266+ with open (os .path .join (output_path + '.monoloco.json' ), 'w' ) as ff :
267+ json .dump (dic_out , ff )
268+ if len (args .output_types ) == 1 :
269+ return
251270
271+ with open (pifpaf_outs ['file_name' ], 'rb' ) as f :
272+ cpu_image = PIL .Image .open (f ).convert ('RGB' )
252273 if args .mode == 'keypoints' :
253274 annotation_painter = openpifpaf .show .AnnotationPainter ()
254- with openpifpaf .show .image_canvas (pifpaf_outs [ 'image' ] , output_path ) as ax :
275+ with openpifpaf .show .image_canvas (cpu_image , output_path ) as ax :
255276 annotation_painter .annotations (ax , pifpaf_outs ['pred' ])
256277 return
257278
258279 if any ((xx in args .output_types for xx in ['front' , 'bird' , 'multi' ])):
259280 LOG .info (output_path )
260281 if args .activities :
261282 show_activities (
262- args , pifpaf_outs [ 'image' ] , output_path , pifpaf_outs ['left' ], dic_out )
283+ args , cpu_image , output_path , pifpaf_outs ['left' ], dic_out )
263284 else :
264- printer = Printer (pifpaf_outs [ 'image' ] , output_path , kk , args )
285+ printer = Printer (cpu_image , output_path , kk , args )
265286 figures , axes = printer .factory_axes (dic_out )
266- printer .draw (figures , axes , pifpaf_outs ['image' ])
267-
268- if 'json' in args .output_types :
269- with open (os .path .join (output_path + '.monoloco.json' ), 'w' ) as ff :
270- json .dump (dic_out , ff )
287+ printer .draw (figures , axes , cpu_image , dic_out )
0 commit comments