0

我正在使用 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()

警报检测工作正常。但是,除了警报文本之外,我还尝试播放哔哔声警报声。然而,当人触摸限制的矩形区域时,音频播放会有很大的延迟,因为如果人一直触摸该区域,它会在每次迭代中播放。

我读过线程可能会有所帮助,但我无法使其工作。

4

1 回答 1

0

你的问题是它是连续播放的,这就是它滞后的原因。你可以做一件事,你可以使用 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())

如有错误请见谅

于 2021-05-08T15:23:10.340 回答