python - 如何播放wav文件,并使您的代码继续在python中运行?

标签 python opencv audio video

我有这段代码可以播放视频,并检测到其中的内容。每当它在视频中检测到某些东西时,我都想听到一些声音,这是代码:

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/

相关文章:

javascript - Howler.js 错误 : ' An array of source files must be passed with any new Howl '

linux - OpenCV、Linux ubuntu12.04 undefined symbol : _ZN9QListData11detach_growEPii

c++ - OpenCV Image Mat 到 1D CHW(RR...R, GG..G, BB..B) vector

python - IOError: [Errno 输入溢出] -9981

Python将字符串添加到列表循环中

opencv - 使用移动设备检测用户在房间中的站立位置

windows - 音频:如何设置默认麦克风的电平?

python - Django/谷歌云 : FATAL: database <db> does not exist/Server Error 500

python - 我正在使用 excel 来构建网站 - 正在寻找替代方案

python - PayPal:Python 传入 JSON 请求未映射到 API 请求