| import xml.etree.ElementTree as ET |
|
|
| def convert_bbox_to_int(xml_file, output_xml_file): |
| |
| tree = ET.parse(xml_file) |
| root = tree.getroot() |
|
|
| |
| for obj in root.findall('object'): |
| |
| bndbox = obj.find('bndbox') |
| if bndbox is not None: |
| |
| xmin = int(float(bndbox.find('xmin').text)) |
| ymin = int(float(bndbox.find('ymin').text)) |
| xmax = int(float(bndbox.find('xmax').text)) |
| ymax = int(float(bndbox.find('ymax').text)) |
|
|
| |
| bndbox.find('xmin').text = str(xmin) |
| bndbox.find('ymin').text = str(ymin) |
| bndbox.find('xmax').text = str(xmax) |
| bndbox.find('ymax').text = str(ymax) |
|
|
| |
| tree.write(output_xml_file) |
| print(f"Converted bounding boxes saved to {output_xml_file}") |
|
|
| |
| input_xml_file = 'path/to/your/input_annotation.xml' |
| output_xml_file = 'path/to/your/output_annotation.xml' |
|
|
| convert_bbox_to_int(input_xml_file, output_xml_file) |
|
|
| |
|
|
| import os |
| import xml.etree.ElementTree as ET |
|
|
| |
| annotations_folder = '/path/to/annotations_folder' |
|
|
| |
| new_annotations_folder = '/path/to/new_annotations_folder' |
|
|
| |
| if not os.path.exists(new_annotations_folder): |
| os.makedirs(new_annotations_folder) |
|
|
| |
| label_mapping = { |
| 'old_label_1': 'new_label_1', |
| 'old_label_2': 'new_label_2', |
| |
| } |
|
|
| |
| def rename_labels_in_xml(xml_file, new_xml_file, label_mapping): |
| tree = ET.parse(xml_file) |
| root = tree.getroot() |
|
|
| |
| for obj in root.findall('object'): |
| label = obj.find('name').text |
| |
| if label in label_mapping: |
| print(f'Renaming label "{label}" to "{label_mapping[label]}" in {xml_file}') |
| obj.find('name').text = label_mapping[label] |
|
|
| |
| tree.write(new_xml_file) |
|
|
| |
| for filename in os.listdir(annotations_folder): |
| if filename.endswith('.xml'): |
| xml_path = os.path.join(annotations_folder, filename) |
| new_xml_path = os.path.join(new_annotations_folder, filename) |
| rename_labels_in_xml(xml_path, new_xml_path, label_mapping) |
|
|
| print('Label renaming and saving completed.') |
|
|
|
|