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

时间:2019-02-16 08:13:46

标签: python opencv vlc cv2

当我尝试运行此代码时:

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

我不断收到此错误:

回溯(最近通话最近一次):

文件“ 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))   在Media_new中的文件“ C:\ Users \ Shivani Khare \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ vlc.py”,行1788     m._instance =自我 AttributeError:'NoneType'对象没有属性'_instance'

请帮助

1 个答案:

答案 0 :(得分:0)

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

尝试进行此更改:

m = vlc.MediaPlayer('D:\faith-42201.mp3')