Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用OpenCV和Python线程避免音频延迟?_Python_Multithreading_Opencv_Winsound - Fatal编程技术网

如何使用OpenCV和Python线程避免音频延迟?

如何使用OpenCV和Python线程避免音频延迟?,python,multithreading,opencv,winsound,Python,Multithreading,Opencv,Winsound,我正在用OpenCV和Python实现报警系统 我有以下资料: import cv2 import winsound import threading # Tracker and camera configuration # ... def beep(): winsound.Beep(frequency=2500, duration=1000) try: while True: # Grab frame from webcam # ...

我正在用OpenCV和Python实现报警系统

我有以下资料:

import cv2
import winsound
import threading

# Tracker and camera configuration
# ...


def beep():
    winsound.Beep(frequency=2500, duration=1000)


try:
    while True:
        # Grab frame from webcam
        # ...

        success, bbox = tracker.update(colorFrame)

        # Draw bounding box
        if success:
            # Tracking success
            (x, y, w, h) = [int(p) for p in bbox]
            cv2.rectangle(colorFrame, (x, y), (x + w, y + h), (0, 255, 0), 2, 1)
         
            if alarm_condition(x, y, w, h):   # if bbox coordinates are touching restricted area
                text = "Alarm"
                threading.Thread(beep())

        # Show images
        cv2.imshow('Color Frame', colorFrame)

        # Record if the user presses a key
        key = cv2.waitKey(1) & 0xFF

        # if the `q` key is pressed, break from the lop
        if key == ord("q"):
            break

finally:

    # cleanup the camera and close any open windows
    vid.release()
    cv2.destroyAllWindows()
警报检测工作正常。然而,我试图在警报文本之外播放一声嘟嘟的警报声。然而,当人接触到受限矩形区域时,音频播放会有很大的延迟,因为如果人一直接触该区域,音频会在每次迭代中播放


我已经读到线程技术可能会有帮助,但我无法让它工作。

您的问题是它会连续播放,所以它会滞后。你可以做一件事,你可以使用datetime检查声音是否最近播放,如果在一分钟后播放

范例

import datetime
import threading
import winsound

def beep():
    winsound.Beep(frequency=2500, duration=1000)

recently = []
if(condition):
    c_time = datetime.datetime.now().strftime('%M')
    if not recently:
        recently.append(c_time)
        threading.Thread(beep())
    elif recently[-1] == c_time:
        continue
    else:
        recently.append(c_time)
        threading.Thread(beep())
如果有任何错误,请道歉