@@ -51,14 +51,14 @@ def track_dataset(self):
5151 self .callback ("on_dataset_track_start" )
5252 self .callback (
5353 "on_video_loop_start" ,
54- video_metadata = pd .Series (name = self .video_filename ),
54+ video_metadata = pd .Series ({ " name" : self .video_filename } ),
5555 video_idx = 0 ,
5656 index = 0 ,
5757 )
5858 detections = self .video_loop ()
5959 self .callback (
6060 "on_video_loop_end" ,
61- video_metadata = pd .Series (name = self .video_filename ),
61+ video_metadata = pd .Series ({ " name" : self .video_filename } ),
6262 video_idx = 0 ,
6363 detections = detections ,
6464 )
@@ -81,6 +81,11 @@ def video_loop(self):
8181 # print('in offline.py, model_names: ', model_names)
8282 frame_idx = - 1
8383 detections = pd .DataFrame ()
84+
85+ # Initialize module callbacks at the start
86+ for model_name in model_names :
87+ self .callback ("on_module_start" , task = model_name , dataloader = [])
88+
8489 while video_cap .isOpened ():
8590 frame_idx += 1
8691 ret , frame = video_cap .read ()
@@ -89,10 +94,13 @@ def video_loop(self):
8994 image = cv2 .cvtColor (frame , cv2 .COLOR_BGR2RGB )
9095 if not ret :
9196 break
92- metadata = pd .Series ({"id" : frame_idx , "frame" : frame_idx ,
97+ base_metadata = pd .Series ({"id" : frame_idx , "frame" : frame_idx ,
9398 "video_id" : video_filename }, name = frame_idx )
9499 self .callback ("on_image_loop_start" ,
95- image_metadata = metadata , image_idx = frame_idx , index = frame_idx )
100+ image_metadata = base_metadata , image_idx = frame_idx , index = frame_idx )
101+
102+ image_metadata = pd .DataFrame ([base_metadata ])
103+
96104 for model_name in model_names :
97105 model = self .models [model_name ]
98106 if len (detections ) > 0 :
@@ -102,49 +110,64 @@ def video_loop(self):
102110 if model .level == "video" :
103111 raise "Video-level not supported for online video tracking"
104112 elif model .level == "image" :
105- batch = model .preprocess (image = image , detections = dets , metadata = metadata )
113+ batch = model .preprocess (image = image , detections = dets , metadata = image_metadata . iloc [ 0 ] )
106114 batch = type (model ).collate_fn ([(frame_idx , batch )])
107- detections = self .default_step (batch , model_name , detections , metadata )
115+ detections , image_metadata = self .default_step (batch , model_name , detections , image_metadata )
108116 elif model .level == "detection" :
109117 for idx , detection in dets .iterrows ():
110- batch = model .preprocess (image = image , detection = detection , metadata = metadata )
118+ batch = model .preprocess (image = image , detection = detection , metadata = image_metadata . iloc [ 0 ] )
111119 batch = type (model ).collate_fn ([(detection .name , batch )])
112- detections = self .default_step (batch , model_name , detections , metadata )
120+ detections , image_metadata = self .default_step (batch , model_name , detections , image_metadata )
121+
113122 self .callback ("on_image_loop_end" ,
114- image_metadata = metadata , image = image ,
123+ image_metadata = image_metadata . iloc [ 0 ] , image = image ,
115124 image_idx = frame_idx , detections = detections )
116125
126+ # Finalize module callbacks at the end
127+ for model_name in model_names :
128+ self .callback ("on_module_end" , task = model_name , detections = detections )
129+
117130 return detections
118131
119- def default_step (self , batch : Any , task : str , detections : pd .DataFrame , metadata , ** kwargs ):
132+ def default_step (self , batch : Any , task : str , detections : pd .DataFrame , image_pred : pd . DataFrame , ** kwargs ):
120133 model = self .models [task ]
121134 self .callback (f"on_module_step_start" , task = task , batch = batch )
122135 idxs , batch = batch
123136 idxs = idxs .cpu () if isinstance (idxs , torch .Tensor ) else idxs
124137 if model .level == "image" :
125- log .info (f"step : { idxs } " )
126- batch_metadatas = pd . DataFrame ([ metadata ])
138+ log .info (f"step : { idxs } --- task : { task } " )
139+ batch_metadatas = image_pred . loc [ list ( idxs )] # self.img_metadatas.loc[idxs]
127140 if len (detections ) > 0 :
128141 batch_input_detections = detections .loc [
129142 np .isin (detections .image_id , batch_metadatas .index )
130143 ]
131144 else :
132145 batch_input_detections = detections
146+
133147 batch_detections = self .models [task ].process (
134148 batch ,
135149 batch_input_detections ,
136150 batch_metadatas )
137151 else :
138152 batch_detections = detections .loc [idxs ]
153+ if not image_pred .empty :
154+ batch_metadatas = image_pred .loc [np .isin (image_pred .index , batch_detections .image_id )]
155+ else :
156+ batch_metadatas = image_pred
139157 batch_detections = self .models [task ].process (
140158 batch = batch ,
141159 detections = batch_detections ,
142- metadatas = None ,
160+ metadatas = batch_metadatas ,
143161 ** kwargs ,
144162 )
163+
164+ if isinstance (batch_detections , tuple ):
165+ batch_detections , batch_metadatas = batch_detections
166+ image_pred = merge_dataframes (image_pred , batch_metadatas )
167+
145168 detections = merge_dataframes (detections , batch_detections )
169+
146170 self .callback (
147171 f"on_module_step_end" , task = task , batch = batch , detections = detections
148172 )
149- return detections
150-
173+ return detections , image_pred
0 commit comments