Finding the bounding box coordinates in tensorflow object

Hello I'm new to tensorflow object detection api I'm trying to find the bounding box coordinates of objects in my image. I wrote the following code but it is not working.

 def find_bounding_boxes(image, graph):
      with graph.as_default():
        with tf.Session() as sess:
          width, height = image.size 
          boxes = graph.get_tensor_by_name('detection_boxes:0')
          np.squeeze(boxes)
          ymin = boxes[0][1][0]*height
          xmin = boxes[0][1][1]*width
          ymax = boxes[0][1][2]*height
          xmax = boxes[0][1][3]*width
          print ('Top left')
          print (xmin,ymin,)
          print ('Bottom right')
          print (xmax,ymax)

I'm getting the following output:

Top left
Tensor("mul_3:0", dtype=float32) Tensor("mul_2:0", dtype=float32)
Bottom right
Tensor("mul_5:0", dtype=float32) Tensor("mul_4:0", dtype=float32)
Top left
Tensor("mul_7:0", dtype=float32) Tensor("mul_6:0", dtype=float32)
Bottom right
Tensor("mul_9:0", dtype=float32) Tensor("mul_8:0", dtype=float32)
Top left
Tensor("mul_11:0", dtype=float32) Tensor("mul_10:0", dtype=float32)
Bottom right
Tensor("mul_13:0", dtype=float32) Tensor("mul_12:0", dtype=float32)

Upvotes: 1

Views: 2815

Answers (1)

Tong
Tong

Reputation: 77

I am finding how to find the coordinates in tensorflow object detection API too and I found this answer. It worked for me:

width = image.size
height = image.size
i = 1
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)

I don't know how can you defind width, height = image.size, I get an error when I do that.

EDITED: I figured it out, I changed:

from

width, height = image.size

to

width = image.shape[1] height = image.shape[0]

It gived me the true width and height of image. If anyone want to find the coordinates of multiple bounding boxes, follow my answer at this link.

Upvotes: 1

Related Questions