@@ -61,9 +61,7 @@ def __init__(self):
6161 def load_model_configs (self ):
6262 """Load model configs"""
6363 # Load list of default models
64- with pkg_resources .open_text (
65- auto_labeling_configs , "models.yaml"
66- ) as f :
64+ with pkg_resources .open_text (auto_labeling_configs , "models.yaml" ) as f :
6765 model_list = yaml .safe_load (f )
6866 for model in model_list :
6967 model ["is_custom_model" ] = False
@@ -74,9 +72,7 @@ def load_model_configs(self):
7472 model_download_path = os .path .join (
7573 home_dir , "anylabeling_data" , "models" , model ["name" ]
7674 )
77- pathlib .Path (model_download_path ).mkdir (
78- parents = True , exist_ok = True
79- )
75+ pathlib .Path (model_download_path ).mkdir (parents = True , exist_ok = True )
8076 config_file = os .path .join (model_download_path , "config.yaml" )
8177 model ["config_file" ] = config_file
8278
@@ -126,9 +122,7 @@ def load_model_configs(self):
126122 if not model_config .get ("is_custom_model" , False ):
127123 model_config ["last_used" ] = - i
128124 else :
129- model_config ["last_used" ] = model_config .get (
130- "last_used" , time .time ()
131- )
125+ model_config ["last_used" ] = model_config .get ("last_used" , time .time ())
132126 model_configs .sort (key = lambda x : x .get ("last_used" , 0 ), reverse = True )
133127
134128 self .model_configs = model_configs
@@ -147,9 +141,7 @@ def set_output_mode(self, mode):
147141 def on_model_download_finished (self ):
148142 """Handle model download thread finished"""
149143 if self .loaded_model_config and self .loaded_model_config ["model" ]:
150- self .new_model_status .emit (
151- self .tr ("Model loaded. Ready for labeling." )
152- )
144+ self .new_model_status .emit (self .tr ("Model loaded. Ready for labeling." ))
153145 self .model_loaded .emit (self .loaded_model_config )
154146 self .output_modes_changed .emit (
155147 self .loaded_model_config ["model" ].Meta .output_modes ,
@@ -165,9 +157,7 @@ def load_custom_model(self, config_file):
165157 self .model_download_thread is not None
166158 and self .model_download_thread .isRunning ()
167159 ):
168- print (
169- "Another model is being loaded. Please wait for it to finish."
170- )
160+ print ("Another model is being loaded. Please wait for it to finish." )
171161 return
172162
173163 # Check config file path
@@ -191,33 +181,26 @@ def load_custom_model(self, config_file):
191181 "type" not in model_config
192182 or "display_name" not in model_config
193183 or "name" not in model_config
194- or model_config ["type" ]
195- not in ["segment_anything" , "yolov5" , "yolov8" ]
184+ or model_config ["type" ] not in ["segment_anything" , "yolov5" , "yolov8" ]
196185 ):
197186 self .new_model_status .emit (
198- self .tr (
199- "Error in loading custom model: Invalid config file format."
200- )
187+ self .tr ("Error in loading custom model: Invalid config file format." )
201188 )
202189 return
203190
204191 # Add or replace custom model
205192 custom_models = get_config ().get ("custom_models" , [])
206193 matched_index = None
207194 for i , model in enumerate (custom_models ):
208- if os .path .normpath (model ["config_file" ]) == os .path .normpath (
209- config_file
210- ):
195+ if os .path .normpath (model ["config_file" ]) == os .path .normpath (config_file ):
211196 matched_index = i
212197 break
213198 if matched_index is not None :
214199 model_config ["last_used" ] = time .time ()
215200 custom_models [matched_index ] = model_config
216201 else :
217202 if len (custom_models ) >= self .MAX_NUM_CUSTOM_MODELS :
218- custom_models .sort (
219- key = lambda x : x .get ("last_used" , 0 ), reverse = True
220- )
203+ custom_models .sort (key = lambda x : x .get ("last_used" , 0 ), reverse = True )
221204 removed_model = custom_models .pop ()
222205 # Remove old model folder
223206 config_file = removed_model ["config_file" ]
@@ -245,9 +228,7 @@ def load_model(self, config_file):
245228 self .model_download_thread is not None
246229 and self .model_download_thread .isRunning ()
247230 ):
248- print (
249- "Another model is being loaded. Please wait for it to finish."
250- )
231+ print ("Another model is being loaded. Please wait for it to finish." )
251232 return
252233 if not config_file :
253234 if self .model_download_worker is not None :
@@ -280,16 +261,10 @@ def load_model(self, config_file):
280261 )
281262 )
282263 self .model_download_worker = GenericWorker (self ._load_model , model_id )
283- self .model_download_worker .finished .connect (
284- self .on_model_download_finished
285- )
286- self .model_download_worker .finished .connect (
287- self .model_download_thread .quit
288- )
264+ self .model_download_worker .finished .connect (self .on_model_download_finished )
265+ self .model_download_worker .finished .connect (self .model_download_thread .quit )
289266 self .model_download_worker .moveToThread (self .model_download_thread )
290- self .model_download_thread .started .connect (
291- self .model_download_worker .run
292- )
267+ self .model_download_thread .started .connect (self .model_download_worker .run )
293268 self .model_download_thread .start ()
294269
295270 def _download_and_extract_model (self , model_config ):
@@ -313,22 +288,16 @@ def _download_and_extract_model(self, model_config):
313288 # Download url
314289 ellipsis_download_url = download_url
315290 if len (download_url ) > 40 :
316- ellipsis_download_url = (
317- download_url [:20 ] + "..." + download_url [- 20 :]
318- )
319- logging .info (
320- "Downloading %s to %s" , ellipsis_download_url , zip_model_path
321- )
291+ ellipsis_download_url = download_url [:20 ] + "..." + download_url [- 20 :]
292+ logging .info ("Downloading %s to %s" , ellipsis_download_url , zip_model_path )
322293 try :
323294 # Download and show progress
324295 def _progress (count , block_size , total_size ):
325296 percent = int (count * block_size * 100 / total_size )
326297 self .new_model_status .emit (
327298 QCoreApplication .translate (
328299 "Model" , "Downloading {download_url}: {percent}%"
329- ).format (
330- download_url = ellipsis_download_url , percent = percent
331- )
300+ ).format (download_url = ellipsis_download_url , percent = percent )
332301 )
333302
334303 urllib .request .urlretrieve (
@@ -352,9 +321,7 @@ def _progress(count, block_size, total_size):
352321 model_folder = root
353322 break
354323 if model_folder is None :
355- raise ValueError (
356- self .tr ("Could not find config.yaml in zip file." )
357- )
324+ raise ValueError (self .tr ("Could not find config.yaml in zip file." ))
358325
359326 # Move model folder to correct location
360327 shutil .rmtree (extract_dir )
@@ -494,9 +461,9 @@ def predict_shapes(self, image, filename=None):
494461 self .prediction_finished .emit ()
495462 return
496463 try :
497- auto_labeling_result = self .loaded_model_config [
498- "model"
499- ]. predict_shapes ( image , filename )
464+ auto_labeling_result = self .loaded_model_config ["model" ]. predict_shapes (
465+ image , filename
466+ )
500467 self .new_auto_labeling_result .emit (auto_labeling_result )
501468 except Exception as e : # noqa
502469 print (f"Error in predict_shapes: { e } " )
@@ -518,9 +485,7 @@ def predict_shapes_threading(self, image, filename=None):
518485 self .tr ("Model is not loaded. Choose a mode to continue." )
519486 )
520487 return
521- self .new_model_status .emit (
522- self .tr ("Inferencing AI model. Please wait..." )
523- )
488+ self .new_model_status .emit (self .tr ("Inferencing AI model. Please wait..." ))
524489 self .prediction_started .emit ()
525490
526491 with self .model_execution_thread_lock :
@@ -530,8 +495,7 @@ def predict_shapes_threading(self, image, filename=None):
530495 ):
531496 self .new_model_status .emit (
532497 self .tr (
533- "Another model is being executed."
534- " Please wait for it to finish."
498+ "Another model is being executed. Please wait for it to finish."
535499 )
536500 )
537501 self .prediction_finished .emit ()
@@ -544,12 +508,8 @@ def predict_shapes_threading(self, image, filename=None):
544508 self .model_execution_worker .finished .connect (
545509 self .model_execution_thread .quit
546510 )
547- self .model_execution_worker .moveToThread (
548- self .model_execution_thread
549- )
550- self .model_execution_thread .started .connect (
551- self .model_execution_worker .run
552- )
511+ self .model_execution_worker .moveToThread (self .model_execution_thread )
512+ self .model_execution_thread .started .connect (self .model_execution_worker .run )
553513 self .model_execution_thread .start ()
554514
555515 def on_next_files_changed (self , next_files ):
0 commit comments