Last active
October 6, 2022 02:19
-
-
Save Mahmood-Hussain/3f8fe59d05e57a1a7eb44c9ee2d06518 to your computer and use it in GitHub Desktop.
Reorganize/Change indexes of yolo format class labels into new indices. This script could be useful when you have two datasets in yolo(5,...) format with same labels but those labels for two datasets are at different indexes which makes it difficult to test or train on same datasets so this script could be handy in those situations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# yolo label format: class x_center y_center width height | |
import glob | |
def change_labels_index(labels_path, new_class_mappings): | |
"""Change the index of labels to match the new class mappings for yolo""" | |
for label_file in glob.glob(labels_path + "/*.txt"): | |
with open(label_file, "r") as f: | |
lines = f.readlines() | |
with open(label_file, "w") as f: | |
for line in lines: | |
class_index = int(line.split(" ")[0]) | |
new_class_index = new_class_mappings[class_index] | |
f.write(str(new_class_index) + " " + line.split(" ", 1)[1]) | |
""" | |
the keys are original class index and the values are the new class index that you want to map to. | |
Below is the example for RTTS detection dataset to nRain Dataset | |
""" | |
#NOTE: Please set the paths to your own paths | |
labels_path = '/home/mahmood/datasets/RTTSYolo/labels/val' | |
new_class_mappings = { | |
0: 1, | |
1: 3, | |
2: 0, | |
3: 2, | |
4: 4, | |
} | |
change_labels_index(labels_path, new_class_mappings) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment