有没有办法让我的笔记本电脑上的这个运动检测器系统播放 mp3 文件?

问题描述

在它说“你好”的地方,它是通过文本到语音说的,我想用一个 mp3 文件(音乐)替换那个你好。这样当我做一些动作时,就会播放一首歌。我真的不知道我在做什么,所以如果有人知道一种更简单的方法来做到这一点,我真的很想知道,我不会以任何方式重新分发代码。谢谢

import cv2
import pyttsx3
import threading


# This funtion plays the audio message
def thread_voice_alert(engine):
    engine.say("hello")
    engine.runAndWait()


baseline_image=None
status_list=[None,None]
video=cv2.VideoCapture(0)

#Setting parameters for voice
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[1].id)
engine.setProperty('rate',150)

while True:
    check,frame = video.read()
    status=0
    gray_frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    gray_frame=cv2.GaussianBlur(gray_frame,(25,25),0)

    if baseline_image is None:
        baseline_image=gray_frame
        continue

    delta=cv2.absdiff(baseline_image,gray_frame)
    threshold=cv2.threshold(delta,30,255,cv2.THRESH_BINARY)[1]
    (contours,_)=cv2.findContours(threshold,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

    for contour in contours:
        if cv2.contourArea(contour) < 10000:
            continue
        status=1
        (x,y,w,h)=cv2.boundingRect(contour)
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0),1)
    status_list.append(status)
    
    
    if status_list[-1]==1 and status_list[-2]==0:
        t = threading.Thread(target=thread_voice_alert,args=(engine,))
        t.start()


    cv2.imshow("gray_frame Frame",gray_frame)
    cv2.imshow("Delta Frame",delta)
    cv2.imshow("Threshold Frame",threshold)
    cv2.imshow("Color Frame",frame)

    key=cv2.waitKey(1)

    if key==ord('q'):
        if status==1:
            times.append(datetime.Now())
        break


#Clean up,Free memory
engine.stop()
video.release()
cv2.destroyAllWindows

我没有写这个代码。原始来源:https://towardsdatascience.com/build-a-motion-triggered-alarm-in-5-minutes-342fbe3d5396

解决方法

您可以使用 playsound 模块。您需要使用 mach_header_64

安装它

那就去做

pip install playsound

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...