|
import os |
|
|
|
|
|
dataset_dirs = ["train", "val", "test"] |
|
base_path = "/path/to/your/dataset" |
|
|
|
|
|
class_names = ["sunglass", "hat", "headphone"] |
|
|
|
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 |
|
|
|
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_image_name = f"{class_name}_{count}.jpg" |
|
new_label_name = f"{class_name}_{count}.txt" |
|
|
|
|
|
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): |
|
os.rename(old_image_path, new_image_path) |
|
|
|
|
|
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!") |
|
|