我正在使用 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()
警报检测工作正常。但是,除了警报文本之外,我还尝试播放哔哔声警报声。然而,当人触摸限制的矩形区域时,音频播放会有很大的延迟,因为如果人一直触摸该区域,它会在每次迭代中播放。
我读过线程可能会有所帮助,但我无法使其工作。