4

I am using Tensorflow object detection API everything is working but I would like to print a dict or array that has the following format {Object name , Score} or something similar all I need is the object name and the score.

I tried with the following code:

with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    # Definite input and output Tensors for detection_graph
    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.
    detection_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.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')
    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)
      # Actual detection.
      (boxes, scores, classes, num) = sess.run(
          [detection_boxes, detection_scores, detection_classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})
      print ([category_index.get(value) for index,value in enumerate(classes[0]) if scores[0,index] > 0.5])

      threshold = 0.5 # in order to get higher percentages you need to lower this number; usually at 0.01 you get 100% predicted objects
      print(len(np.where(scores[0] > threshold)[0])/num_detections[0])

this part is working

  print ([category_index.get(value) for index,value in enumerate(classes[0]) if scores[0,index] > 0.5])

it's printing [{'name': 'computer', 'id': 1}] Is their any way I can add the score of that object to the dict??

I saw another question on Stackoverflow they used:

 threshold = 0.5 # in order to get higher percentages you need to lower this number; usually at 0.01 you get 100% predicted objects
 print(len(np.where(scores[0] > threshold)[0])/num_detections[0])

This is giving me Tensor("truediv:0", dtype=float32) but eventhough if it worked It's not enough because I don't have the name of the object.

Thank you

1 Answer 1

11

So here is the solution that worked for me. (In case you're still looking for a solution, that is)

# The following code replaces the 'print ([category_index...' statement
objects = []
for index, value in enumerate(classes[0]):
  object_dict = {}
  if scores[0, index] > threshold:
    object_dict[(category_index.get(value)).get('name').encode('utf8')] = \
                        scores[0, index]
    objects.append(object_dict)
print objects
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.