From 62d4de22e8c38a6417b5de1b9c70054426d2f15b Mon Sep 17 00:00:00 2001 From: "Dr. Dennis Wittich" Date: Wed, 17 Sep 2025 15:05:13 +0200 Subject: [PATCH 1/2] use nodelib 0.17.1 and replace assertions by Exceptions --- detector/docker.sh | 2 +- detector/yolov5_detector.py | 16 ++++++++++------ detector_cpu/docker.sh | 4 ++-- detector_cpu/yolov5_detector.py | 26 ++++++++++++++++---------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/detector/docker.sh b/detector/docker.sh index 148ea93..20396c2 100755 --- a/detector/docker.sh +++ b/detector/docker.sh @@ -29,7 +29,7 @@ fi # ========================== BUILD CONFIGURATION / IMAGE SELECTION ======================= SEMANTIC_VERSION=0.1.13 -NODE_LIB_VERSION=0.17.0 +NODE_LIB_VERSION=0.17.1 build_args=" --build-arg NODE_LIB_VERSION=$NODE_LIB_VERSION" if [ -f /etc/nv_tegra_release ] # Check if we are on a Jetson device diff --git a/detector/yolov5_detector.py b/detector/yolov5_detector.py index 9a2a420..c916dc0 100644 --- a/detector/yolov5_detector.py +++ b/detector/yolov5_detector.py @@ -32,10 +32,12 @@ def __init__(self) -> None: self.conf_threshold = float(os.getenv('CONF_THRESHOLD', '0.2')) def init(self) -> None: - assert self.model_info is not None, 'model_info must be set before calling init()' - resolution = self.model_info.resolution - assert resolution is not None - engine_file = self._create_engine(resolution, + if self.model_info is None: + raise RuntimeError('Model info not initialized. Call load_model_info_and_init_model() first.') + if not isinstance(self.model_info.resolution, int) or self.model_info.resolution <= 0: + raise RuntimeError("input_size must be an integer > 0") + + engine_file = self._create_engine(self.model_info.resolution, len(self.model_info.categories), self.model_info.model_size, f'{self.model_info.model_root_path}/model.wts') @@ -87,8 +89,10 @@ def clip_point(x: float, y: float, img_width: int, img_height: int) -> Tuple[flo return x, y def evaluate(self, image: bytes) -> ImageMetadata: - assert self.yolov5 is not None, 'init() must be executed first. Maybe loading the engine failed?!' - assert self.model_info is not None, 'model_info must be set before calling evaluate()' + if self.yolov5 is None: + raise RuntimeError('init() must be executed first. Maybe loading the engine failed?!') + if self.model_info is None: + raise RuntimeError('model_info must be set before calling evaluate()') image_metadata = ImageMetadata() diff --git a/detector_cpu/docker.sh b/detector_cpu/docker.sh index 42e44c7..f8f0ed8 100755 --- a/detector_cpu/docker.sh +++ b/detector_cpu/docker.sh @@ -28,8 +28,8 @@ fi # ========================== BUILD CONFIGURATION / IMAGE SELECTION ======================= -SEMANTIC_VERSION=0.1.11 -NODE_LIB_VERSION=0.17.0 +SEMANTIC_VERSION=0.1.12 +NODE_LIB_VERSION=0.17.1 build_args=" --build-arg NODE_LIB_VERSION=$NODE_LIB_VERSION" diff --git a/detector_cpu/yolov5_detector.py b/detector_cpu/yolov5_detector.py index ff2e55a..16a4051 100644 --- a/detector_cpu/yolov5_detector.py +++ b/detector_cpu/yolov5_detector.py @@ -28,16 +28,20 @@ def __init__(self) -> None: self.conf_threshold = float(os.getenv('CONF_THRESHOLD', '0.2')) def init(self) -> None: - assert self.model_info is not None, 'model_info must be set before calling init()' - resolution = self.model_info.resolution - assert resolution is not None - pt_file = f'{self.model_info.model_root_path}/model.pt' + if self.model_info is None: + raise RuntimeError('Model info not initialized. Call load_model_info_and_init_model() first.') + if not isinstance(self.model_info.resolution, int) or self.model_info.resolution <= 0: + raise RuntimeError("input_size must be an integer > 0") + pt_file = f'{self.model_info.model_root_path}/model.pt' yolov5_path = os.path.join( os.path.dirname(__file__), 'app_code', 'yolov5') self.yolov5 = torch.hub.load( yolov5_path, 'custom', pt_file, source='local') - assert self.yolov5 is not None + + if self.yolov5 is None: + raise RuntimeError('Failed to load YOLOv5 model') + self.yolov5.eval() @staticmethod @@ -76,11 +80,14 @@ def clip_point(x: float, y: float, img_width: int, img_height: int) -> Tuple[flo return x, y def evaluate(self, image: bytes) -> ImageMetadata: - assert self.yolov5 is not None, 'init() must be executed first!' - assert self.model_info is not None, 'model_info must be set before calling evaluate()' + if self.yolov5 is None: + raise RuntimeError('Model not initialized. Call load_model_info_and_init_model() first.') + if self.model_info is None: + raise RuntimeError('Model info not initialized. Call load_model_info_and_init_model() first.') image_metadata = ImageMetadata() - assert self.model_info.resolution, "input_size must be an integer > 0" + if not isinstance(self.model_info.resolution, int) or self.model_info.resolution <= 0: + raise RuntimeError("input_size must be an integer > 0") self.input_size = self.model_info.resolution try: @@ -145,8 +152,7 @@ def evaluate(self, image: bytes) -> ImageMetadata: return image_metadata except Exception: - self.log.exception('inference failed') - return image_metadata + raise RuntimeError('Error during inference') from None def _preprocess_image(self, raw_bgr_image): """ From 9aa6fdf79547bb2bb4cb8a7cdd11b3c3759a71c4 Mon Sep 17 00:00:00 2001 From: "Dr. Dennis Wittich" Date: Wed, 17 Sep 2025 15:10:55 +0200 Subject: [PATCH 2/2] raise Exception during inference --- detector/yolov5_detector.py | 9 +++++---- detector_cpu/yolov5_detector.py | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/detector/yolov5_detector.py b/detector/yolov5_detector.py index c916dc0..373ef06 100644 --- a/detector/yolov5_detector.py +++ b/detector/yolov5_detector.py @@ -35,7 +35,7 @@ def init(self) -> None: if self.model_info is None: raise RuntimeError('Model info not initialized. Call load_model_info_and_init_model() first.') if not isinstance(self.model_info.resolution, int) or self.model_info.resolution <= 0: - raise RuntimeError("input_size must be an integer > 0") + raise RuntimeError("resolution must be an integer > 0") engine_file = self._create_engine(self.model_info.resolution, len(self.model_info.categories), @@ -134,9 +134,10 @@ def evaluate(self, image: bytes) -> ImageMetadata: if skipped_detections: log_msg = '\n'.join([str(d) for d in skipped_detections]) self.log.warning('Removed %d small detections from result: \n%s', len(skipped_detections), log_msg) - except Exception: - self.log.exception('inference failed') - return image_metadata + return image_metadata + + except Exception as e: + raise RuntimeError('Error during inference') from e def batch_evaluate(self, images: List[bytes]) -> ImagesMetadata: raise NotImplementedError('batch_evaluate is not implemented for Yolov5Detector') diff --git a/detector_cpu/yolov5_detector.py b/detector_cpu/yolov5_detector.py index 16a4051..ed252bb 100644 --- a/detector_cpu/yolov5_detector.py +++ b/detector_cpu/yolov5_detector.py @@ -31,7 +31,7 @@ def init(self) -> None: if self.model_info is None: raise RuntimeError('Model info not initialized. Call load_model_info_and_init_model() first.') if not isinstance(self.model_info.resolution, int) or self.model_info.resolution <= 0: - raise RuntimeError("input_size must be an integer > 0") + raise RuntimeError("model_info.resolution must be an integer > 0") pt_file = f'{self.model_info.model_root_path}/model.pt' yolov5_path = os.path.join( @@ -151,8 +151,8 @@ def evaluate(self, image: bytes) -> ImageMetadata: self.log.debug('took %f s', time.time() - t) return image_metadata - except Exception: - raise RuntimeError('Error during inference') from None + except Exception as e: + raise RuntimeError('Error during inference') from e def _preprocess_image(self, raw_bgr_image): """