繁体   English   中英

python如何播放一个wav文件并让你的代码继续运行?

[英]How to play a wav file and make your code continue running in python?

我有这段代码可以播放视频,并检测其中的内容。 每当它检测到视频中的某些内容时,我想听到一些内容,代码如下:

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM