image-segmentationimage-recognitionfaster-rcnndetectronlabelimg

How to extract or get the image bounded by Detectron


I am working on creating bounding boxes upon images with my own created training dataset with the help of Detection, while I'm now stuck at the part of extracting the bounded image. I just want the image of the part inside the bounding box. The input image to predicted.

The predicted image with the bounding box outlines.

Please help me with this query.The resultant image should be like this.


Solution

  • Detection Function in Tensorflow

     # Detection Function
     detections = detect_fn(input_tensor)
    
     bscores = detections['detection_scores'][0].numpy()
     bclasses = detections['detection_classes'][0].numpy().astype(np.int32)
     bboxes = detections['detection_boxes'][0].numpy()
    
     det_boxes, class_labels = ExtractBBoxes(bboxes, bclasses, bscores, im_width, im_height, image_name=image_file)
    

    Method to extract and crop bounding box

    def ExtractBBoxes(bboxes, bclasses, bscores, im_width, im_height, image_name):
     bbox = []
     class_labels = []
     for idx in range(len(bboxes)):
        if bscores[idx] >= Threshold:
          #Region of Interest
          y_min = int(bboxes[idx][0] * im_height)
          x_min = int(bboxes[idx][1] * im_width)
          y_max = int(bboxes[idx][2] * im_height)
          x_max = int(bboxes[idx][3] * im_width)
          class_label = category_index[int(bclasses[idx])]['name']
          class_labels.append(class_label)
          bbox.append([x_min, y_min, x_max, y_max, class_label, float(bscores[idx])])
    
          #Crop Image
          cropped_image = tf.image.crop_to_bounding_box(image, y_min, x_min, y_max - y_min, x_max - x_min).numpy().astype(np.int32)
          output_image = tf.image.encode_jpeg(cropped_image) #For Jpeg
          score = bscores[idx] * 100
          # Create a constant as filename
          file_name = tf.constant(youfilename)
          file = tf.io.write_file(file_name, output_image)