| import json | |
| import os | |
| import multiprocessing | |
| import numpy as np | |
| import cv2 | |
| from tqdm import tqdm | |
| from PIL import Image | |
| from pycocotools import mask as mask_utils | |
| # 加载json文件 | |
| # json_path = "/scratch/yuqian_fu/competition_test_20250515_png_v3.json" | |
| # save_path = "/scratch/yuqian_fu/competition_test_20250516_rle_v3.json" | |
| # with open(json_path, "r") as f: | |
| # datas = json.load(f) | |
| # takes = list(datas.keys()) | |
| # # 定义处理一个样本的函数 | |
| # def maskpng_to_rle(sample): | |
| # cur_pred = cv2.imread(sample['pred_mask'], cv2.IMREAD_GRAYSCALE) | |
| # cur_pred = mask_utils.encode(np.asfortranarray(cur_pred.astype(np.uint8))) | |
| # cur_pred['counts'] = cur_pred['counts'].decode('ascii') | |
| # return cur_pred | |
| # results_new = {} | |
| # for take in takes: | |
| # datas_this_take = datas[take] | |
| # objs_this_take = list(datas_this_take['masks'].keys()) | |
| # # 保存新结果 | |
| # pred_json_new = {'masks': {}, 'subsample_idx': datas_this_take['subsample_idx']} | |
| # for obj in objs_this_take: | |
| # cam_combinations = list(datas_this_take['masks'][obj].keys()) | |
| # for cam in cam_combinations: | |
| # ids = list(datas_this_take['masks'][obj][cam].keys()) | |
| # for id in ids: | |
| # sample = datas_this_take['masks'][obj][cam][id] | |
| # cur_pred = maskpng_to_rle(sample) | |
| # sample['pred_mask'] = cur_pred | |
| # # 保存前检查key是否存在 | |
| # if obj not in pred_json_new['masks']: | |
| # pred_json_new['masks'][obj] = {} | |
| # if cam not in pred_json_new['masks'][obj]: | |
| # pred_json_new['masks'][obj][cam] = {} | |
| # # 保存 | |
| # pred_json_new['masks'][obj][cam][id] = sample | |
| # # 保存每个take的结果 | |
| # results_new[take] = pred_json_new | |
| # # 保存最后的结果 | |
| # with open(save_path, "w") as fp: | |
| # json.dump(results_new, fp) | |
| # 增加“ego-exo”、“results”键 | |
| # json_path_1 = "/scratch/yuqian_fu/competition_Exo2Ego_Final_all_999.json" | |
| # json_path_2 = "/scratch/yuqian_fu/competition_Ego2Exo_Final_all_999.json" | |
| # with open(json_path_1, "r") as f: | |
| # result_exoego = json.load(f) | |
| # with open(json_path_2, "r") as f: | |
| # result_egoexo = json.load(f) | |
| # save_path = "/scratch/yuqian_fu/Competition_Final_All_Submit_reshape.json" | |
| # with open(save_path, "w") as fp: | |
| # json.dump({"exo-ego": {"version": "xx","challenge": "xx", 'results': result_exoego['exo-ego']['results']}, "ego-exo": {"version": "xx","challenge": "xx",'results': result_egoexo['ego-exo']['results']}}, fp) | |
| #处理exo2ego | |
| # results_new_exo2ego = {} | |
| # list_values = list(range(1, 17)) | |
| # for value in [6, 8, 9, 12, 16]: | |
| # list_values.remove(value) | |
| # for i in list_values: | |
| # i = str(i) | |
| # json_path = f"/scratch/yuqian_fu/competition_test_ExoEgo_v{i}.json" | |
| # with open(json_path, "r") as f: | |
| # result = json.load(f) | |
| # # 依次合并字典 | |
| # results_new_exo2ego.update(result) | |
| # for j in [6, 8, 9, 12, 16]: | |
| # j = str(j) | |
| # json_path = f"/scratch/yuqian_fu/competition_test_ExoEgo_v{j}_New.json" | |
| # with open(json_path, "r") as f: | |
| # result = json.load(f) | |
| # # 依次合并字典 | |
| # results_new_exo2ego.update(result) | |
| # 处理ego2exo | |
| # list_values = list(range(1, 9)) | |
| # results_new_ego2exo = {} | |
| # for i in list_values: | |
| # i = str(i) | |
| # json_path = f"/scratch/yuqian_fu/competition_Ego2Exo_Final_v{i}.json" | |
| # with open(json_path, "r") as f: | |
| # result = json.load(f) | |
| # # 依次合并字典 | |
| # results_new_ego2exo.update(result) | |
| # save_path = f"/scratch/yuqian_fu/competition_Exo2Ego_Final_all_999.json" | |
| # with open(save_path, "w") as fp: | |
| # json.dump({"ego-exo": {"version": "xx","challenge": "xx", 'results': {}}, "exo-ego": {"version": "xx","challenge": "xx",'results': results_new_exo2ego}}, fp) | |
| # 处理独立的json | |
| json_path = "/scratch/yuqian_fu/report_soccer_new.json" | |
| with open(json_path, "r") as f: | |
| result = json.load(f) | |
| save_path = "/scratch/yuqian_fu/report_soccer_new_tmp.json" | |
| with open(save_path, "w") as fp: | |
| json.dump({"ego-exo": {"version": "xx","challenge": "xx", 'results': result}, "exo-ego": {"version": "xx","challenge": "xx",'results': {}}}, fp) |