import json import re import pickle import numpy as np def get_image_qa(json_path): image_qa = dict() with open(json_path, 'r') as f: data = json.load(f) for item in data: image_qa['./' + item['image'][0]+ '_' +item['id']] = item return image_qa def read_pkl_file(file_path): try: with open(file_path, 'rb') as file: data = pickle.load(file) return data except FileNotFoundError: print(f"错误:文件 '{file_path}' 不存在。") except pickle.UnpicklingError: print(f"错误:无法解析文件 '{file_path}'。可能不是有效的 Pickle 文件。") except Exception as e: print(f"读取文件时发生错误:{str(e)}") return None file_path = 'nuscenes2d_ego_temporal_infos_val.pkl' data = read_pkl_file(file_path) pkl_info = dict() for info in data['infos']: pkl_info[info['cams']['CAM_FRONT']['data_path']] = info qa_pairs = get_image_qa('test_drivelm.json') new_pkl = [] for qa in qa_pairs: match = re.match(r"(.*\.jpg)", qa) if match: result = match.group(1) pkl_info[result]['qa'] = qa_pairs[qa] new_pkl.append(pkl_info[result]) data['infos'] = new_pkl with open('drivelm_test.pkl', 'wb') as file: pickle.dump(data, file)