Search code examples
pythonopencvimage-processingcomputer-visioncrop

Crop multiple bounding boxes from image with list of bounding boxes


Using Amazon's Rekognition, I have extracted the bounding boxes of interest from the JSON response using the following:

    def __init__(self, image):
        self.shape = image.shape 

    def bounding_box_convert(self, bounding_box):

        xmin = int(bounding_box['Left'] * self.shape[1])
        xmax = xmin + int(bounding_box['Width'] * self.shape[1])
        ymin = int(bounding_box['Top'] * self.shape[0])
        ymax = ymin + int(bounding_box['Height'] * self.shape[0])

        return (xmin,ymin,xmax,ymax)

    def polygon_convert(self, polygon):
        pts = []
        for p in polygon:
            x = int(p['X'] * self.shape[1])
            y = int(p['Y'] * self.shape[0])
            pts.append( [x,y] )

        return pts

def get_bounding_boxes(jsondata):
    objectnames = ('Helmet','Hardhat')
    bboxes = []
    a = jsondata
    if('Labels' in a):
        for label in a['Labels']:

            #-- skip over anything that isn't hardhat,helmet
            if(label['Name'] in objectnames):
                print('extracting {}'.format(label['Name']))


                lbl = "{}: {:0.1f}%".format(label['Name'], label['Confidence'])
                print(lbl)

                for instance in label['Instances']:
                    coords = tmp.bounding_box_convert(instance['BoundingBox'])
                    bboxes.append(coords)

    return bboxes

if __name__=='__main__':

    imagefile = 'image011.jpg'
    bgr_image = cv2.imread(imagefile)
    tmp = Tmp(bgr_image)

    jsonname = 'json_000'
    fin = open(jsonname, 'r')

    jsondata = json.load(fin)
    bb = get_bounding_boxes(jsondata)
    print(bb)

The output is a list of bounding boxes:

[(865, 731, 1077, 906), (1874, 646, 2117, 824)]

I am able to easily extract one position from the list and save as a new image using:

from PIL import Image
img = Image.open("image011.jpg")
area = (865, 731, 1077, 906)
cropped_img = img.crop(area)
cropped_img.save("cropped.jpg")

However, I haven't found a good solution to crop and save multiple bounding boxes from the image using the 'bb' list output.

I did find a solution that extracts the information from a csv here: Most efficient/quickest way to crop multiple bounding boxes in 1 image, over thousands of images?.

But, I believe there is a more efficient way than saving the bounding box data to a csv and reading it back in.

I'm not very strong at writing my own functions - all suggestions are much appreciated!


Solution

  • Assuming your bounding box coordinates are in the form of x,y,w,h you can do ROI = image[y:y+h,x:x+w] to crop. With this input image:

    enter image description here

    Using the script from how to get ROI Bounding Box Coordinates without Guess & Check to obtain the x,y,w,h bounding box coordinates to crop out these ROIs:

    enter image description here

    We simply iterate through the bounding box list and crop it using Numpy slicing. The extracted ROIs:

    enter image description here

    Here's a minimum example:

    import cv2
    import numpy as np 
    
    image = cv2.imread('1.png')
    bounding_boxes = [(17, 24, 47, 47),
                      (74, 28, 47, 50),
                      (125, 15, 51, 61),
                      (184, 18, 53, 53),
                      (247, 25, 44, 46),
                      (296, 6, 65, 66)
    ]
    
    num = 0
    for box in bounding_boxes:
        x,y,w,h = box
        ROI = image[y:y+h, x:x+w]
        cv2.imwrite('ROI_{}.png'.format(num), ROI)
        num += 1
        cv2.imshow('ROI', ROI)
        cv2.waitKey()