import os # Define dataset paths dataset_dirs = ["train", "val", "test"] base_path = "/path/to/your/dataset" # Change this to your dataset root directory # Define class names and their mapping class_names = ["sunglass", "hat", "headphone"] # Add more classes as needed for dataset in dataset_dirs: images_path = os.path.join(base_path, dataset, "images") labels_path = os.path.join(base_path, dataset, "labels") if not os.path.exists(images_path) or not os.path.exists(labels_path): print(f"Skipping {dataset}: Missing images or labels directory.") continue # Skip if directories don't exist for class_name in class_names: count = 1 image_files = sorted([f for f in os.listdir(images_path) if f.lower().endswith(('.jpg', '.png', '.jpeg'))]) for filename in image_files: label_filename = os.path.splitext(filename)[0] + ".txt" # New names new_image_name = f"{class_name}_{count}.jpg" new_label_name = f"{class_name}_{count}.txt" # Rename image old_image_path = os.path.join(images_path, filename) new_image_path = os.path.join(images_path, new_image_name) if not os.path.exists(new_image_path): # Avoid overwriting os.rename(old_image_path, new_image_path) # Rename label file if it exists old_label_path = os.path.join(labels_path, label_filename) new_label_path = os.path.join(labels_path, new_label_name) if os.path.exists(old_label_path) and not os.path.exists(new_label_path): os.rename(old_label_path, new_label_path) count += 1 print("Renaming completed successfully!")