How to crop all object based on mask, Mask-RCNN

Viewed 335

So I was working on instance segmentation but now I am just able to segment 1 of the object from the image.

So I run the below codes to get my output:

image2 = mpimg.imread(path_to_new_image)

# Run object detection
print(len([image2]))
results = model.detect([image2], verbose=1)

# Display results
ax = get_ax(1)
r2 = results[0]
visualize.display_instances(image2, r2['rois'], r2['masks'], r2['class_ids'],
dataset.class_names, r2['scores'], ax=ax, title="Predictions3")

And the output I get is as followed: 1

Then I run the following codes to crop the image based on the mask, but I only able to crop one of it...

2

So how can I crop both of them, instead of 1 mask only?

1 Answers

the reason you are getting only one region cropped is because you're taking your index to be r2['scores'].argmax(). This will only consider that building which has the highest score, blue region in your case.

Change the code from:

idx = r2['scores'].argmax()

To:

for idx in range(len(r2['scores']):

And apply some small modifications to make it run.

Related