# 影像處理 更改dataset名稱 (記得先改錯誤的檔案名稱在moodle公告上) ```python= def rename_ridiculous_dataset(org_image_folder, org_scaphoid_annotation_folder, org_fracture_annotation_folder, dest_folder, prefix="file_"): # Ensure the destination folder exists os.makedirs(dest_folder, exist_ok=True) dest_image_folder = os.path.join(dest_folder, "image") os.makedirs(dest_image_folder, exist_ok=True) dest_scaphoid_annotation_folder = os.path.join(dest_folder, "scaphoid_annotation") os.makedirs(dest_scaphoid_annotation_folder, exist_ok=True) dest_fracture_annotation_folder = os.path.join(dest_folder, "fracture_annotation") os.makedirs(dest_fracture_annotation_folder, exist_ok=True) # List all files in the source folder files_names = os.listdir(org_image_folder) files_names = [file_name.split('.')[0] for file_name in files_names] files_names = sorted(files_names, key=lambda x: int(''.join(filter(str.isdigit, x))) if any(char.isdigit() for char in x) else x) #print(files_names) filename_dict = {} index = 1 count = 0 for idx, name in enumerate(files_names, start=0): file_number = index current_name_number = re.split(r'[- ]+', name)[0] next_name_number = re.split(r'[- ]+', files_names[(idx+1) % len(files_names)])[0] #print(name_number) if 'LA' in name: label = f"LA-{file_number:03}" elif ('AP' in name) | ('OB' in name) | ('SC' in name): label = f"AP-{file_number:03}" else: label = f"fuck" #print(name) filename_dict[name] = label #print('current_numaber:', current_name_number, 'next_number:', next_name_number) if (current_name_number != next_name_number): index += 1 count = 0 elif (current_name_number == next_name_number)&(count == 0): count = 1 else: index += 1 print(name, ':', label) image_path = os.path.join(org_image_folder, name) + ".jpg" new_image_path = os.path.join(dest_image_folder, label) + ".jpg" shutil.copy(image_path, new_image_path) print(f"Renamed and copied: {image_path} -> {new_image_path}") scaphoid_annotation_path = os.path.join(org_scaphoid_annotation_folder, name) + ".json" new_scaphoid_annotation_path = os.path.join(dest_scaphoid_annotation_folder, label) + ".json" shutil.copy(scaphoid_annotation_path, new_scaphoid_annotation_path) print(f"Renamed and copied: {scaphoid_annotation_path} -> {new_scaphoid_annotation_path}") fracture_annotation_path = os.path.join(org_fracture_annotation_folder, name) + ".json" new_fracture_annotation_path = os.path.join(dest_fracture_annotation_folder, label) + ".json" shutil.copy(fracture_annotation_path, new_fracture_annotation_path) print(f"Renamed and copied: {fracture_annotation_path} -> {new_fracture_annotation_path}") print('\n') ``` 使用範例 ``` image_folder = "./ip_homework_data/scaphoid_detection/images" # Replace with your source folder path scaphoid_annotation_folder = "./ip_homework_data/scaphoid_detection/annotations" fracture_annotation_folder = "./ip_homework_data/fracture_detection/annotations" dest_folder = "./dataset" # Replace with your destination folder path rename_ridiculous_dataset(image_folder, scaphoid_annotation_folder, fracture_annotation_folder, dest_folder) ```