Update convert_to_pascalVOC.py
Browse files- convert_to_pascalVOC.py +33 -0
convert_to_pascalVOC.py
CHANGED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import xml.etree.ElementTree as ET
|
2 |
+
|
3 |
+
def convert_bbox_to_int(xml_file, output_xml_file):
|
4 |
+
# Parse the XML file
|
5 |
+
tree = ET.parse(xml_file)
|
6 |
+
root = tree.getroot()
|
7 |
+
|
8 |
+
# Loop through all 'object' elements to find bboxes
|
9 |
+
for obj in root.findall('object'):
|
10 |
+
# Find the 'bndbox' element which contains the bbox info
|
11 |
+
bndbox = obj.find('bndbox')
|
12 |
+
if bndbox is not None:
|
13 |
+
# Get the coordinates as float and convert them to int
|
14 |
+
xmin = int(float(bndbox.find('xmin').text))
|
15 |
+
ymin = int(float(bndbox.find('ymin').text))
|
16 |
+
xmax = int(float(bndbox.find('xmax').text))
|
17 |
+
ymax = int(float(bndbox.find('ymax').text))
|
18 |
+
|
19 |
+
# Update the XML with the integer values
|
20 |
+
bndbox.find('xmin').text = str(xmin)
|
21 |
+
bndbox.find('ymin').text = str(ymin)
|
22 |
+
bndbox.find('xmax').text = str(xmax)
|
23 |
+
bndbox.find('ymax').text = str(ymax)
|
24 |
+
|
25 |
+
# Save the modified XML to the output file
|
26 |
+
tree.write(output_xml_file)
|
27 |
+
print(f"Converted bounding boxes saved to {output_xml_file}")
|
28 |
+
|
29 |
+
# Example usage:
|
30 |
+
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)
|