def read_file(path): img_list = [] label_list = [] dir_counter = 0 IMG_SIZE = 128 #对路径下的所有子文件夹中的所有jpg文件进行读取并存入到一个list中 for child_dir in os.listdir(path): child_path = os.path.join(path, child_dir) for dir_image in os.listdir(child_path): print(child_path) if endwith(dir_image, 'jpg'): img = cv2.imread(os.path.join(child_path, dir_image)) resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) recolored_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY) img_list.append(recolored_img) label_list.append(dir_counter) dir_counter += 1 # 返回的img_list转成了 np.array的格式 img_list = np.array(img_list) return img_list, label_list, dir_counter
def read_file(path): # Picture collection img_list = [] # Label collection label_list = [] # The number of subfolders, that is, the number of labels label_num = 0 # size of the picture IMG_SIZE = 128 # Read and save all jpg files in all subfolders under the path to a list for child_dir in os.listdir(path): child_path = os.path.join(path, child_dir) for dir_image in os.listdir(child_path): if endwith(dir_image, 'jpg'): img = cv2.imread(os.path.join(child_path, dir_image)) resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) recolored_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY) img_list.append(recolored_img) label_list.append(label_num) # label number increment label_num += 1 # The returned img_list is converted to np.array format img_list = np.array(img_list) # Return value list: all pictures collection, label collection, label number return img_list, label_list, label_num
def read_file(path): # 图片集合 img_list = [] # 标签集合 label_list = [] # 子文件夹个数,即label数 label_num = 0 # 图片尺寸 IMG_SIZE = 128 # 对路径下的所有子文件夹中的所有jpg文件进行读取并存入到一个list中 for child_dir in os.listdir(path): child_path = os.path.join(path, child_dir) for dir_image in os.listdir(child_path): if endwith(dir_image, 'jpg'): img = cv2.imread(os.path.join(child_path, dir_image)) resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) recolored_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY) img_list.append(recolored_img) label_list.append(label_num) # label数自增 label_num += 1 # 返回的img_list转成了 np.array的格式 img_list = np.array(img_list) # 返回值列表:所有图片集合、标签集合、标签数 return img_list, label_list, label_num
def read_file(path): img_list = [] label_list = [] dir_counter = 0 #对路径下的所有子文件夹中的所有jpg文件进行读取并存入到一个list中 for child_dir in os.listdir(path): child_path = os.path.join(path, child_dir) for dir_image in os.listdir(child_path): if endwith(dir_image, 'jpg'): #print(os.path.join(child_path, dir_image)) img = cv2.imread(os.path.join(child_path, dir_image)) #img =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) img = cv2.resize(img, (255, 255), interpolation=cv2.INTER_CUBIC) img = img[:, :, (2, 0, 1)] img_list.append(img) label_list.append(dir_counter) dir_counter += 1 # 返回的img_list转成了 np.array的格式 img_list = np.array(img_list) print(img_list.shape) return img_list, label_list, dir_counter
def read_file(path): img_list = [] label_list = [] dir_counter = 0 #for i in os.listdir(path): #print (os.path.isfile(i)) #Read all jpg files in all subfolders under the path and save them to a list for child_dir in os.listdir(path): if os.path.isfile(child_dir) == False: child_path = os.path.join(path, child_dir) for dir_image in os.listdir(child_path): print(child_path) if endwith(dir_image, 'jpg'): img = cv2.imread(os.path.join(child_path, dir_image)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_list.append(img) label_list.append(dir_counter) dir_counter += 1 # The returned img_list is converted to the format of np.array img_list = np.array(img_list) return img_list, label_list, dir_counter
def read_file(path): img_list = [] label_list = [] dir_counter = 0 IMG_SIZE = 128 file_count = 0 labbel = {} #对路径下的所有子文件夹中的所有jpg文件进行读取并存入到一个list中 for child_dir in os.listdir(path): if child_dir == '.DS_Store': continue child_path = os.path.join(path, child_dir) for child_dir2 in os.listdir(child_path): if child_dir2 == '.DS_Store': continue child_path2 = os.path.join(child_path, child_dir2) num = 20 for dir_image in os.listdir(child_path2): if dir_image == '.DS_Store': continue if dir_image[-3:] == 'gif': continue num -= 1 #if num>0: # print(child_path2) file_count += 1 #print(dir_image) if endwith(dir_image, 'jpg'): img = cv2.imread(os.path.join(child_path2, dir_image)) #print(img) resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) recolored_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY) img_list.append(recolored_img) label_list.append(dir_counter) labbel[dir_counter] = dir_image[:-6] dir_counter += 1 with open('label.txt', 'wb') as file: #file.write(json.dumps(labbel)) pickle.dump(labbel, file) # 返回的img_list转成了 np.array的格式 img_list = np.array(img_list) return img_list, label_list, dir_counter
def read_file(path): img_list = [] label_list = [] dir_counter = 0 # 对路径下的所有子文件夹中的所有jpg文件进行读取并存入到一个list中 for child_dir in os.listdir(path): child_path = os.path.join(path, child_dir) for dir_image in os.listdir(child_path): if endwith(dir_image, 'jpg'): #img = cv2.imread(os.path.join(child_path, dir_image)) img = Image.open(os.path.join(child_path, dir_image)) #图片归一化处理 #img = cv2.resize(img, (255, 255), interpolation=cv2.INTER_CUBIC) img = img.resize((255, 255)) arr = np.asarray(img, dtype="float32") #高斯模糊处理 image1 = img.filter(GaussianBlur(radius=1)) arr1 = np.asarray(image1, dtype="float32") image2 = img.filter(GaussianBlur(radius=3)) arr2 = np.asarray(image2, dtype="float32") image3 = img.filter(GaussianBlur(radius=5)) arr3 = np.asarray(image3, dtype="float32") #合成四维矩阵 new = np.empty((255, 255, 3, 4), dtype="float32") new[:, :, :, 0] = arr new[:, :, :, 1] = arr1 new[:, :, :, 2] = arr2 new[:, :, :, 3] = arr3 img_list.append(new) label_list.append(dir_counter) dir_counter += 1 # 返回的img_list转成了 np.array的格式 img_list = np.array(img_list) return img_list, label_list, dir_counter
def read_file(path): label_list = [] dir_counter = 0 #二维数组存储每个子文件夹下每张图片的face_encoding. img_encoding = [[] for i in range(5)] for child_dir in os.listdir(path): child_path = os.path.join(path, child_dir) for dir_image in os.listdir(child_path): if endwith(dir_image, 'jpg'): img = scipy.misc.imread(os.path.join(child_path, dir_image)) #resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) #recolored_img = cv2.cvtColor(resized_img,cv2.COLOR_BGR2GRAY) img_encoding[dir_counter].append( face_recognition.face_encodings(img)[0]) label_list.append(dir_counter) dir_counter += 1 return img_encoding, label_list, dir_counter
def read_file(path): img_list = [] label_list = [] user_number = 0 # total user number IMG_SIZE = 128 for child_dir in os.listdir(path): child_path = os.path.join(path, child_dir) child_path = child_path + "\\picked-faces" for dir_image in os.listdir(child_path): if endwith(dir_image, 'jpg'): img = cv2.imread(os.path.join(child_path, dir_image)) resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) recolored_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY) img_list.append(recolored_img) label_list.append(user_number) user_number += 1 img_list = np.array(img_list) return img_list, label_list, user_number
def read_file(path): img_list = [] label_list = [] dir_counter = 0 IMG_SIZE = 128 #Read images for child_dir in os.listdir(path): child_path = os.path.join(path, child_dir) for dir_image in os.listdir(child_path): if endwith(dir_image, 'jpg'): img = cv2.imread(os.path.join(child_path, dir_image)) resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) recolored_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY) img_list.append(recolored_img) label_list.append(dir_counter) dir_counter += 1 img_list = np.array(img_list) return img_list, label_list, dir_counter
def read_file(path): img_list = [] #全部图片list label_list = [] #标签列表 dir_counter = 0 #子文件夹个数 IMG_SIZE = 128 #对路径下的所有子文件夹中的所有jpg文件进行读取并存入到一个list中 for child_dir in os.listdir(path): child_path = os.path.join(path, child_dir) #进入子文件夹读取图片 for dir_image in os.listdir(child_path): if endwith(dir_image, 'jpg'): img = cv2.imread(os.path.join(child_path, dir_image)) resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) recolored_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY) img_list.append(recolored_img) label_list.append(dir_counter) #子文件夹个数加1 dir_counter += 1 #转成np.array的格式并返回 img_list = np.array(img_list) return img_list, label_list, dir_counter