python opencv2, 错误的相机界面,只有cam light

python opencv2, error camera interface, only cam light

这是 opencv2 Takeimage 按钮的代码。
无法正常使用,只有摄像头灯亮,但相机界面不显示:

def TakeImage():
    Id=(txt.get())
    name=(txt2.get())
    if(is_number(Id) and name.isalpha()):
        Video= cv2.VideoCapture(0)
        harcascadePath = "haarcascade_frontalfacedafults_.xml"
        detector=cv2.CascadeClassifier(harcascadePath)
        sampleNum=0
        while(True):
            ret,img=Video.read();
            gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
            faces=detector.DetectMultiScale(gray,1.2,5);
            for(x,y,w,h) in faces:
                cv2.recatangle(img,(x,y),(x+w,y+h),(255,0,0),2)
                sampleNum=sampleNum+1
                cv2.imwrite("TrainImages\ "+name +"."+Id +'.'+str(samlpeNum)+".jpg",gray[y:y+h,x:x+h])
                cv2.imshow('Frame',img)
            if cv2.waitKey(100) &0XFF == ord('s'):
                break
            elif sample>60:
                break
        Video.release()
        cv2.destroyAllWindows()
        res = "Images Saved for ID: "+ Id + " Name : "+ name
        row = [Id, Name]
        with open ('studentDetails\studentDetails.csv','a+') as csvFile:
             writer = csv.writer(csvFile)
             writer.writerow(row)
             csvFile.close()
             message.configure(text=res)
    else:
        if(is_number(Id)):
            res ="Enter Alphabetical Name"
            message.configure(text= res)
        if (name.isalpha()):
            res="Enter Numberic Id"
            message.configure(text=res)

错误显示:

Exception in Tkinter callback Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 1705, in call return self.func(*args) File "C:\Users\Lenovo\Desktop\face reconiger system.py", line 87, in TakeImage faces=detector.DetectMultiScale(gray,1.2,5); AttributeError: 'cv2.CascadeClassifier' object has no attribute 'DetectMultiScale'

.xml 文件的路径看起来不正确。您需要替换以下行:

harcascadePath = "haarcascade_frontalfacedafults_.xml"

harcascadePath = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'