import os import random # 新增:导入 random 模块用于随机采样 from display_result import show_to_pr as sp import numpy as np def extract_process_data_from_subfolders(root_path, shuffle=False): """ 从指定路径下的每个子文件夹中提取 process.data 文件的内容,并与子文件夹名称建立对应关系。 如果 shuffle 为 True,则随机从 root_path 中选择 65 个子文件夹。 """ # 存储子文件夹名称与 process.data 文件内容的对应关系 result = {} # 获取所有子文件夹路径 subfolders = [folder for folder in os.listdir(root_path) if os.path.isdir(os.path.join(root_path, folder))] # 如果 shuffle 为 True,则随机选择 65 个子文件夹 if shuffle and len(subfolders) > 114: subfolders = random.sample(subfolders, 114) # 遍历指定路径下的所有子文件夹 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.replace('\n', '') # content = content.split("select back tracking feats index:", 1)[0].strip() # # 去掉 content 中的换行符 # content = content.replace('\n', '') content = \ full_content.split('gift identify result:', 1)[1].split('select back tracking feats index:')[ 0].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 shuffle and result_list and result_list[0] > 0.68: # 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}") if len(result) >= 113: break print(result) return result def estimate(data_list, type=None): number = round(len(data_list) * 0.2) if number > 0: if type == 0: result = round(sum(data_list[:number]) / number, 2) # 修改:使用 round() 函数保留小数点后两位 elif type == 1: result = data_list return result else: return 0 def deal_with_process_data(gift_data_dict, comm_data_dict): targets = [] outputs = [] type = 1 # type 0:qu for gift, comm, gift_evnt_name, comm_evnt_name in zip(gift_data_dict.values(), comm_data_dict.values(), gift_data_dict.keys(), comm_data_dict.keys()): outputs += [estimate(gift, type), estimate(comm, type)] targets += [1, 0] # print(outputs, targets) if type == 0: sp(np.array(targets), np.array(outputs), type) elif type == 1: sp(np.array(targets), outputs, type) # 调用函数并打印结果 if __name__ == "__main__": # 指定根路径 gift_path = "/testDataAndLogs/测试视频数据以及日志/全实时_YoloV10s/赠品测试/正样本" comm_path = "/testDataAndLogs/测试视频数据以及日志/全实时_YoloV10s/赠品测试/负样本" # 提取 process.data 文件内容 gift_data_dict = extract_process_data_from_subfolders(gift_path, shuffle=False) # 修改:新增 shuffle 参数 comm_data_dict = extract_process_data_from_subfolders(comm_path, shuffle=True) # 修改:新增 shuffle 参数 deal_with_process_data(gift_data_dict, comm_data_dict)