From a4b62d930b7294ab00d6c00db99c3f31bcb4fa88 Mon Sep 17 00:00:00 2001 From: hjwang <2392948297@qq.com> Date: Tue, 2 Jun 2026 09:40:22 +0800 Subject: [PATCH] Switch to LIVE_STREAM mode for real-time async inference --- dead_bug_detector.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/dead_bug_detector.py b/dead_bug_detector.py index 0707727..046625a 100644 --- a/dead_bug_detector.py +++ b/dead_bug_detector.py @@ -1,5 +1,6 @@ from __future__ import annotations +import threading from dataclasses import dataclass from enum import Enum from pathlib import Path @@ -106,9 +107,19 @@ class DeadBugDetector: self.extension_confirm_frames = extension_confirm_frames self.reset_confirm_frames = reset_confirm_frames + self._latest_result = None + self._result_lock = threading.Lock() + self._result_event = threading.Event() + + def on_result(pose_result, _image, _timestamp_ms): + with self._result_lock: + self._latest_result = pose_result + self._result_event.set() + options = PoseLandmarkerOptions( base_options=BaseOptions(model_asset_path=self.model_path), - running_mode=VisionRunningMode.VIDEO, + running_mode=VisionRunningMode.LIVE_STREAM, + result_callback=on_result, num_poses=1, min_pose_detection_confidence=0.5, min_pose_presence_confidence=0.5, @@ -131,7 +142,13 @@ class DeadBugDetector: timestamp_ms = self._normalize_timestamp(timestamp_ms) rgb_frame = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB) mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb_frame) - pose_result = self._landmarker.detect_for_video(mp_image, timestamp_ms) + + self._result_event.clear() + self._landmarker.detect_async(mp_image, timestamp_ms) + self._result_event.wait(timeout=0.1) + + with self._result_lock: + pose_result = self._latest_result annotated = bgr_frame.copy() if not pose_result.pose_landmarks: