Search code examples
tensorflowobject-detection

How to save the bounding boxes to disk as images while using object detection api in tensorflow


Related to this post - Crop image to bounding box in Tensorflow Object Detection API

Below is snippet of code from the tensorflow object detection API sample that I am trying to change

Two questions/issues that I am facing 1) What would be the value of "i" should I use in the boxes if I want the first bounding box image? Is it 0 for first bounding box and 1 for second bounding box?

2) I am getting error on last line when trying to plot the image - plt.imshow "TypeError: Image data can not convert to float"

  ymin = boxes[0,0,0]
  xmin = boxes[0,0,1]
  ymax = boxes[0,0,2]
  xmax = boxes[0,0,3]
  (im_width, im_height) = image.size
  (xminn, xmaxx, yminn, ymaxx) = (xmin * im_width, xmax * im_width, ymin * im_height, ymax * im_height)
  cropped_image = tf.image.crop_to_bounding_box(image_np, int(yminn), int(xminn),int(ymaxx - yminn), int(xmaxx - xminn))
  plt.figure(figsize=IMAGE_SIZE)
  plt.imshow(cropped_image)

Solution

  • cropped_image is a Tensor. You need to evaluate the tensor in a session to get a numpy array. E.g.:

    import tensorflow as tf
    
    # <insert the rest of your graph building code before here>
    cropped_image = ...
    sess = tf.Session()
    img_data = sess.run(cropped_image)
    sess.close()
    
    plt.figure(figsize=IMAGE_SIZE)
    plt.imshow(img_data)