You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenCV摄像头拳头检测卡顿求助:需正常显示并延迟二次检测

Hey Jake, let's fix that webcam lag issue first— I bet the problem is that you're using a blocking sleep call (like time.sleep(2)) when you detect a fist. That freezes the entire loop, so your webcam can't capture new frames until the sleep finishes, hence the stutter.

Here's a better approach using timestamp tracking instead of blocking delays, which keeps your camera feed running smoothly while waiting for the 2-second window:

Step-by-Step Fix

First, forget about time.sleep()— we'll track when the fist was first detected, and check the elapsed time every frame without stopping the loop.

Example Code Structure (using OpenCV)

import cv2
import time

# Assume you have these functions already implemented:
# - is_fist_in_box(frame): returns True if fist is detected in your target box

def main():
    cap = cv2.VideoCapture(0)
    first_fist_time = None
    show_fist_text = False

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        # Check if fist exists in the target box
        fist_detected = is_fist_in_box(frame)

        if fist_detected:
            # Record the first moment we detect the fist
            if first_fist_time is None:
                first_fist_time = time.time()
            # Calculate how long we've seen the fist continuously
            elapsed_time = time.time() - first_fist_time
            # Trigger text display if 2 seconds have passed
            if elapsed_time >= 2:
                show_fist_text = True
        else:
            # Reset timer and text flag if fist disappears
            first_fist_time = None
            show_fist_text = False

        # Draw "FIST" text if the condition is met
        if show_fist_text:
            cv2.putText(frame, "FIST", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

        # Keep showing the live webcam feed
        cv2.imshow("Webcam Feed", frame)

        # Exit loop when 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

Why This Works

  • Instead of pausing the entire loop, we just track the timestamp of the first fist detection. Every frame, we verify if the fist is still present and if the 2-second window has passed.
  • The webcam keeps capturing new frames non-stop, so no lag or stutter in the feed.
  • If the fist disappears at any point before the 2 seconds are up, we reset the timer— exactly the reliable behavior you need.

Extra Tips to Prevent Lag

  1. Optimize your detection function: If is_fist_in_box() runs slowly (e.g., using a heavy model), try resizing the frame to a lower resolution before detection, or switch to a lighter object detection model (like MobileNet-based variants).
  2. Throttle detection frequency: If detection is still too resource-heavy, you could run the fist check every other frame instead of every single one— just adjust the timestamp logic to account for skipped frames if needed.

That should fix the stutter while keeping your 2-second delay logic fully functional!

内容的提问来源于stack exchange,提问作者Jake Lam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:56:26