Traceback (most recent call last):
File "<ipython-input-1-cf0ff588e2e2>", line 1, in <module>
runfile('E:/FaceDetection/face_recognition.py', wdir='E:/FaceDetection')
File "C:\Users\varun\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\varun\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "E:/FaceDetection/face_recognition.py", line 43, in <module>
recognizer.train(x_train, np.array(y_labels))
TypeError: src is not a numpy array, neither a scalar
import cv2
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, "Images")
face_cascade = cv2.CascadeClassifier('Haar\haarcascade_frontalcatface.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
current_id = 0
label_id = {}
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(root).replace(" ", "-").lower()
#print (label, path)
if not label in label_id:
label_id[label] = current_id
current_id += 1
idee = label_id[label]
#print(label_id)
y_labels.append(label)
x_train.append(path)
pil_image = Image.open(path).convert("L")
image_array = np.array(pil_image, 'uint8')
faces = face_cascade.detectMultiScale(image_array, scaleFactor = 1.03 ,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(idee)
with open("labels.pickle", 'wb') as f:
pickle.dump(label_id, f)
recognizer.train(x_train, np.array(y_labels))
recognizer.save("trainner.yml")
我该如何解决?为什么我会收到此错误?