Update rename.py
Browse files
rename.py
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# Define dataset paths
|
| 4 |
+
dataset_dirs = ["train", "val", "test"]
|
| 5 |
+
base_path = "/path/to/your/dataset" # Change this to your dataset root directory
|
| 6 |
+
|
| 7 |
+
# Define class names and their mapping
|
| 8 |
+
class_names = ["sunglass", "hat", "headphone"] # Add more classes as needed
|
| 9 |
+
|
| 10 |
+
for dataset in dataset_dirs:
|
| 11 |
+
images_path = os.path.join(base_path, dataset, "images")
|
| 12 |
+
labels_path = os.path.join(base_path, dataset, "labels")
|
| 13 |
+
|
| 14 |
+
if not os.path.exists(images_path) or not os.path.exists(labels_path):
|
| 15 |
+
print(f"Skipping {dataset}: Missing images or labels directory.")
|
| 16 |
+
continue # Skip if directories don't exist
|
| 17 |
+
|
| 18 |
+
for class_name in class_names:
|
| 19 |
+
count = 1
|
| 20 |
+
image_files = sorted([f for f in os.listdir(images_path) if f.lower().endswith(('.jpg', '.png', '.jpeg'))])
|
| 21 |
+
|
| 22 |
+
for filename in image_files:
|
| 23 |
+
label_filename = os.path.splitext(filename)[0] + ".txt"
|
| 24 |
+
|
| 25 |
+
# New names
|
| 26 |
+
new_image_name = f"{class_name}_{count}.jpg"
|
| 27 |
+
new_label_name = f"{class_name}_{count}.txt"
|
| 28 |
+
|
| 29 |
+
# Rename image
|
| 30 |
+
old_image_path = os.path.join(images_path, filename)
|
| 31 |
+
new_image_path = os.path.join(images_path, new_image_name)
|
| 32 |
+
|
| 33 |
+
if not os.path.exists(new_image_path): # Avoid overwriting
|
| 34 |
+
os.rename(old_image_path, new_image_path)
|
| 35 |
+
|
| 36 |
+
# Rename label file if it exists
|
| 37 |
+
old_label_path = os.path.join(labels_path, label_filename)
|
| 38 |
+
new_label_path = os.path.join(labels_path, new_label_name)
|
| 39 |
+
|
| 40 |
+
if os.path.exists(old_label_path) and not os.path.exists(new_label_path):
|
| 41 |
+
os.rename(old_label_path, new_label_path)
|
| 42 |
+
|
| 43 |
+
count += 1
|
| 44 |
+
|
| 45 |
+
print("Renaming completed successfully!")
|