Search code examples
tensorflowobject-detectionthresholdobject-detection-api

Return coordinates that passes threshold value for bounding boxes Google's Object Detection API


Does anyone know how to get bounding box coordinates which only passes threshold value?

I found this answer (here's a link), so I tried using it and done the following:

vis_util.visualize_boxes_and_labels_on_image_array(
    image,
    np.squeeze(boxes),
    np.squeeze(classes).astype(np.int32),
    np.squeeze(scores),
    category_index,
    use_normalized_coordinates=True,
    line_thickness=1,
    min_score_thresh=0.80)

for i,b in enumerate(boxes[0]):
    ymin = boxes[0][i][0]*height
    xmin = boxes[0][i][1]*width
    ymax = boxes[0][i][2]*height
    xmax = boxes[0][i][3]*width
    print ("Top left")
    print (xmin,ymin,)
    print ("Bottom right")
    print (xmax,ymax)

But I noticed that by using answer provided in link - returns all the values. From all the bounding boxes detected by the classifier (which I do not want). What I want is only values from bounding boxes that passes "min_score_thresh".

I feel like this should be very simple, but I do lack knowledge in this area. If I'll find the answer, I'll be sure to post it right here, but if anyone else knows the answer and could save me some time - I would be grateful.


Solution

  • Update: The boxes and scores returned by previous functions are both numpy array objects, therefore you can use boolean indexing to filter out boxes below the threshold.

    This should give you the box that passes the threshold.

    true_boxes = boxes[0][scores[0] > min_score_thresh]
    

    And then you can do

    for i in range(true_boxes.shape[0]):
        ymin = true_boxes[i,0]*height
        xmin = true_boxes[i,1]*width
        ymax = true_boxes[i,2]*height
        xmax = true_boxes[i,3]*width
        print ("Top left")
        print (xmin,ymin,)
        print ("Bottom right")
        print (xmax,ymax)