3、定义一个方法,用于输入文件夹中提取图像和标签:
def get_images_and_labels(input_path): label_words = [] # 对输入文件夹做递归迭代并追加文件 for root, dirs, files in os.walk(input_path): for filename in (x for x in files if x.endswith('.jpg')): filepath = os.path.join(root, filename) label_words.append(filepath.split('\\')[-2]) # 初始化变量 images = [] le = LabelEncoder() le.encode_labels(label_words) labels = [] # 解析输入目录 for root, dirs, files in os.walk(input_path): for filename in (x for x in files if x.endswith('.jpg')): filepath = os.path.join(root, filename) # 将当前图像读取成灰度格式 image = cv2.imread(filepath, 0) # 从文件夹路径中提取标签 name = filepath.split('\\')[-2] # 做人脸检测 faces = faceCascade.detectMultiScale(image, 1.1, 2, minSize=(100, 100)) # 循环处理每一张脸
# 提取ROI属性值,并将这些值和标签编码器返回: for (x, y, w, h) in faces: images.append(image[y:y + h, x:x + w]) labels.append(le.word_to_num(name)) return images, labels, le