Update convert_to_pascalVOC.py
Browse files- convert_to_pascalVOC.py +40 -0
convert_to_pascalVOC.py
CHANGED
@@ -31,3 +31,43 @@ input_xml_file = 'path/to/your/input_annotation.xml'
|
|
31 |
output_xml_file = 'path/to/your/output_annotation.xml'
|
32 |
|
33 |
convert_bbox_to_int(input_xml_file, output_xml_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
output_xml_file = 'path/to/your/output_annotation.xml'
|
32 |
|
33 |
convert_bbox_to_int(input_xml_file, output_xml_file)
|
34 |
+
|
35 |
+
#######################################################################################################
|
36 |
+
|
37 |
+
## for rename labels
|
38 |
+
import os
|
39 |
+
import xml.etree.ElementTree as ET
|
40 |
+
|
41 |
+
# Path to the folder containing XML annotation files
|
42 |
+
annotations_folder = '/path/to/annotations_folder'
|
43 |
+
|
44 |
+
# Dictionary for renaming labels: {old_label: new_label}
|
45 |
+
label_mapping = {
|
46 |
+
'old_label_1': 'new_label_1',
|
47 |
+
'old_label_2': 'new_label_2',
|
48 |
+
# Add more label mappings as needed
|
49 |
+
}
|
50 |
+
|
51 |
+
# Function to rename labels in the XML file
|
52 |
+
def rename_labels_in_xml(xml_file, label_mapping):
|
53 |
+
tree = ET.parse(xml_file)
|
54 |
+
root = tree.getroot()
|
55 |
+
|
56 |
+
# Iterate over each object in the XML file
|
57 |
+
for obj in root.findall('object'):
|
58 |
+
label = obj.find('name').text
|
59 |
+
# If label matches any in the label_mapping dictionary, rename it
|
60 |
+
if label in label_mapping:
|
61 |
+
print(f'Renaming label "{label}" to "{label_mapping[label]}" in {xml_file}')
|
62 |
+
obj.find('name').text = label_mapping[label]
|
63 |
+
|
64 |
+
# Write the updated XML back to file
|
65 |
+
tree.write(xml_file)
|
66 |
+
|
67 |
+
# Loop through all XML files in the annotations folder
|
68 |
+
for filename in os.listdir(annotations_folder):
|
69 |
+
if filename.endswith('.xml'):
|
70 |
+
xml_path = os.path.join(annotations_folder, filename)
|
71 |
+
rename_labels_in_xml(xml_path, label_mapping)
|
72 |
+
|
73 |
+
print('Label renaming completed.')
|