def get_images_and_labels(path): image_paths = [os.path.join(path, f) for f in os.listdir(path)] # 新建连个list用于存放 face_samples = [] ids = [] # 遍历图片路径,导入图片和id添加到list中 for image_path in image_paths: # 通过图片路径将其转换为灰度图片 img = Image.open(image_path).convert('L') # 将图片转化为数组 img_np = np.array(img, 'uint8') if os.path.split(image_path)[-1].split(".")[-1] != 'jpg': continue # 为了获取id,将图片和路径分裂并获取 id = int(os.path.split(image_path)[-1].split(".")[1]) # 调用熟悉的人脸分类器 detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') faces = detector.detectMultiScale(img_np) # 将获取的图片和id添加到list中 for (x, y, w, h) in faces: face_samples.append(img_np[y:y + h, x:x + w]) ids.append(id) return face_samples, ids