Mask_RCNN icon indicating copy to clipboard operation
Mask_RCNN copied to clipboard

Extracting the coordinates of the predicted polygons after model.detect

Open induparkavi opened this issue 5 years ago • 5 comments

Run object detection

results = model.detect([image], verbose=1)

Display results

ax = get_ax(1) r = results[0] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'], ax=ax, title="Predictions") log("gt_class_id", gt_class_id) log("gt_bbox", gt_bbox) log("gt_mask", gt_mask) How to extract the x,y coordinates of the predicted polygons?

induparkavi avatar Dec 06 '19 09:12 induparkavi

I think this should work for you, I think under segmentation each pair is x,y! I think this was asked before but I couldn't find the question I used this on.

 temp_mask = r['masks'].astype(int)
 # Here we just take the first mask to see if it works
 ground_truth_binary_mask = np.array(temp_mask[:,:,1], dtype=np.uint8)
 fortran_ground_truth_binary_mask = np.asfortranarray(ground_truth_binary_mask)
 encoded_ground_truth = mask.encode(fortran_ground_truth_binary_mask)
 ground_truth_area = mask.area(encoded_ground_truth)
 ground_truth_bounding_box = mask.toBbox(encoded_ground_truth)
 contours = measure.find_contours(ground_truth_binary_mask, 0.5)
 annotation = {
         "segmentation": [],
         "area": ground_truth_area.tolist(),
         "iscrowd": 0,
         "image_id": 123,
         "bbox": ground_truth_bounding_box.tolist(),
         "category_id": 1,
         "id": 1
     }
 
 for contour in contours:
     contour = np.flip(contour, axis=1)
     segmentation = contour.ravel().tolist()
     annotation["segmentation"].append(segmentation)
     
 print(json.dumps(annotation, indent=4))

marcfielding1 avatar Dec 09 '19 12:12 marcfielding1

Hi @induparkavi , This one can help you: https://github.com/cocodataset/cocoapi/issues/39#issuecomment-456641698

hoangphucITJP avatar Dec 23 '19 09:12 hoangphucITJP

I think this should work for you, I think under segmentation each pair is x,y! I think this was asked before but I couldn't find the question I used this on.

 temp_mask = r['masks'].astype(int)
 # Here we just take the first mask to see if it works
 ground_truth_binary_mask = np.array(temp_mask[:,:,1], dtype=np.uint8)
 fortran_ground_truth_binary_mask = np.asfortranarray(ground_truth_binary_mask)
 encoded_ground_truth = mask.encode(fortran_ground_truth_binary_mask)
 ground_truth_area = mask.area(encoded_ground_truth)
 ground_truth_bounding_box = mask.toBbox(encoded_ground_truth)
 contours = measure.find_contours(ground_truth_binary_mask, 0.5)
 annotation = {
         "segmentation": [],
         "area": ground_truth_area.tolist(),
         "iscrowd": 0,
         "image_id": 123,
         "bbox": ground_truth_bounding_box.tolist(),
         "category_id": 1,
         "id": 1
     }
 
 for contour in contours:
     contour = np.flip(contour, axis=1)
     segmentation = contour.ravel().tolist()
     annotation["segmentation"].append(segmentation)
     
 print(json.dumps(annotation, indent=4))

What is "mask" here? Not defined before. @marcfielding1

hamzahkhan avatar Feb 03 '20 17:02 hamzahkhan

Did someone figure this out????

sohinimallick avatar Jun 16 '21 01:06 sohinimallick

I think this should work for you, I think under segmentation each pair is x,y! I think this was asked before but I couldn't find the question I used this on.

 temp_mask = r['masks'].astype(int)
 # Here we just take the first mask to see if it works
 ground_truth_binary_mask = np.array(temp_mask[:,:,1], dtype=np.uint8)
 fortran_ground_truth_binary_mask = np.asfortranarray(ground_truth_binary_mask)
 encoded_ground_truth = mask.encode(fortran_ground_truth_binary_mask)
 ground_truth_area = mask.area(encoded_ground_truth)
 ground_truth_bounding_box = mask.toBbox(encoded_ground_truth)
 contours = measure.find_contours(ground_truth_binary_mask, 0.5)
 annotation = {
         "segmentation": [],
         "area": ground_truth_area.tolist(),
         "iscrowd": 0,
         "image_id": 123,
         "bbox": ground_truth_bounding_box.tolist(),
         "category_id": 1,
         "id": 1
     }
 
 for contour in contours:
     contour = np.flip(contour, axis=1)
     segmentation = contour.ravel().tolist()
     annotation["segmentation"].append(segmentation)
     
 print(json.dumps(annotation, indent=4))

What is "mask" here? Not defined before. @marcfielding1

@hamzahkhan

from pycocotools import mask

firluk avatar May 12 '22 09:05 firluk