| import json |
| import os |
| import shutil |
|
|
| |
| json_file_path = '/path/to/GameQA_RL_5k.json' |
| |
| image_root_directory = '/path/to/GameQA-5K' |
|
|
|
|
| def main(): |
| """ |
| Main function to back up the JSON file, update image paths to absolute paths, and save the changes. |
| """ |
| if not os.path.exists(json_file_path): |
| print(f"Error: File not found -> {json_file_path}") |
| return |
|
|
| |
| backup_path = json_file_path + '.bak' |
| try: |
| shutil.copy(json_file_path, backup_path) |
| print(f"Original file has been backed up to: {backup_path}") |
| except IOError as e: |
| print(f"Error backing up file: {e}") |
| return |
|
|
| |
| absolute_image_root = os.path.abspath(image_root_directory) |
|
|
| |
| try: |
| with open(json_file_path, 'r') as f: |
| data = json.load(f) |
|
|
| for sample in data: |
| if 'images' in sample and isinstance(sample['images'], list): |
| |
| sample['images'] = [os.path.join(absolute_image_root, img_path) for img_path in sample['images']] |
|
|
| |
| with open(json_file_path, 'w') as f: |
| json.dump(data, f, indent=4, ensure_ascii=False) |
|
|
| print(f"Processing complete. Updated file: {json_file_path}") |
|
|
| except (IOError, json.JSONDecodeError) as e: |
| print(f"Error processing JSON file: {e}") |
|
|
|
|
| if __name__ == '__main__': |
| main() |