Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow Object detection API: Print detected class as output to terminal

I have a simple question, but I can't figure out how to do it. I am using the TF Object detection API to detect images, it is working fine and given an image it will draw the bounding box with a label and confidence score of what class it thinks its detected. My question is how can I print the detected class (as a string) and the score to terminal i.e not just on the image but as an output to the terminal too.

Below is the code responsible for the image detection

with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    for image_path in TEST_IMAGE_PATHS:
      image = Image.open(image_path)
      # the array based representation of the image will be used later in order to prepare the
      # result image with boxes and labels on it.
      image_np = load_image_into_numpy_array(image)
      # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
      image_np_expanded = np.expand_dims(image_np, axis=0)
      image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
      # Each box represents a part of the image where a particular object was detected.
      boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
      # Each score represent how level of confidence for each of the objects.
      # Score is shown on the result image, together with the class label.
      scores = detection_graph.get_tensor_by_name('detection_scores:0')
      classes = detection_graph.get_tensor_by_name('detection_classes:0')
      num_detections = detection_graph.get_tensor_by_name('num_detections:0')
      # Actual detection.
      (boxes, scores, classes, num_detections) = sess.run(
          [boxes, scores, classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})
      # Visualization of the results of a detection.
      vis_util.visualize_boxes_and_labels_on_image_array(
          image_np,
          np.squeeze(boxes),
          np.squeeze(classes).astype(np.int32),
          np.squeeze(scores),
          category_index,
          use_normalized_coordinates=True,
          line_thickness=8, min_score_thresh=.2)
      plt.figure(figsize=IMAGE_SIZE)
      plt.imshow(image_np)
      plt.show()

Thanks in advance, first post on Stack Overflow so please go easy on me

like image 213
Omar Avatar asked Jul 24 '17 14:07

Omar


People also ask

Is TensorFlow good for object detection?

But, with recent advancements in Deep Learning, Object Detection applications are easier to develop than ever before. TensorFlow's Object Detection API is an open source framework built on top of TensorFlow that makes it easy to construct, train and deploy object detection models.


2 Answers

Well that's very easy. The classes are encrypted in the category_index which is a dict, so you could do something like this:

with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
for image_path in TEST_IMAGE_PATHS:
  image = Image.open(image_path)
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = load_image_into_numpy_array(image)
  # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  image_np_expanded = np.expand_dims(image_np, axis=0)
  image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
  # Each box represents a part of the image where a particular object was detected.
  boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
  # Each score represent how level of confidence for each of the objects.
  # Score is shown on the result image, together with the class label.
  scores = detection_graph.get_tensor_by_name('detection_scores:0')
  classes = detection_graph.get_tensor_by_name('detection_classes:0')
  num_detections = detection_graph.get_tensor_by_name('num_detections:0')
  # Actual detection.
  (boxes, scores, classes, num_detections) = sess.run(
      [boxes, scores, classes, num_detections],
      feed_dict={image_tensor: image_np_expanded})

  # Here output the category as string and score to terminal
  print([category_index.get(i) for i in classes[0]])
  print(scores)
like image 123
Dat Tran Avatar answered Sep 16 '22 22:09

Dat Tran


Simply, go to the utils directory in object_detection folder and open the script visualization_utils.py. You will find a function namely visualize_boxes_and_labels_on_image_array, add a print command in the end of function to print the variable class_name(print(class_name)). Now run your code and see the magic.

like image 31
Ravish Kumar Sharma Avatar answered Sep 20 '22 22:09

Ravish Kumar Sharma