fix split tile size

rework inference events and handling
todo: add Medias table and reflect recognition status there
This commit is contained in:
Oleksandr Bezdieniezhnykh
2025-09-05 16:50:09 +03:00
parent 7d68f7faee
commit 9a16099194
14 changed files with 86 additions and 47 deletions
+26 -9
View File
@@ -1,13 +1,12 @@
import mimetypes
import time
from pathlib import Path
import cv2
import msgpack
import numpy as np
cimport constants_inf
from ai_availability_status cimport AIAvailabilityEnum, AIAvailabilityStatus
from remote_command_inf cimport RemoteCommand
from annotation cimport Detection, Annotation
from ai_config cimport AIRecognitionConfig
import pynvml
@@ -60,6 +59,7 @@ cdef class Inference:
self.model_input = None
self.model_width = 0
self.model_height = 0
self.detection_counts = {}
self.engine = None
self.is_building_engine = False
self.ai_availability_status = AIAvailabilityStatus()
@@ -233,14 +233,17 @@ cdef class Inference:
if self.engine is None:
constants_inf.log(<str> "AI engine not available. Conversion may be in progress. Skipping inference.")
response = RemoteCommand(CommandType.AI_AVAILABILITY_RESULT, self.ai_availability_status.serialize())
self.remote_handler.send(cmd.client_id, response.serialize())
self.remote_handler.send(cmd.client_id, <RemoteCommand>response)
return
for m in ai_config.paths:
if self.is_video(m):
videos.append(m)
self.detection_counts = {}
for p in ai_config.paths:
media_name = Path(<str>p).stem.replace(" ", "")
self.detection_counts[media_name] = 0
if self.is_video(p):
videos.append(p)
else:
images.append(m)
images.append(p)
# images first, it's faster
if len(images) > 0:
constants_inf.log(<str>f'run inference on {" ".join(images)}...')
@@ -295,7 +298,7 @@ cdef class Inference:
cdef on_annotation(self, RemoteCommand cmd, Annotation annotation):
cdef RemoteCommand response = RemoteCommand(CommandType.INFERENCE_DATA, annotation.serialize())
self.remote_handler.send(cmd.client_id, response.serialize())
self.remote_handler.send(cmd.client_id, response)
cdef _process_images(self, RemoteCommand cmd, AIRecognitionConfig ai_config, list[str] image_paths):
cdef list frame_data
@@ -345,14 +348,16 @@ cdef class Inference:
y = img_h - tile_size
tile = frame[y:y_end, x:x_end]
name = f'{original_media_name}{constants_inf.SPLIT_SUFFIX}{tile_size:04d}{x:04d}_{y:04d}!_000000'
name = f'{original_media_name}{constants_inf.SPLIT_SUFFIX}{tile_size:04d}_{x:04d}_{y:04d}!_000000'
results.append((tile, original_media_name, name))
return results
cdef _process_images_inner(self, RemoteCommand cmd, AIRecognitionConfig ai_config, list frame_data):
cdef list frames, original_media_names, names
cdef Annotation annotation
cdef int i
frames, original_media_names, names = map(list, zip(*frame_data))
input_blob = self.preprocess(frames)
outputs = self.engine.run(input_blob)
@@ -360,10 +365,22 @@ cdef class Inference:
for i in range(len(list_detections)):
annotation = Annotation(names[i], original_media_names[i], 0, list_detections[i])
if self.is_valid_image_annotation(annotation):
constants_inf.log(<str> f'Detected {annotation}')
_, image = cv2.imencode('.jpg', frames[i])
annotation.image = image.tobytes()
self.on_annotation(cmd, annotation)
self.detection_counts[original_media_names[i]] = self.detection_counts.get(original_media_names[i], 0) + 1
# Send detecting status for each original media name
for media_name in self.detection_counts.keys():
try:
status = {
"mn": media_name,
"dc": self.detection_counts[media_name]
}
self.remote_handler.send(cmd.client_id, <RemoteCommand>RemoteCommand(CommandType.INFERENCE_STATUS, msgpack.packb(status)))
except Exception:
pass
cdef stop(self):
self.stop_signal = True