You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.4 KiB
46 lines
1.4 KiB
11 months ago
|
# -*- coding: utf-8 -*-
|
||
|
# Time : 2023/12/8 9:11
|
||
|
# Author : lirunsheng
|
||
|
# User : l'r's
|
||
|
# Software: PyCharm
|
||
|
# File : read_data.py
|
||
|
|
||
|
import os
|
||
|
import glob
|
||
|
import random
|
||
|
import pandas as pd
|
||
|
|
||
|
def read_folders(folder_path):
|
||
|
# 遍历文件夹中的子文件夹
|
||
|
subfolders = []
|
||
|
for root, dirs, files in os.walk(folder_path):
|
||
|
for subdir in dirs:
|
||
|
subfolders.append(subdir)
|
||
|
# print(subfolders)
|
||
|
|
||
|
# 提取子文件夹中的图像路径
|
||
|
image_paths = []
|
||
|
for root, dirs, files in os.walk(folder_path):
|
||
|
for file in files:
|
||
|
if file.endswith('.png') or file.endswith('.jpg'):
|
||
|
image_path = os.path.join(root, file)
|
||
|
image_path = image_path.replace("\\", "/") # 将路径中的反斜杠替换为斜杠
|
||
|
index_name = image_path.split('/')[-2]
|
||
|
image_paths.append([image_path, subfolders.index(index_name)])
|
||
|
# 打乱列表中的元素
|
||
|
random.shuffle(image_paths)
|
||
|
# 打印图像路径列表
|
||
|
path_list = []
|
||
|
label_list = []
|
||
|
for path in image_paths:
|
||
|
# print(path)
|
||
|
path_list.append(path[0])
|
||
|
label_list.append(path[1])
|
||
|
# print(label_list)
|
||
|
# 将列表转换为DataFrame
|
||
|
df = pd.DataFrame(label_list, columns=['label'])
|
||
|
# print(df)
|
||
|
label_name = folder_path.split('/')[-2]
|
||
|
df.to_csv(label_name+'.csv', header=False, index=False)
|
||
|
return subfolders, path_list # 标签,图片路径集
|