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