Search code examples
pythonimage-processingobject-detectiondata-annotationsdetection

Create automatic way to convert whole image dataset into annotation file(.xml file)


I have a dataset which is already cropped detecting the roof. (1 image has only 1 roof) ex:- enter image description here

I'm familiar with labelImg tool but it take time.

Is there any way to convert every image file from folder into annotation file like image size as (xmin,ymin,xmax,ymax) and save in PascalVOC format .xml file.


Solution

  • If the images are already cropped, then you should be able to get your bounding boxes by inspecting the images.

    from PIL import Image
    
    paths_of_images_in_training_directories = #recursive directory walk of training dir
    
    for training_image in paths_of_images_in_training_directories:
        img = Image.open(training_image)
        image_w, image_h = img.size
    
        # Determine xmin,ymin,xmax,ymax or annotations
        xmin = 0
        ymin = 0
        xmax = image_w
        ymax = image_h
    
        # ...write annotations to .xml file