Hey guys I am doing a face recognition project using LBPHFaceRecognizer the problen is that for few images (test images) the model is recognizing and for few images it is giving this error: (-215:Assertion failed) s >= 0 in function 'cv::setSize' [closed]

asked 2018-12-19 08:15:20 -0600

oja gravatar image

updated 2020-11-14 05:43:26 -0600

I know the problem is with the picture but can't figure out what the problem is i tried reducing size for the image still doesn't work

Here is the complete code

import cv2
import os
import numpy as np


subjects=["","oja","mom"]
#img=cv2.imread('obama.jpg')
#function_1
def detect_face(img):
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    face_cascade=cv2.CascadeClassifier('A:/project_face_detection/face_detetion/opencv-files/lbpcascade_frontalface.xml')
    faces=face_cascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5);
    if(len(faces)==0):
        return None,None
    (x,y,w,h)=faces[0]
    return gray[y:y+w,x:x+h],faces[0]

#function_2
def prepare_training_data(data_folder_path):
    dirs=os.listdir(data_folder_path)
    faces=[]
    labels=[]
    for dir_name in dirs:
        if not dir_name.startswith('s'):
            continue;
        label=int(dir_name.replace("s",""))
        subject_dir_path= data_folder_path +"/"+dir_name
        subject_images_names=os.listdir(subject_dir_path)   
        for image_name in subject_images_names:
            if image_name.startswith("."):
                continue;
            image_path=subject_dir_path + "/" + image_name 

            image=cv2.imread(image_path)


            cv2.imshow("training on image",cv2.resize(image,(400,500)))


            cv2.waitKey(100)
            face,rect=detect_face(image)
            if face is not None:
                faces.append(face)
                labels.append(label)
    cv2.destroyAllWindows()
    cv2.waitKey(1)
    cv2.destroyAllWindows()
    return faces,labels

print("preparing data")
faces,labels=prepare_training_data("training_data") 
print("total faces:",len(faces))
print("total labels:",len(labels))

face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.train(faces, np.array(labels))            



#function_3
def draw_rectangle(img, rect):
    (x, y, w, h) = rect
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)

#function_4
def draw_text(img, text, x, y):
    cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
#function_5
def predict(test_img):
    img = test_img.copy()
    face, rect = detect_face(img)
    label, confidence = face_recognizer.predict(face)
    label_text = subjects[label]
    draw_rectangle(img, rect)
    draw_text(img, label_text, rect[0], rect[1]-5)    
    return img


print("Predicting images...")

test_img1 = cv2.imread("A:/project_face_detection/face_detetion/test_data/test3.1.jpg")
test_img2 = cv2.imread("A:/project_face_detection/face_detetion/test_data/test2.1.jpg")

predicted_img1 = predict(test_img1)
predicted_img2 = predict(test_img2)
print("Prediction complete")

cv2.imshow(subjects[1], cv2.resize(predicted_img1, (250, 250)))
cv2.imshow(subjects[2], cv2.resize(predicted_img2, (250, 250)))
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.destroyAllWindows()

*when I try to predict for test3.1 image it works for test2.1 i get this error: (-215:Assertion failed) s >= 0 in function 'cv::setSize' *

test_img1 = cv2.imread("A:/project_face_detection/face_detetion/test_data/test3.1.jpg")
    test_img2 = cv2.imread("A:/project_face_detection/face_detetion/test_data/test2.1.jpg")

    predicted_img1 = predict(test_img1)
    predicted_img2 = predict(test_img2)

Thanks for looking into it I am relatively new to openCV so please don't step back from elaborating even the most basic stuff

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-11-09 10:22:10.814831

Comments

after image=cv2.imread(image_path) check if image is empty.

"A:" have you got a floppy disk? 5"1/4 or 3 1/2"

LBerger gravatar imageLBerger ( 2018-12-19 08:19:46 -0600 )edit
1

no i named my partition A

oja gravatar imageoja ( 2018-12-19 08:22:31 -0600 )edit

you have to CHECK every imread() call (python users NEVER do this):

if img isNone:
    # bad luck

also, directories might contain invisible non image files like thumbs.db, etc.

berak gravatar imageberak ( 2018-12-19 08:29:37 -0600 )edit

it has 20 images each i took them my self

oja gravatar imageoja ( 2018-12-19 08:39:45 -0600 )edit

well, you did not check, that's why you get the error.

berak gravatar imageberak ( 2018-12-19 08:58:03 -0600 )edit