opencvdeep-learningcomputer-visionobject-detectiondetectron

Is it possible to get the number of pixels of the detected object with Detectron2?


I'm totally new to deep learning. I have a Detectron2 model detecting solar panels located on rooftops. I'd love to calculate the surface of the detected panels, therefore I need to get the number of pixels of detected objects. Is there any way to do it?


Solution

  • If you use official framework and segmentation over detections (for more precise surface estimation) on image im like:

    outputs = predictor(im)
    

    Dict outputs has the key instances (you can see source code) - you can just take the masks boolean tensors and calculate its sum:

    masks = outputs['instances'].pred_masks
    masks.shape
    
    # torch.Size([15, 480, 640])
    

    So we have here 15 objects and boolean masks size of input image as masks for each object, now we calculate total amount of pixels for each detected object:

    torch.sum(torch.flatten(masks, start_dim=1),dim=1)
    
    # tensor([32251,  8786,  2513, 11821,  1395,  2129,   496,  2722,  1020,   571,
         1060,   806,  2006,   535,   297], device='cuda:0')