-2

THIS IS THE CODE:

import cv2
import numpy as np

face_classifier = cv2.CascadeClassifier(cv2.data.haarcascades + 'C:/Users/sarba/AppData/Local/Programs/Python/Python36/Lib/site-packages/cv2/data/haarcascade_frontalface_default')

def face_extractor(img):

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray,1.3,5)

    if faces is():
        return None

    for(x,y,w,h) in faces:
        cropped_face = img[y:y+h, x:x+w]

    return cropped_face
cap = cv2.VideoCapture(0)
count = 0

while True:
    ret, frame = cap.read()
    if face_extractor(frame) is not None:
        count+=1
        face = cv2.resize(face_extractor(frame),(200,200))
        face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

        file_name_path = 'C:/Users/sarba/Downloads/Project/faces/user'+str(count)+'.jpg'
        cv2.imwrite(file_name_path, face)

        cv2.putText(face,str(count),(50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
        cv2.imshow('Face Cropper',face)
    else:
        print("Face not found")
        pass

    if cv2.waitKey(1)==13:
        break

cap.release()
cv2.destroyAllWindows()
print('collecting samples complete!!!')`

ERROR:`File "C:/Users/sarba/PycharmProjects/Face_recognition/main.py", line 29, in if face_extractor(frame) is not None: File "C:/Users/sarba/PycharmProjects/Face_recognition/main.py", line 11, in face_extractor faces = face_classifier.detectMultiScale(gray,1.3,5) cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

[ WARN:0@4.678] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (539) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

Process finished with exit code 1 `

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87

1 Answers1

1

The error is pointing out that the haarcascade file can't be found due to the wrong given path.You are giving the base path to the haarcascade files twice with no format.Replace the path with

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
Moeed
  • 21
  • 3