我有这段代码可以播放视频,并检测到其中的内容。每当它在视频中检测到某些东西时,我都想听到一些声音,这是代码:
import cv2
import os
video_capture = cv2.VideoCapture('video')
while True:
_, frame = video_capture.read()
found = detect_something(frame)
if found :
os.system("aplay 'alarm'")
cv2.imshow('Video',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
问题在于,无论何时播放警报,视频都会冻结。我希望将警报作为背景声音播放。我怎样才能做到这一点?
最佳答案
它需要的是一个踏板:
import cv2
import os
from threading import Thread # Import Thread here
video_capture = cv2.VideoCapture('video')
def music(): # Define a function to go in the Thread
os.system("aplay 'alarm'")
while True:
_, frame = video_capture.read()
found = detect_something(frame)
if found :
mus = Thread(target=music) # Create a Thread each time found
mus.start() # Start the Thread as soon as created
cv2.imshow('Video',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
关于python - 如何播放wav文件,并使您的代码继续在python中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62085103/