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

Attribute error when importing VLC module in python

当我尝试 运行 此代码时:

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 行,在 new 中 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 = 自己 AttributeError: 'NoneType' 对象没有属性 '_instance'

请帮忙

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

尝试进行此更改:

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