import xml.etree.ElementTree as ET def convert_bbox_to_int(xml_file, output_xml_file): # Parse the XML file tree = ET.parse(xml_file) root = tree.getroot() # Loop through all 'object' elements to find bboxes for obj in root.findall('object'): # Find the 'bndbox' element which contains the bbox info bndbox = obj.find('bndbox') if bndbox is not None: # Get the coordinates as float and convert them to int 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)) # Update the XML with the integer values bndbox.find('xmin').text = str(xmin) bndbox.find('ymin').text = str(ymin) bndbox.find('xmax').text = str(xmax) bndbox.find('ymax').text = str(ymax) # Save the modified XML to the output file tree.write(output_xml_file) print(f"Converted bounding boxes saved to {output_xml_file}") # Example usage: 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 # Path to the folder containing XML annotation files annotations_folder = '/path/to/annotations_folder' # Path to save the modified XML annotation files new_annotations_folder = '/path/to/new_annotations_folder' # Create the new folder if it doesn't exist if not os.path.exists(new_annotations_folder): os.makedirs(new_annotations_folder) # Dictionary for renaming labels: {old_label: new_label} label_mapping = { 'old_label_1': 'new_label_1', 'old_label_2': 'new_label_2', # Add more label mappings as needed } # Function to rename labels in the XML file and save to new location def rename_labels_in_xml(xml_file, new_xml_file, label_mapping): tree = ET.parse(xml_file) root = tree.getroot() # Iterate over each object in the XML file for obj in root.findall('object'): label = obj.find('name').text # If label matches any in the label_mapping dictionary, rename it if label in label_mapping: print(f'Renaming label "{label}" to "{label_mapping[label]}" in {xml_file}') obj.find('name').text = label_mapping[label] # Write the updated XML to the new location tree.write(new_xml_file) # Loop through all XML files in the annotations folder 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) # New file path rename_labels_in_xml(xml_path, new_xml_path, label_mapping) print('Label renaming and saving completed.')