I'm training a model to recognize hands and want to extract the segmentation masks after detection using the matterport MRCNN (https://github.com/matterport/Mask_RCNN):
model= mrcnn.model.MaskRCNN(mode="inference",
config=SimpleConfig(),
model_dir=os.getcwd())
model.load_weights( filepath="mask_rcnn_0028.h5",
by_name=True)
image = cv2.imread("CARDS_COURTYARD.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = model.detect([image], verbose=0)
r = results[0]
mrcnn.visualize.display_instances(image=image,
boxes=r['rois'],
masks=r['masks'],
class_ids=r['class_ids'],
class_names=CLASS_NAMES,
scores=r['scores'])
Here is an example detection:
MaskRCNN hands detection output image
After detection, I reshape the masks boolean array (saved in the model as r['masks']) so I can access each segmentation mask individually (masks[0] being masks of the first class id, in this case 'yourright'), and save each array as an image:
masks=r['masks']
masks = masks.reshape(2, 720, 1280)
im = Image.fromarray(masks[0])
im.save("mask.jpeg")
My output from this is:
Whilst this is the shape of the segmentation mask, and the dimensions are the same as the original image, the output image is not the segmentation as it appears in the original image. I am looking for the extracted masks to be output as they are overlayed on the original image, and not 'zoomed-in' as they are currently. I assumed because the masks array held the same dimensions of the original image that the masks would retain their position, but apparently not. How can I output the segmentation masks as they appear in the original image?
cheers