发布网友 发布时间:2022-05-14 16:54
共1个回答
热心网友 时间:2023-10-14 10:49
摘要2 images = []43 labels = []44 def read_path(path_name): 45 for dir_item in os.listdir(path_name):46 #从初始路径开始叠加,合并成可识别的操作路径47 full_path = os.path.abspath(os.path.join(path_name, dir_item))48 49 if os.path.isdir(full_path): #如果是文件夹,继续递归调用50 read_path(full_path)51 else: #文件52 if dir_item.endswith('.jpg'):53 image = cv2.imread(full_path) 54 image = resize_image(image, IMAGE_SIZE, IMAGE_SIZE)55 56 #放开这个代码,可以看到resize_image()函数的实际调用效果57 #cv2.imwrite('1.jpg', image)58 59 images.append(image) 60 labels.append(path_name) 61 62 return images,labels63 【希望回答对您有帮助哦】咨询记录 · 回答于2021-11-24人脸识别实验中,程序步骤是导入模块安装接口分析结果调用函数您好,您的问题我已经了解。正在打字回复您,请给我五分钟的时间,一定回复您~您好,亲爱的--1 # -*- coding: utf-8 -*- 2 3 import os 4 import sys 5 import numpy as np 6 import cv2 7 8 IMAGE_SIZE = 64 9 10 #按照指定图像大小调整尺寸11 def resize_image(image, height = IMAGE_SIZE, width = IMAGE_SIZE):12 top, bottom, left, right = (0, 0, 0, 0)13 14 #获取图像尺寸15 h, w, _ = image.shape16 17 #对于长宽不相等的图片,找到最长的一边18 longest_edge = max(h, w) 19 20 #计算短边需要增加多上像素宽度使其与长边等长21 if h < longest_edge:22 dh = longest_edge - h23 top = dh // 224 bottom = dh - top25 elif w < longest_edge:26 dw = longest_edge - w27 left = dw // 228 right = dw - left29 else:30 pass 31 32 #RGB颜色33 BLACK = [0, 0, 0]34 35 #给图像增加边界,是图片长、宽等长,cv2.BORDER_CONSTANT指定边界颜色由value指定36 constant = cv2.copyMakeBorder(image, top , bottom, left, right, cv2.BORDER_CONSTANT, value = BLACK)37 38 #调整图像大小并返回39 return cv2.resize(constant, (height, width))40 41 #读取训练数据42 images = []43 labels = []44 def read_path(path_name): 45 for dir_item in os.listdir(path_name):46 #从初始路径开始叠加,合并成可识别的操作路径47 full_path = os.path.abspath(os.path.join(path_name, dir_item))48 49 if os.path.isdir(full_path): #如果是文件夹,继续递归调用50 read_path(full_path)51 else: #文件52 if dir_item.endswith('.jpg'):53 image = cv2.imread(full_path) 54 image = resize_image(image, IMAGE_SIZE, IMAGE_SIZE)55 56 #放开这个代码,可以看到resize_image()函数的实际调用效果57 #cv2.imwrite('1.jpg', image)58 59 images.append(image) 60 labels.append(path_name) 61 62 return images,labels63 【希望回答对您有帮助哦】