Switch to LIVE_STREAM mode for real-time async inference
This commit is contained in:
+19
-2
@@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import threading
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -106,9 +107,19 @@ class DeadBugDetector:
|
|||||||
self.extension_confirm_frames = extension_confirm_frames
|
self.extension_confirm_frames = extension_confirm_frames
|
||||||
self.reset_confirm_frames = reset_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(
|
options = PoseLandmarkerOptions(
|
||||||
base_options=BaseOptions(model_asset_path=self.model_path),
|
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,
|
num_poses=1,
|
||||||
min_pose_detection_confidence=0.5,
|
min_pose_detection_confidence=0.5,
|
||||||
min_pose_presence_confidence=0.5,
|
min_pose_presence_confidence=0.5,
|
||||||
@@ -131,7 +142,13 @@ class DeadBugDetector:
|
|||||||
timestamp_ms = self._normalize_timestamp(timestamp_ms)
|
timestamp_ms = self._normalize_timestamp(timestamp_ms)
|
||||||
rgb_frame = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB)
|
rgb_frame = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB)
|
||||||
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb_frame)
|
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()
|
annotated = bgr_frame.copy()
|
||||||
if not pose_result.pose_landmarks:
|
if not pose_result.pose_landmarks:
|
||||||
|
|||||||
Reference in New Issue
Block a user