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!