From 1e4ef299f95c228f5e6efab61e4ea15237dd1c7c Mon Sep 17 00:00:00 2001 From: Oleksandr Bezdieniezhnykh Date: Thu, 26 Mar 2026 00:40:02 +0200 Subject: [PATCH] Refactor annotation handling and inference processing: Update 'time' attribute in Annotation class to private, streamline video time formatting in main processing function, and adjust annotations dictionary access in detection conversion. Ensure consistency in handling input frames during inference. --- annotation.pxd | 2 +- classes.json | 2 +- constants_inf.pxd | 2 +- constants_inf.pyx | 3 --- inference.pyx | 4 +--- main.py | 11 +++-------- 6 files changed, 7 insertions(+), 17 deletions(-) diff --git a/annotation.pxd b/annotation.pxd index c841d10..8bfc0bc 100644 --- a/annotation.pxd +++ b/annotation.pxd @@ -8,7 +8,7 @@ cdef class Detection: cdef class Annotation: cdef public str name cdef public str original_media_name - cdef public long time + cdef long time cdef public list[Detection] detections cdef public bytes image diff --git a/classes.json b/classes.json index 663844f..4ba2d0a 100644 --- a/classes.json +++ b/classes.json @@ -18,4 +18,4 @@ { "Id": 16, "Name": "Caponier", "ShortName": "Капонір", "Color": "#ffa500", "MaxSizeM": 10 }, { "Id": 17, "Name": "Ammo", "ShortName": "БК", "Color": "#33658a", "MaxSizeM": 2 }, { "Id": 18, "Name": "Protect.Struct", "ShortName": "Зуби.драк", "Color": "#969647", "MaxSizeM": 2 } -] +] \ No newline at end of file diff --git a/constants_inf.pxd b/constants_inf.pxd index 59fb7a0..f5573eb 100644 --- a/constants_inf.pxd +++ b/constants_inf.pxd @@ -21,7 +21,7 @@ cdef log(str log_message) cdef logerror(str error) cdef format_time(int ms) -cdef dict annotations_dict +cdef dict[int, AnnotationClass] annotations_dict cdef class AnnotationClass: cdef public int id diff --git a/constants_inf.pyx b/constants_inf.pyx index a94e6ef..4b515bf 100644 --- a/constants_inf.pyx +++ b/constants_inf.pyx @@ -70,9 +70,6 @@ logger.add( colorize=True ) -def get_annotations_dict(): - return annotations_dict - cdef log(str log_message): logger.info(log_message) diff --git a/inference.pyx b/inference.pyx index 81acee1..f6153f6 100644 --- a/inference.pyx +++ b/inference.pyx @@ -264,9 +264,7 @@ cdef class Inference: if frame is None: raise ValueError("Invalid image data") - cdef int bs = self.engine.get_batch_size() - frames = [frame] * bs - input_blob = self.preprocess(frames) + input_blob = self.preprocess([frame]) outputs = self.engine.run(input_blob) list_detections = self.postprocess(outputs, ai_config) if list_detections: diff --git a/main.py b/main.py index c3f0f26..5852c2f 100644 --- a/main.py +++ b/main.py @@ -109,10 +109,9 @@ class AIConfigDto(BaseModel): def detection_to_dto(det) -> DetectionDto: import constants_inf - ann = constants_inf.get_annotations_dict() label = "" - if det.cls in ann: - label = ann[det.cls].name + if det.cls in constants_inf.annotations_dict: + label = constants_inf.annotations_dict[det.cls].name return DetectionDto( centerX=det.x, centerY=det.y, @@ -178,14 +177,10 @@ def _post_annotation_to_service(token_mgr: TokenManager, media_id: str, try: token = token_mgr.get_valid_token() image_b64 = base64.b64encode(annotation.image).decode() if annotation.image else None - total_seconds = annotation.time // 1000 if annotation.time else 0 - hours = total_seconds // 3600 - minutes = (total_seconds % 3600) // 60 - seconds = total_seconds % 60 payload = { "mediaId": media_id, "source": 0, - "videoTime": f"{hours:02d}:{minutes:02d}:{seconds:02d}", + "videoTime": f"00:00:{annotation.time // 1000:02d}" if annotation.time else "00:00:00", "detections": [d.model_dump() for d in dtos], } if image_b64: