opencv-face-recognition-python
opencv-face-recognition-python copied to clipboard
error: (-215:Assertion failed) s >= 0 in function 'cv::setSize'
I am getting this error while training my classifier - face_recog.train(faces,np.array(faceID))
The full code is as follow:
[import cv2 import os import numpy as np %config IPCompleter.greedy=True
def facedetection(test_img):
gray_img = cv2.cvtColor(test_img,cv2.COLOR_BGR2GRAY)
face_cascade= cv2.CascadeClassifier("C:\\Users\\Abhijeet Debadwar\\AppData\\Local\\Programs\\Python\\Python38\\Lib\\site-packages\\cv2\\\data\\haarcascade_frontalface_default.xml")
faces = face_cascade.detectMultiScale(gray_img ,1.42,10)
return faces, gray_img
def labels_for_training_data(directory):
faces =[]
faceID= []
for maindirectory, subdirectory,filenames in os.walk(directory):
for filename in filenames:
if filename[0]==".":
print("skipped")
continue
id = os.path.basename(maindirectory)
img_path = os.path.join(maindirectory,filename)
print("img_path ",img_path)
print("image ID: ", id)
test_img = cv2.imread(img_path)
if test_img is None:
print("IMAGE NOT LOADED PROPERLY")
continue
faces_rect, gray_img = facedetection(test_img)
if len(faces_rect)!=1:
continue
(x,y,w,h)= faces_rect[0]
ROF_gray = gray_img[x:x+w,y:y+h]
faces.append(ROF_gray)
faceID.append(int(id))
return faces, faceID
def train_classifier(faces,faceID):
face_recog = cv2.face.LBPHFaceRecognizer_create()
face_recog.train(faces,np.array(faceID))
return face_recog
def draw_rect(test_img,faces):
(x,y,w,h) = faces
cv2.rectange(test_img,(x,y), (x+w,y+h),(255,0,0), 5)
def put_text(test_img,text,x,y):
cv2.putText(test_img,text, (x,y), cv2.FONT_HERSHEY_DUPLEX, 5, (0,255,0),5)
test_img = cv2.imread("D:\testimages\1\IMG_20190611_185155.jpg",1)
face_detected, gray_img = facedetection(test_img)
faces, faceID = labels_for_training_data("D:\testimages")
face_recog = train_classifier( faces, faceID)
name = { 1: "ABHIJEET", 0: "HERO"}
for face in face_detected:
(x,y,w,h)= face
roi_gray= gray_img[x:x+w,y:y+h]
label,confidencelevel = face_recog.predict(roi_gray)
print(" confidence ", confidencelevel)
print("label " , label)
draw_rect(test_img, face)
predicted_name = name[label]
put_text(test_img,predicted_name, x,y)
resized_img = cv2.resize(test_img, (500,500))
cv2.imshow("ABHIJEET S NICE", resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Did u find a solution for this ?