Update convert_to_pascalVOC.py
Browse files- convert_to_pascalVOC.py +20 -1
convert_to_pascalVOC.py
CHANGED
@@ -59,4 +59,23 @@ def rename_labels_in_xml(xml_file, new_xml_file, label_mapping):
|
|
59 |
tree = ET.parse(xml_file)
|
60 |
root = tree.getroot()
|
61 |
|
62 |
-
# Iterate over each object
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
tree = ET.parse(xml_file)
|
60 |
root = tree.getroot()
|
61 |
|
62 |
+
# Iterate over each object in the XML file
|
63 |
+
for obj in root.findall('object'):
|
64 |
+
label = obj.find('name').text
|
65 |
+
# If label matches any in the label_mapping dictionary, rename it
|
66 |
+
if label in label_mapping:
|
67 |
+
print(f'Renaming label "{label}" to "{label_mapping[label]}" in {xml_file}')
|
68 |
+
obj.find('name').text = label_mapping[label]
|
69 |
+
|
70 |
+
# Write the updated XML to the new location
|
71 |
+
tree.write(new_xml_file)
|
72 |
+
|
73 |
+
# Loop through all XML files in the annotations folder
|
74 |
+
for filename in os.listdir(annotations_folder):
|
75 |
+
if filename.endswith('.xml'):
|
76 |
+
xml_path = os.path.join(annotations_folder, filename)
|
77 |
+
new_xml_path = os.path.join(new_annotations_folder, filename) # New file path
|
78 |
+
rename_labels_in_xml(xml_path, new_xml_path, label_mapping)
|
79 |
+
|
80 |
+
print('Label renaming and saving completed.')
|
81 |
+
|