mirror of
https://gitee.com/nanjing-yimao-information/ieemoo-ai-gift.git
synced 2025-08-18 13:20:25 +00:00
76 lines
3.3 KiB
Python
76 lines
3.3 KiB
Python
import os
|
||
import random
|
||
import shutil # 新增:导入 shutil 模块用于目录复制
|
||
|
||
def extract_process_data_from_subfolders(root_path, shuffle=False):
|
||
"""
|
||
从指定路径下的每个子文件夹中提取 process.data 文件的内容,并与子文件夹名称建立对应关系。
|
||
如果 shuffle 为 True,则随机从 root_path 中选择 65 个子文件夹。
|
||
"""
|
||
# 存储子文件夹名称与 process.data 文件内容的对应关系
|
||
result = {}
|
||
negtive = []
|
||
# 获取所有子文件夹路径
|
||
subfolders = [folder for folder in os.listdir(root_path) if os.path.isdir(os.path.join(root_path, folder))]
|
||
|
||
num = 0
|
||
# 遍历指定路径下的所有子文件夹
|
||
for folder_name in subfolders:
|
||
folder_path = os.path.join(root_path, folder_name)
|
||
if "process.data" in os.listdir(folder_path):
|
||
file_path = os.path.join(folder_path, "process.data")
|
||
|
||
try:
|
||
# 读取 process.data 文件内容
|
||
with open(file_path, 'r', encoding='utf-8') as file:
|
||
full_content = file.read()
|
||
|
||
# 提取 "gift identify result:" 后的内容
|
||
if "gift identify result:" in full_content:
|
||
content = full_content.split("gift identify result:", 1)[1].strip()
|
||
# 去掉 content 中的换行符
|
||
content = content.replace('\n', '')
|
||
|
||
# 新增:去掉逗号后面的空格
|
||
content = content.replace(', ', ',')
|
||
|
||
# 新增:将 content 以逗号为分隔符分成若干组
|
||
groups = content.split(',')
|
||
|
||
# 修改:以空格为分隔符取每组的前一个字符,并转成列表,同时去掉空字符串并转换为整数
|
||
result_list = [float(group.split(' ')[0]) for group in groups if group.split(' ')[0]]
|
||
result_list = sorted(result_list, reverse=True)
|
||
if result_list and result_list[0] > 0.5:
|
||
num += 1
|
||
negtive.append(folder_path)
|
||
continue
|
||
|
||
# 将结果列表存入字典
|
||
result[os.path.basename(folder_path)] = result_list
|
||
else:
|
||
print(f"文件中未找到 'gift identify result:' 标记: {file_path}")
|
||
continue
|
||
except Exception as e:
|
||
print(f"读取文件失败: {file_path}, 错误信息: {e}")
|
||
print(num)
|
||
print(negtive)
|
||
|
||
# 新增:将 negtive 列表中的目录复制到目标路径
|
||
target_path = "../data/negtive"
|
||
for folder in negtive:
|
||
try:
|
||
# 使用 shutil.copytree 复制目录
|
||
shutil.copytree(folder, os.path.join(target_path, os.path.basename(folder)))
|
||
print(f"成功复制目录: {folder}")
|
||
except Exception as e:
|
||
print(f"复制目录失败: {folder}, 错误信息: {e}")
|
||
|
||
return result
|
||
|
||
|
||
# 调用函数并打印结果
|
||
if __name__ == "__main__":
|
||
# 指定根路径
|
||
comm_path = "/testDataAndLogs/测试视频数据以及日志/全实时_YoloV10s/20250512"
|
||
|
||
comm_data_dict = extract_process_data_from_subfolders(comm_path, shuffle=False) # 修改:新增 shuffle 参数 |