Visualize the .xml annotations in python

Clone the repo:
git clone https://github.com/Piyush-Kulkarni/Visualize-the-XML-annotations.git
Organize the data:
- Images (All the images)
- Annotations (All the .xml annotations)
- Visualization (Output folder for images with Bounding Boxes)
Replace the path for the following three variables: image_path, annotation_path & flag
import os
import cv2
import xml.dom.minidom
image_path="C:/Users/piyus/Documents/ObjectDetectionUdemy/PascalVOC-to-Images/data/images/"
annotation_path="C:/Users/piyus/Documents/ObjectDetectionUdemy/PascalVOC-to-Images/data/annotations/"
files_name = os.listdir(image_path)
for filename_ in files_name:
filename, extension= os.path.splitext(filename_)
img_path =image_path+filename+'.jpg'
xml_path =annotation_path+filename+'.xml'
print(img_path)
img = cv2.imread(img_path)
if img is None:
pass
dom = xml.dom.minidom.parse(xml_path)
root = dom.documentElement
objects=dom.getElementsByTagName("object")
print(objects)
i=0
for object in objects:
bndbox = root.getElementsByTagName('bndbox')[i]
xmin = bndbox.getElementsByTagName('xmin')[0]
ymin = bndbox.getElementsByTagName('ymin')[0]
xmax = bndbox.getElementsByTagName('xmax')[0]
ymax = bndbox.getElementsByTagName('ymax')[0]
xmin_data=xmin.childNodes[0].data
ymin_data=ymin.childNodes[0].data
xmax_data=xmax.childNodes[0].data
ymax_data=ymax.childNodes[0].data
print(object)
print(xmin_data)
print(ymin_data)
i= i +1
cv2.rectangle(img,(int(xmin_data),int(ymin_data)),(int(xmax_data),int(ymax_data)),(55,255,155),5)
flag=0
flag=cv2.imwrite("C:/Users/piyus/Documents/ObjectDetectionUdemy/PascalVOC-to-Images/data/Visualization/{}.jpg".format(filename),img)
if(flag):
print(filename,"done")
print("all done ====================================")
This visualization could be useful to check the training or testing data is rightly annotated for Object Detection task.
To learn more: check out
Written by:
(Data Scientist)