Upload image_path_to_abspath.py with huggingface_hub
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import jsonimport osimport shutil
# Specify the path to your JSON dataset filejson_file_path = '/path/to/GameQA_RL_5k.json'# Specify the root directory for images. The script will convert this to an absolute path and prepend it to the image paths.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
# 1. Back up the original file 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
# Get the absolute path of the image root directory absolute_image_root = os.path.abspath(image_root_directory)
# 2. Read and update the JSON data 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): # Update image path(s) sample['images'] = [os.path.join(absolute_image_root, img_path) for img_path in sample['images']]
# 3. Write the updated data back to the original file 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()image_path_to_abspath.pyAdded+52