python opencv2, error camera interface, only cam light

Viewed 72

This is the code for opencv2 Takeimage button.
It doesn't work properly, only the cam light on, but camera interface doesn't show:

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)

Error showing:

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'

1 Answers

The path to the .xml file looks incorrect. You need to replace the following line:

harcascadePath = "haarcascade_frontalfacedafults_.xml"

with

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