@@ -84,18 +84,21 @@ class YOLO:
8484 model = FakeInnerModel ()
8585
8686 def __call__ (self , source = None , stream = False , ** kwargs ):
87+ return self .predict (source , stream = stream , ** kwargs )
88+
89+ def predict (self , source = None , stream = False , ** kwargs ):
8790 return [FakeResult ([(10 , 20 , 100 , 200 , 0.9 , 0 )])]
8891
8992
9093class FailingYOLO (YOLO ):
91- def __call__ (self , source = None , stream = False , ** kwargs ):
94+ def predict (self , source = None , stream = False , ** kwargs ):
9295 raise RuntimeError ("cuda oom" )
9396
9497
9598class ClassifyYOLO (YOLO ):
9699 task = "classify"
97100
98- def __call__ (self , source = None , stream = False , ** kwargs ):
101+ def predict (self , source = None , stream = False , ** kwargs ):
99102 return [FakeClassifyResult ()]
100103
101104
@@ -464,3 +467,36 @@ def test_unsupported_ultralytics_version_warns_and_does_not_patch(monkeypatch, c
464467 assert fake_yolo .__init__ is original_init
465468 messages = [r .getMessage () for r in caplog .records ]
466469 assert any ("ultralytics 8.0.0 is older than 8.0.200" in m for m in messages )
470+
471+
472+ @pytest .mark .parametrize ("entrypoint" , ["predict" , "__call__" ])
473+ def test_predict_and_call_track_once_with_repeated_registration (
474+ publish_spy , entrypoint
475+ ):
476+ model = YOLO ()
477+ handle = make_handle (publish_spy )
478+ _extractor .install_hooks (model , handle )
479+ _extractor .install_hooks (model , handle )
480+ results = getattr (model , entrypoint )(source = "image.png" , verbose = False )
481+ assert len (results ) == 1
482+ assert len (publish_spy .events ) == 1
483+ assert publish_spy .events [0 ]["event_type" ] == "inference"
484+ assert publish_spy .events [0 ]["inference" ]["output_meta" ]["num_predictions" ] == 1
485+
486+
487+ def test_predict_error_is_preserved_and_tracked_once (publish_spy ):
488+ model = FailingYOLO ()
489+ _extractor .install_hooks (model , make_handle (publish_spy ))
490+ with pytest .raises (RuntimeError , match = "cuda oom" ):
491+ model .predict (source = "image.png" )
492+ assert len (publish_spy .events ) == 1
493+ assert publish_spy .events [0 ]["event_type" ] == "error"
494+
495+
496+ def test_predict_does_not_instrument_unregistered_instances (publish_spy ):
497+ tracked , untouched = YOLO (), YOLO ()
498+ _extractor .install_hooks (tracked , make_handle (publish_spy ))
499+ untouched .predict ()
500+ assert not publish_spy .events
501+ tracked .predict ()
502+ assert len (publish_spy .events ) == 1
0 commit comments