perf(video): 优化视频处理性能监控和音频播放
- 添加视频处理性能计时和统计功能 - 实现帧处理时间监控和慢帧警告 - 添加音频文件静音修剪功能 - 优化Windows平台音频播放实现 - 调整默认日志输出频率减少冗余信息 - 修复MediaPipe GPU委托在Windows上的兼容性问题
This commit is contained in:
@@ -52,6 +52,7 @@ class DeadBugDetector:
|
||||
self._result_event = threading.Event()
|
||||
self._inflight = False
|
||||
self._inflight_started_at = 0.0
|
||||
self.last_timing: dict[str, float | bool] = {}
|
||||
|
||||
def on_result(pose_result, _image, _timestamp_ms):
|
||||
with self._result_lock:
|
||||
@@ -79,7 +80,9 @@ class DeadBugDetector:
|
||||
|
||||
def process_frame(self, bgr_frame: np.ndarray, timestamp_ms: int) -> tuple[np.ndarray, DeadBugResult]:
|
||||
"""处理单帧:姿态检测、指标计算、状态机更新、可视化叠加"""
|
||||
total_started = time.perf_counter()
|
||||
timestamp_ms = self._normalize_timestamp(timestamp_ms)
|
||||
normalize_done = time.perf_counter()
|
||||
|
||||
with self._result_lock:
|
||||
if self._inflight and time.monotonic() - self._inflight_started_at > 0.5:
|
||||
@@ -90,9 +93,11 @@ class DeadBugDetector:
|
||||
if should_submit:
|
||||
self._inflight = True
|
||||
self._inflight_started_at = time.monotonic()
|
||||
lock_done = time.perf_counter()
|
||||
|
||||
if should_submit:
|
||||
rgba_frame = bgr_to_rgba(bgr_frame)
|
||||
convert_done = time.perf_counter()
|
||||
mp_image = mp.Image(image_format=mp.ImageFormat.SRGBA, data=rgba_frame)
|
||||
self._result_event.clear()
|
||||
try:
|
||||
@@ -102,12 +107,20 @@ class DeadBugDetector:
|
||||
self._inflight = False
|
||||
self._inflight_started_at = 0.0
|
||||
raise
|
||||
submit_done = time.perf_counter()
|
||||
self._result_event.wait(timeout=0.08)
|
||||
wait_done = time.perf_counter()
|
||||
else:
|
||||
convert_done = lock_done
|
||||
submit_done = lock_done
|
||||
wait_done = lock_done
|
||||
|
||||
with self._result_lock:
|
||||
pose_result = self._latest_result
|
||||
result_read_done = time.perf_counter()
|
||||
|
||||
annotated = bgr_frame.copy()
|
||||
copy_done = time.perf_counter()
|
||||
|
||||
if pose_result is None or not pose_result.pose_landmarks:
|
||||
self._state.mark_no_pose()
|
||||
@@ -120,6 +133,18 @@ class DeadBugDetector:
|
||||
metrics=None,
|
||||
)
|
||||
draw_status_overlay(annotated, result)
|
||||
self._record_timing(
|
||||
total_started,
|
||||
normalize_done,
|
||||
lock_done,
|
||||
convert_done,
|
||||
submit_done,
|
||||
wait_done,
|
||||
result_read_done,
|
||||
copy_done,
|
||||
time.perf_counter(),
|
||||
should_submit,
|
||||
)
|
||||
return annotated, result
|
||||
|
||||
landmarks = [Point(lm.x, lm.y, lm.z, getattr(lm, "visibility", 1.0)) for lm in pose_result.pose_landmarks[0]]
|
||||
@@ -136,6 +161,18 @@ class DeadBugDetector:
|
||||
metrics=None,
|
||||
)
|
||||
draw_status_overlay(annotated, result)
|
||||
self._record_timing(
|
||||
total_started,
|
||||
normalize_done,
|
||||
lock_done,
|
||||
convert_done,
|
||||
submit_done,
|
||||
wait_done,
|
||||
result_read_done,
|
||||
copy_done,
|
||||
time.perf_counter(),
|
||||
should_submit,
|
||||
)
|
||||
return annotated, result
|
||||
|
||||
raw = calculate_metrics(
|
||||
@@ -169,8 +206,46 @@ class DeadBugDetector:
|
||||
|
||||
result = self._state.update(metrics)
|
||||
draw_status_overlay(annotated, result)
|
||||
self._record_timing(
|
||||
total_started,
|
||||
normalize_done,
|
||||
lock_done,
|
||||
convert_done,
|
||||
submit_done,
|
||||
wait_done,
|
||||
result_read_done,
|
||||
copy_done,
|
||||
time.perf_counter(),
|
||||
should_submit,
|
||||
)
|
||||
return annotated, result
|
||||
|
||||
def _record_timing(
|
||||
self,
|
||||
total_started: float,
|
||||
normalize_done: float,
|
||||
lock_done: float,
|
||||
convert_done: float,
|
||||
submit_done: float,
|
||||
wait_done: float,
|
||||
result_read_done: float,
|
||||
copy_done: float,
|
||||
finished: float,
|
||||
submitted: bool,
|
||||
) -> None:
|
||||
self.last_timing = {
|
||||
"total_ms": (finished - total_started) * 1000,
|
||||
"timestamp_ms": (normalize_done - total_started) * 1000,
|
||||
"lock_ms": (lock_done - normalize_done) * 1000,
|
||||
"convert_ms": (convert_done - lock_done) * 1000,
|
||||
"submit_ms": (submit_done - convert_done) * 1000,
|
||||
"wait_ms": (wait_done - submit_done) * 1000,
|
||||
"result_read_ms": (result_read_done - wait_done) * 1000,
|
||||
"copy_ms": (copy_done - result_read_done) * 1000,
|
||||
"postprocess_draw_ms": (finished - copy_done) * 1000,
|
||||
"submitted": submitted,
|
||||
}
|
||||
|
||||
def _normalize_timestamp(self, timestamp_ms: int) -> int:
|
||||
"""确保时间戳严格递增(MediaPipe要求)"""
|
||||
if timestamp_ms <= self._last_timestamp_ms:
|
||||
|
||||
Reference in New Issue
Block a user