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.
54 lines
2.3 KiB
54 lines
2.3 KiB
11 months ago
|
import os
|
||
|
import csv
|
||
|
import shutil
|
||
|
import pandas as pd
|
||
|
|
||
|
def index_label(photo_folder, csv_name):
|
||
|
# 读取照片数据文件夹
|
||
|
photo_files = os.listdir(photo_folder)
|
||
|
# 提取数字作为索引
|
||
|
indexes = [int(os.path.splitext(file)[0]) for file in photo_files]
|
||
|
# 打开原始CSV文件和新的CSV文件
|
||
|
csv_file = 'data/emotion.csv'
|
||
|
new_csv_file = 'data/'+csv_name+'.csv'
|
||
|
with open(csv_file, 'r') as file:
|
||
|
reader = csv.reader(file)
|
||
|
data = list(reader)
|
||
|
# 添加索引列
|
||
|
header = ['索引', '标签']
|
||
|
result = [header] + [[index, data[index][0]] + data[index][1:] for index in indexes if index < len(data)]
|
||
|
# 将结果保存为新的CSV文件
|
||
|
# new_csv_file = '新的CSV文件路径'
|
||
|
with open(new_csv_file, 'w', newline='') as file:
|
||
|
writer = csv.writer(file)
|
||
|
writer.writerows(result)
|
||
|
|
||
|
def image_classification(photo_folder, csv_name):
|
||
|
# 读取包含索引和标签的数据文件
|
||
|
# 定义要删除的文件夹路径
|
||
|
target_folder = 'data_classification/' + csv_name # 目标文件夹路径
|
||
|
|
||
|
# 遍历文件夹中的文件并删除
|
||
|
for filename in os.listdir(target_folder):
|
||
|
file_path = os.path.join(target_folder, filename)
|
||
|
try:
|
||
|
if os.path.isfile(file_path) or os.path.islink(file_path):
|
||
|
os.unlink(file_path) # 删除文件
|
||
|
elif os.path.isdir(file_path):
|
||
|
shutil.rmtree(file_path) # 递归删除文件夹及其内容
|
||
|
except Exception as e:
|
||
|
print(f'无法删除文件:{file_path}')
|
||
|
|
||
|
new_csv_file = 'data/' + csv_name + '.csv'
|
||
|
df = pd.read_csv(new_csv_file, encoding='GBK')
|
||
|
# 设置原始文件夹路径和目标文件夹路径
|
||
|
# 遍历 DataFrame
|
||
|
for index, row in df.iterrows():
|
||
|
file_index = row['索引'] # 索引对应的列名
|
||
|
file_label = row['标签'] # 标签对应的列名
|
||
|
source_file = os.path.join(photo_folder, f'{file_index}.jpg') # 原始文件路径
|
||
|
target_folder_label = os.path.join(target_folder, str(file_label)) # 目标文件夹路径
|
||
|
# 创建对应标签的目标文件夹(如果不存在)
|
||
|
os.makedirs(target_folder_label, exist_ok=True)
|
||
|
# 复制文件到目标文件夹
|
||
|
shutil.copy(source_file, target_folder_label)
|