0

I'm a newbie in Tensorflow, and looking to the sample object detection codes. One of these is that

I just don't know how can I get the exact coordinates(position) of detected array in image for this sample codes.

Thanks

sundowatch
  • 3,012
  • 3
  • 38
  • 66

2 Answers2

1
# Run the model
    out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
                    sess.graph.get_tensor_by_name('detection_scores:0'),
                    sess.graph.get_tensor_by_name('detection_boxes:0'),
                    sess.graph.get_tensor_by_name('detection_classes:0')],
                   feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)})

     # Visualize detected bounding boxes.
    num_detections = int(out[0][0])
    for i in range(num_detections):
        classId = int(out[3][0][i])
        score = float(out[1][0][i])
        bbox = [float(v) for v in out[2][0][i]]
  • I've done it as you said. But when I print bbox it return this: [0.2702380418777466, 0.4774879515171051, 0.6260198354721069, 1.0]. Are they coordinates? – sundowatch Sep 28 '18 at 16:12
  • 1
    They are relative coordinates.If you want to get absolute coordinates, use the following code.x = bbox[1] * cols y = bbox[0] * rows right = bbox[3] * cols bottom = bbox[2] * rows – rootkitchao Sep 28 '18 at 19:58
  • Do you mean: x is x1; y is y1; right is x2; bottom is y2; cols is image_width and rows is image_height? – sundowatch Sep 28 '18 at 22:58
1

The exact answer: Get the bounding box coordinates in the TensorFlow object detection API tutorial

After this:

out = 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)

Position is:

im_height, im_width = image.shape[:2]

position = boxes[0][0]

(xmin, xmax, ymin, ymax) = (position[1]*im_width, position[3]*im_width, position[0]*im_height, position[2]*im_height)

roi = image2[int(ymin):int(ymax), int(xmin):int(xmax)]

Thanks rootkitchao

sundowatch
  • 3,012
  • 3
  • 38
  • 66