fix switcher between modes in DatasetExplorer.xaml

This commit is contained in:
Alex Bezdieniezhnykh
2025-03-02 21:32:31 +02:00
parent 227d01ba5e
commit d93da15528
15 changed files with 141 additions and 55 deletions
+24 -20
View File
@@ -20,7 +20,6 @@ cdef class Inference:
self.model_width = 0
self.model_height = 0
self.class_names = None
self.ai_config = AIRecognitionConfig(4, 2, 0.25, 0.15, 15, 0.8, b'', [])
def init_ai(self):
model_bytes = self.api_client.load_ai_model()
@@ -47,7 +46,7 @@ cdef class Inference:
for frame in frames]
return np.vstack(blobs)
cdef postprocess(self, output):
cdef postprocess(self, output, ai_config):
cdef list[Detection] detections = []
cdef int ann_index
cdef float x1, y1, x2, y2, conf, cx, cy, w, h
@@ -70,7 +69,8 @@ cdef class Inference:
y = (y1 + y2) / 2
w = x2 - x1
h = y2 - y1
detections.append(Detection(x, y, w, h, class_id, conf))
if conf >= ai_config.probability_threshold:
detections.append(Detection(x, y, w, h, class_id, conf))
filtered_detections = self.remove_overlapping_detections(detections)
results.append(filtered_detections)
return results
@@ -116,30 +116,32 @@ cdef class Inference:
cdef run_inference(self, RemoteCommand cmd):
cdef list[str] videos = []
cdef list[str] images = []
cdef AIRecognitionConfig ai_config = AIRecognitionConfig.from_msgpack(cmd.data)
if ai_config is None:
raise Exception('ai recognition config is empty')
self.ai_config = AIRecognitionConfig.from_msgpack(cmd.data)
self.stop_signal = False
if self.session is None:
self.init_ai()
for m in self.ai_config.paths:
print(ai_config.paths)
for m in ai_config.paths:
if self.is_video(m):
videos.append(m)
else:
images.append(m)
# images first, it's faster
if len(images) > 0:
for chunk in self.split_list_extend(images, constants.MODEL_BATCH_SIZE):
print(f'run inference on {" ".join(chunk)}...')
self._process_images(cmd, chunk)
self._process_images(cmd, ai_config, chunk)
if len(videos) > 0:
for v in videos:
print(f'run inference on {v}...')
self._process_video(cmd, v)
self._process_video(cmd, ai_config, v)
cdef _process_video(self, RemoteCommand cmd, str video_name):
cdef _process_video(self, RemoteCommand cmd, AIRecognitionConfig ai_config, str video_name):
cdef int frame_count = 0
cdef list batch_frames = []
cdef list[int] batch_timestamps = []
@@ -152,30 +154,32 @@ cdef class Inference:
break
frame_count += 1
if frame_count % self.ai_config.frame_period_recognition == 0:
if frame_count % ai_config.frame_period_recognition == 0:
batch_frames.append(frame)
batch_timestamps.append(int(v_input.get(cv2.CAP_PROP_POS_MSEC)))
if len(batch_frames) == constants.MODEL_BATCH_SIZE:
input_blob = self.preprocess(batch_frames)
outputs = self.session.run(None, {self.model_input: input_blob})
list_detections = self.postprocess(outputs)
list_detections = self.postprocess(outputs, ai_config)
for i in range(len(list_detections)):
detections = list_detections[i]
annotation = Annotation(video_name, batch_timestamps[i], detections)
if self.is_valid_annotation(annotation):
_, image = cv2.imencode('.jpg', frame)
if self.is_valid_annotation(annotation, ai_config):
_, image = cv2.imencode('.jpg', batch_frames[i])
annotation.image = image.tobytes()
self._previous_annotation = annotation
print(annotation.to_str(self.class_names))
self.on_annotation(cmd, annotation)
self._previous_annotation = annotation
batch_frames.clear()
batch_timestamps.clear()
v_input.release()
cdef _process_images(self, RemoteCommand cmd, list[str] image_paths):
cdef _process_images(self, RemoteCommand cmd, AIRecognitionConfig ai_config, list[str] image_paths):
cdef list frames = []
cdef list timestamps = []
self._previous_annotation = None
@@ -186,7 +190,7 @@ cdef class Inference:
input_blob = self.preprocess(frames)
outputs = self.session.run(None, {self.model_input: input_blob})
list_detections = self.postprocess(outputs)
list_detections = self.postprocess(outputs, ai_config)
for i in range(len(list_detections)):
detections = list_detections[i]
annotation = Annotation(image_paths[i], timestamps[i], detections)
@@ -198,7 +202,7 @@ cdef class Inference:
cdef stop(self):
self.stop_signal = True
cdef bint is_valid_annotation(self, Annotation annotation):
cdef bint is_valid_annotation(self, Annotation annotation, AIRecognitionConfig ai_config):
# No detections, invalid
if not annotation.detections:
return False
@@ -208,7 +212,7 @@ cdef class Inference:
return True
# Enough time has passed since last annotation
if annotation.time >= self._previous_annotation.time + <long>(self.ai_config.frame_recognition_seconds * 1000):
if annotation.time >= self._previous_annotation.time + <long>(ai_config.frame_recognition_seconds * 1000):
return True
# More objects detected than before
@@ -236,11 +240,11 @@ cdef class Inference:
closest_det = prev_det
# Check if beyond tracking distance
if min_distance_sq > self.ai_config.tracking_distance_confidence:
if min_distance_sq > ai_config.tracking_distance_confidence:
return True
# Check probability increase
if current_det.confidence >= closest_det.confidence + self.ai_config.tracking_probability_increase:
if current_det.confidence >= closest_det.confidence + ai_config.tracking_probability_increase:
return True
return False