Nekshay commited on
Commit
98e86c7
·
verified ·
1 Parent(s): d3cf882

Update Autolense_mismatchedXML.py

Browse files
Files changed (1) hide show
  1. Autolense_mismatchedXML.py +42 -1
Autolense_mismatchedXML.py CHANGED
@@ -24,4 +24,45 @@ def update_xml_extension(xml_file, image_path):
24
  # Find the 'filename' tag which contains the image filename
25
  filename_tag = root.find('filename')
26
  if filename_tag is not None:
27
- # Get th
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  # Find the 'filename' tag which contains the image filename
25
  filename_tag = root.find('filename')
26
  if filename_tag is not None:
27
+ # Get the correct extension
28
+ correct_extension = get_correct_extension(image_path)
29
+ current_filename = filename_tag.text
30
+
31
+ # If the extension is incorrect, update it
32
+ if not current_filename.endswith(correct_extension):
33
+ # Replace the old extension with the correct one
34
+ new_filename = current_filename.rsplit('.', 1)[0] + correct_extension
35
+ filename_tag.text = new_filename
36
+
37
+ # Update the path to match the new extension in the 'path' tag (if it exists)
38
+ path_tag = root.find('path')
39
+ if path_tag is not None:
40
+ current_path = path_tag.text
41
+ new_path = Path(current_path).with_name(new_filename)
42
+ path_tag.text = str(new_path)
43
+
44
+ # Save the modified XML file
45
+ tree.write(xml_file)
46
+ print(f"Updated XML: {xml_file} to use {new_filename} and path {new_path}")
47
+
48
+ # Process all XML files in both Train and Test directories
49
+ def process_xml_files(image_dir, annotations_dir):
50
+ for xml_file in Path(annotations_dir).rglob('*.xml'):
51
+ # Get corresponding image file (same name, different extension)
52
+ image_filename = xml_file.stem + '.jpg' # Default to '.jpg' first
53
+ image_path = Path(image_dir) / image_filename
54
+
55
+ # If the .jpg file doesn't exist, try the .jpeg version
56
+ if not image_path.exists():
57
+ image_filename = xml_file.stem + '.jpeg'
58
+ image_path = Path(image_dir) / image_filename
59
+
60
+ # If the image exists, update the XML file
61
+ if image_path.exists():
62
+ update_xml_extension(xml_file, image_path)
63
+ else:
64
+ print(f"Image not found for XML: {xml_file}")
65
+
66
+ # Run for both training and testing datasets
67
+ process_xml_files(image_dir_train, annotations_dir_train)
68
+ process_xml_files(image_dir_test, annotations_dir_test)