Search code examples
pythontensorflowtensorflow2.0object-detection-api

How to get coordinates of best object detected with tensorflow 2?


The accepted answer of this question says how tensorflow draws the bounding boxes of the detected object however does not show or explain how to retrieve these coordinates. Could someone show me how this can be done for tensorflow 2?


Solution

  • You can use most of the code in this documentation here.

    Just add the below code for getting the bounding box coordinates (after detection_classes has been defined)

    width = image_np.shape[1]
    height = image_np.shape[0]
    
    for box,score,cls in zip(detections['detection_boxes'][0],detections['detection_scores'][0],detections['detection_classes'][0]):
        if score >= 0.5: # or any other value
            xmin = box[1]*width
            ymin = box[0]*height
            xmax = box[3]*width
            ymax = box[2]*height