import json from PIL import Image import os def convert_data(): try: # 读取 JSON 文件 json_path = '/home1/gmh/InternVL-main/with_llm_confidence.json' with open(json_path, 'r', encoding='utf-8') as f: data = json.load(f) base_dir = '/home1/gmh/profused_benchmark/dataset' # 基础目录 converted_data = [] failed_images = [] # 记录无法加载的图片 possible_extensions = ['.jpg', '.jpeg', '.png', '.bmp'] # 常见图片后缀 for index, sample in enumerate(data['data']): original_data=sample["origin_data"] llm_confidence=original_data['llm_confidence'] if llm_confidence<0.9: continue extend_data = sample['extend_data_1'] image_count = extend_data['image_count'] result = { "id": index } # 判断 extend_data['image'] 是字符串还是列表 image_data = extend_data['image'] if isinstance(image_data, str): # 如果是字符串,直接使用 image_path = image_data elif isinstance(image_data, list) and image_data: # 如果是列表,取第一个元素 image_path = image_data[0] else: # 无效格式,记录错误并使用默认值 print(f"样本 {index} image 格式错误: {image_data}") failed_images.append(str(image_data)) result["image"] = str(image_data) result["width"] = 897 result["height"] = 1152 image_tags = "\n".join([""] * image_count) result["conversations"] = [ { "from": "human", "value": f"{image_tags}\n{extend_data['question']}" }, { "from": "gpt", "value": extend_data['program_answer'] } ] converted_data.append(result) continue # 赋值 image 路径 result["image"] = image_path if image_count == 1: absolute_image_path = os.path.join(base_dir, image_path) print(f"样本 {index}, 图片路径: {image_path}, 绝对路径: {absolute_image_path}") try: # 检查文件是否存在 target_path = absolute_image_path if not os.path.exists(absolute_image_path): # 尝试添加后缀 for ext in possible_extensions: temp_path = absolute_image_path + ext if os.path.exists(temp_path): target_path = temp_path print(f"找到文件: {target_path}") break else: print(f"文件不存在: {absolute_image_path} (也尝试了后缀)") failed_images.append(image_path) result["width"] = 897 result["height"] = 1152 image_tags = "\n".join([""] * image_count) result["conversations"] = [ { "from": "human", "value": f"{image_tags}\n{extend_data['question']}" }, { "from": "gpt", "value": extend_data['program_answer'] } ] converted_data.append(result) continue # 使用 PIL 打开图片 img = Image.open(target_path) img.verify() # 验证图片完整性 img = Image.open(target_path) # 重新打开以获取尺寸 width, height = img.size print(f"图片尺寸: {width}x{height}") result["width"] = width result["height"] = height except Exception as e: print(f"样本 {index} 读取图片 {image_path} 失败: {e}") failed_images.append(image_path) result["width"] = 897 result["height"] = 1152 else: result["image"] = image_data # 保持原始数据(列表) result["width_list"] = [240] * image_count result["height_list"] = [23] * image_count image_tags = "\n".join([""] * image_count) result["conversations"] = [ { "from": "human", "value": f"{image_tags}\n{extend_data['question']}" }, { "from": "gpt", "value": extend_data['program_answer'] } ] converted_data.append(result) if failed_images: print(f"以下图片加载失败: {failed_images}") return converted_data except FileNotFoundError: print(f"错误: 未找到 JSON 文件 {json_path}") except json.JSONDecodeError: print("错误: JSON 文件解析失败") except Exception as e: print(f"发生未知错误: {e}") return [] if __name__ == "__main__": converted = convert_data() if converted: output_file_path = '/home1/gmh/InternVL-main/aaa1.jsonl' # 修改文件后缀为.jsonl with open(output_file_path, 'w', encoding='utf-8') as output_file: for item in converted: output_file.write(json.dumps(item, ensure_ascii=False) + '\n') # 逐行写入 JSON 对象 print(f"转换完成,输出保存至: {output_file_path}") else: print("转换失败,未生成输出文件")