I can't figure out why my snippet of code keeps failing, maybe some of you could help.
import re
import cv2 as cv
import os
import numpy as np
from PIL import Image
import pickle
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
image_dir = os.path.join(BASE_DIR, "Faces")
face_cascade = cv.CascadeClassifier(r'C:\Users\swhit\Documents\OPENCVTests\Basic-Augmented-reality-course-opencv-master\GENERATE_MARKERS\Cascades\data\haarcascade_frontalface_alt.xml')
recognizer = cv.face.LBPHFaceRecognizer_create()
current_id = 0
label_ids = {}
y_labels = []
x_train = []
for root, dirs, files in os.walk(image_dir):
for file in files:
if file.endswith("png") or file.endswith("jpg"):
path = os.path.join(root, file)
label = os.path.basename(os.path.dirname(path)).replace(" ", "-").lower()
#print(label, path)
if label in label_ids:
pass
else:
label_ids[label] = current_id
current_id += 1
id_ = label_ids[label]
y_labels.append(label) # Some number
x_train.append(path) # Verify this, turn into NUMPY, GRAY
pil_image = Image.open(path).convert("L") # Grayscale
image_array = np.array(pil_image, "uint8")
#print(image_array)
faces = face_cascade.detectMultiScale(image_array, scaleFactor=1.5, minNeighbors=5)
for (x,y,w,h) in faces:
roi = image_array[y:y+h, x:x+w]
x_train.append(roi)
y_labels.append(id_)
#print(y_labels)
#print(x_train)
with open("labels.pickle", 'wb') as f:
pickle.dump(label_ids, f)
recognizer.train(x_train, np.array(y_labels))
recognizer.save("trainer.yml")
In specific it's the recognizer.train(x_train, np.array(y_labels))
part. I get this error code
OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'train'
> Overload resolution failed:
> - Can't parse 'src'. Sequence item with index 0 has a wrong type
> - Can't parse 'src'. Sequence item with index 0 has a wrong type.