在python中导入VLC模块时的属性错误

问题描述 投票:0回答:1

当我尝试运行此代码时:

import cv2
import vlc
cam = cv2.VideoCapture(0)
m = vlc.MediaPlayer('D:\faith-42201.mp3')
fd = cv2.CascadeClassifier(r'C:\Users\Shivani Khare\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\cv2\data\haarcascade_frontalface_alt.xml')
m.play()
flag=1
m.audio_set_volume(100)
while(True):
    r,i = cam.read()
    #i[0:10,:,:]=255
    #for adding rows
    j = cv2.cvtColor(i,cv2.COLOR_BGR2GRAY)
    face = fd.detectMultiScale(j,1.3,7)
    for (x,y,w,h) in face:
        cv2.rectangle(i,(x,y),(x+w,y+h),(0,0,0),-1)
        t = w*h
        m.audio_set_volume(100-int(t/500))
        l = len(face)
        if(l>0):
            m.play()
            flag = 0
        elif(flag==0):
            m.pause()
            flag = 1
    print("no of faces are : ",len(face),face)
    cv2.imshow('image1',i)

    k = cv2.waitKey(5)
    print(k)
    if(k==27):
        cv2.destroyAllWindows()
        break

我一直收到这个错误:

Traceback(最近一次调用最后一次):

文件“C:\ Users \ Shivani Khare \ AppData \ Local \ Programs \ Python \ Python36 \ hi.py”,第5行,m = vlc.MediaPlayer('D:\ faith-42201.mp3')文件“C: \ Users \ Shivani Khare \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ vlc.py“,第3184行,在新的o.set_media(instance.media_new(* args))文件”C:\ Users \ Shivani Khare \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ vlc.py“,第1788行,在media_new m._instance = self AttributeError:'NoneType'对象没有属性'_instance'

请帮忙

python opencv vlc cv2
1个回答
0
投票

你在第5行的MediaPlayer构造函数中传递一个列表,但它需要一个字符串(ref:https://linuxconfig.org/how-to-play-audio-with-vlc-in-python)。

尝试进行此更改:

m = vlc.MediaPlayer('D:\faith-42201.mp3')
© www.soinside.com 2019 - 2024. All rights reserved.