import os
import re
# 定义文件夹路径
folder_path = '/path/to/your/folder'
# 定义文件名格式转换函数
def rename_file(filename):
match = re.search(r'_(\d+)_Cam5', filename)
if match:
new_name = match.group(1) + '.png'
return new_name
return filename
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
if filename.endswith('.png'):
new_name = rename_file(filename)
old_file = os.path.join(folder_path, filename)
new_file = os.path.join(folder_path, new_name)
os.rename(old_file, new_file)
print(f'Renamed: {filename} -> {new_name}')
print("All files have been renamed successfully.")
import json
import re
# 定义文件路径
file_path = '/mnt/data/cam5_val_updated_cleaned.json'
# 定义函数移除前导零并提取需要的部分
def process_id(image_id):
match = re.search(r'(\d+)', image_id)
if match:
return match.group(1)
return image_id
def process_filename(file_name):
match = re.search(r'(\d+)_Cam5\.png', file_name)
if match:
return f"{match.group(1)}.png"
return file_name
# 加载JSON数据
with open(file_path, 'r') as file:
data = json.load(file)
# 遍历images并修改id和file_name
for image in data['images']:
image['id'] = process_id(image['id'])
image['file_name'] = process_filename(image['file_name'])
# 遍历annotations并修改image_id
for annotation in data['annotations']:
annotation['image_id'] = process_id(annotation['image_id'])
# 保存修改后的JSON数据
new_file_path = '/mnt/data/cam5_val_updated_cleaned_modified.json'
with open(new_file_path, 'w') as file:
json.dump(data, file, indent=4)
print("JSON文件已成功修改并保存。")
import json
# Load the JSON file
file_path = '/mnt/data/cam5_val_updated_cleaned.json'
with open(file_path, 'r') as file:
data = json.load(file)
# Function to update image ids and file names
def update_image_info(images):
for image in images:
image['id'] = image['id'].split('_')[0]
image['file_name'] = image['file_name'].split('_')[0] + '.png'
return images
# Update the image info in the data
data['images'] = update_image_info(data['images'])
# Update the annotation image_id
for annotation in data['annotations']:
annotation['image_id'] = annotation['image_id'].split('_')[0]
# Save the updated JSON back to the file
updated_file_path = '/mnt/data/cam5_val_updated_cleaned_updated.json'
with open(updated_file_path, 'w') as updated_file:
json.dump(data, updated_file, indent=4)
updated_file_path
import json
# Load the JSON data from the provided file
file_path = "/mnt/data/person_keypoints_val2017.json"
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
# Update the 'images' and 'annotations' fields as requested
for image in data['images']:
if "id" in image:
image['id'] = int(image['id'])
for annotation in data['annotations']:
if "image_id" in annotation:
annotation['image_id'] = int(annotation['image_id'])
if annotation['id'].startswith('mus'):
# skip conversion for 'mus*' ids
continue
else:
annotation['id'] = int(annotation['id'])
# Save the updated JSON data back to a file
updated_file_path = "/mnt/data/updated_person_keypoints_val2017.json"
with open(updated_file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
updated_file_path