[Python] OpenCV Callback Lag

Discussão em 'Python' iniciado por Stack, Setembro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm running OpenCV 3 on Python 3.4 on a 2015 15" MacBook Pro. Below is a minimal example that illustrates my problem:

    import cv2 as cv
    import numpy as np

    def mouse_callback(event, x, y, flags, param):
    print("Callback!")

    cv.namedWindow("Display")
    cv.setMouseCallback("Display", mouse_callback)

    cap = cv.VideoCapture(0)
    while True:
    ret, frame = cap.read()
    cv.imshow("Display", frame)

    if cv.waitKey(1) == ord("q"):
    break


    When I click on the screen, the text "Callback!" takes about 3 seconds to appear on the terminal screen. I'm not sure why I'm seeing this much lag--my laptop shouldn't be so bad that I can't even run this simple script.

    Additionally, the problem persists when I reduce the webcam resolution, or even when I replace the webcam altogether with a still image. I rewrote a similar program in C++, and the C++ OpenCV libraries also suffer from this lag.

    Any tips on how I can reduce or eliminate the lag?

    Continue reading...

Compartilhe esta Página