@@ -148,7 +148,7 @@ def _get_inputs(self) -> tuple[list[str], ...]:
148148 )
149149 return image_blob_names , image_info_blob_names
150150
151- def preprocess (self , inputs : np .ndarray ) -> list [dict ]:
151+ def base_preprocess (self , inputs : np .ndarray ) -> list [dict ]:
152152 """Data preprocess method
153153
154154 It performs basic preprocessing of a single image:
@@ -173,35 +173,61 @@ def preprocess(self, inputs: np.ndarray) -> list[dict]:
173173 }
174174 - the input metadata, which might be used in `postprocess` method
175175 """
176- original_shape = inputs .shape
177-
178176 if self .params .embedded_processing :
179- processed_image = inputs [None ]
180- if self ._is_dynamic :
181- h , w , c = inputs .shape
182- resized_shape = (w , h , c )
183- else :
184- resized_shape = (self .w , self .h , self .c )
185- elif self ._is_dynamic :
177+ dict_inputs , meta = self ._preprocess_embedded (inputs )
178+ dict_inputs , meta = self .preprocess (dict_inputs , meta )
179+ return [dict_inputs , meta ]
180+
181+ # 1. Resize
182+ resized_image , meta = self ._resize_image (inputs )
183+
184+ # 2. Transform
185+ processed_image = self ._input_transform (resized_image )
186+
187+ # 3. Layout
188+ processed_image = self ._change_layout (processed_image )
189+
190+ # 4. Pack
191+ dict_inputs = {self .image_blob_name : processed_image }
192+
193+ # 5. Model-specific preprocess
194+ dict_inputs , meta = self .preprocess (dict_inputs , meta )
195+
196+ return [dict_inputs , meta ]
197+
198+ def _preprocess_embedded (self , inputs : np .ndarray ) -> tuple [dict , dict ]:
199+ original_shape = inputs .shape
200+ processed_image = inputs [None ]
201+ if self ._is_dynamic :
186202 h , w , c = inputs .shape
187203 resized_shape = (w , h , c )
188- processed_image = self .input_transform (inputs )
189- processed_image = self ._change_layout (processed_image )
190204 else :
191- # Fixed model without embedded preprocessing
192205 resized_shape = (self .w , self .h , self .c )
193206
194- resized_image = self .resize (inputs , (self .w , self .h ), pad_value = self .params .pad_value )
195- processed_image = self .input_transform (resized_image )
196- processed_image = self ._change_layout (processed_image )
197-
198- return [
207+ return (
199208 {self .image_blob_name : processed_image },
200209 {
201210 "original_shape" : original_shape ,
202211 "resized_shape" : resized_shape ,
203212 },
204- ]
213+ )
214+
215+ def _resize_image (self , image : np .ndarray ) -> tuple [np .ndarray , dict ]:
216+ original_shape = image .shape
217+ if self ._is_dynamic :
218+ h , w , c = image .shape
219+ resized_shape = (w , h , c )
220+ return image , {"original_shape" : original_shape , "resized_shape" : resized_shape }
221+
222+ resized_shape = (self .w , self .h , self .c )
223+ resized_image = self .resize (image , (self .w , self .h ), pad_value = self .params .pad_value )
224+ return resized_image , {"original_shape" : original_shape , "resized_shape" : resized_shape }
225+
226+ def _input_transform (self , image : np .ndarray ) -> np .ndarray :
227+ return self .input_transform (image )
228+
229+ def preprocess (self , dict_inputs : dict , meta : dict ) -> tuple [dict , dict ]:
230+ return dict_inputs , meta
205231
206232 def _change_layout (self , image : np .ndarray ) -> np .ndarray :
207233 """Changes the input image layout to fit the layout of the model input layer.
0 commit comments