1111import urllib .request
1212
1313import yaml
14- from PyQt5 .QtCore import QObject , QThread , pyqtSignal , pyqtSlot
15- from PyQt5 .QtCore import QCoreApplication
14+ from PyQt6 .QtCore import QObject , QThread , pyqtSignal , pyqtSlot
15+ from PyQt6 .QtCore import QCoreApplication
1616
1717from anylabeling .configs import auto_labeling as auto_labeling_configs
1818from anylabeling .services .auto_labeling .types import AutoLabelingResult
@@ -144,6 +144,16 @@ def set_output_mode(self, mode):
144144 @pyqtSlot ()
145145 def on_model_download_finished (self ):
146146 """Handle model download thread finished"""
147+ if self .model_download_thread :
148+ try :
149+ self .model_download_thread .quit ()
150+ if not self .model_download_thread .wait (1000 ):
151+ logging .warning ("Model download thread did not stop in time" )
152+ except RuntimeError :
153+ pass
154+ self .model_download_thread = None
155+ self .model_download_worker = None
156+
147157 if self .loaded_model_config and self .loaded_model_config ["model" ]:
148158 self .new_model_status .emit (self .tr ("Model loaded. Ready for labeling." ))
149159 self .model_loaded .emit (self .loaded_model_config )
@@ -269,6 +279,12 @@ def load_model(self, config_file):
269279 self .model_download_worker = GenericWorker (self ._load_model , model_id )
270280 self .model_download_worker .finished .connect (self .on_model_download_finished )
271281 self .model_download_worker .finished .connect (self .model_download_thread .quit )
282+ self .model_download_worker .finished .connect (
283+ self .model_download_worker .deleteLater
284+ )
285+ self .model_download_thread .finished .connect (
286+ self .model_download_thread .deleteLater
287+ )
272288 self .model_download_worker .moveToThread (self .model_download_thread )
273289 self .model_download_thread .started .connect (self .model_download_worker .run )
274290 self .model_download_thread .start ()
@@ -382,6 +398,7 @@ def _load_model(self, model_id):
382398 """Load and return model info"""
383399 if self .loaded_model_config is not None :
384400 self .loaded_model_config ["model" ].unload ()
401+ # If the model has a thread, it should have been joined in unload()
385402 self .loaded_model_config = None
386403 self .auto_segmentation_model_unselected .emit ()
387404
@@ -504,22 +521,25 @@ def predict_shapes_threading(self, image, filename=None):
504521 self .tr ("Model is not loaded. Choose a mode to continue." )
505522 )
506523 return
507- self .new_model_status .emit (self .tr ("Inferencing AI model. Please wait..." ))
508- self .prediction_started .emit ()
509- QCoreApplication .processEvents ()
510524
511525 with self .model_execution_thread_lock :
526+ # If a model is already running, try to stop it first
512527 if (
513528 self .model_execution_thread is not None
514529 and self .model_execution_thread .isRunning ()
515530 ):
516- self .new_model_status .emit (
517- self .tr (
518- "Another model is being executed. Please wait for it to finish."
519- )
520- )
521- self .prediction_finished .emit ()
522- return
531+ if hasattr (self .loaded_model_config ["model" ], "unload" ):
532+ self .loaded_model_config ["model" ].unload ()
533+
534+ # Wait for the thread to finish
535+ self .model_execution_thread .quit ()
536+ if not self .model_execution_thread .wait (1000 ):
537+ # If still running, we skip this request to avoid over-queuing
538+ self .prediction_finished .emit ()
539+ return
540+
541+ self .new_model_status .emit (self .tr ("Inferencing AI model. Please wait..." ))
542+ self .prediction_started .emit ()
523543
524544 self .model_execution_thread = QThread ()
525545 self .model_execution_worker = GenericWorker (
@@ -528,6 +548,15 @@ def predict_shapes_threading(self, image, filename=None):
528548 self .model_execution_worker .finished .connect (
529549 self .model_execution_thread .quit
530550 )
551+ self .model_execution_worker .finished .connect (
552+ self .model_execution_worker .deleteLater
553+ )
554+ self .model_execution_thread .finished .connect (
555+ lambda : setattr (self , "model_execution_thread" , None )
556+ )
557+ self .model_execution_thread .finished .connect (
558+ self .model_execution_thread .deleteLater
559+ )
531560 self .model_execution_worker .moveToThread (self .model_execution_thread )
532561 self .model_execution_thread .started .connect (self .model_execution_worker .run )
533562 self .model_execution_thread .start ()
0 commit comments