1

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?

Engin Todd
  • 27
  • 4
  • Does this answer your question? [Tensorflow object detection API : Multiple Objects coordinates](https://stackoverflow.com/questions/52424348/tensorflow-object-detection-api-multiple-objects-coordinates) – Jitesh Malipeddi Mar 31 '21 at 10:43
  • I was wanting to know how to do this with tensorflow2 – Engin Todd Mar 31 '21 at 10:51

1 Answers1

1

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
      
Jitesh Malipeddi
  • 2,150
  • 3
  • 17
  • 37
  • Hey thank you alough when adding this code I get "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" – Engin Todd Mar 31 '21 at 20:53
  • I had just added your code to the webcam object detection python file – Engin Todd Mar 31 '21 at 20:54
  • I just updated my answer, can you check if it works now? – Jitesh Malipeddi Apr 01 '21 at 01:26
  • Yes this works now. Just to clarify, does this return the coordinates for the object with the highest score? – Engin Todd Apr 01 '21 at 04:13
  • Not for the highest score. As you can see, I used a condition to check if the score is greater than 0.5. However you can easily modify this to get the max value from the scores array and then get the corresponding bounding boxes based on the index – Jitesh Malipeddi Apr 01 '21 at 04:19