2

I'm a newbie to tensorflow. I'm confused about how to count the number of objects detected using the TensorFlow Object Detection API?

# 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')

if image_np_expanded is not None:
    # Actual detection.
    (boxes, scores, classes, num_detections) = sess.run(
        [boxes, scores, classes, num_detections],
        feed_dict={image_tensor: image_np_expanded}
    )

As num_detections is a numpy.ndarray I have tried to retrieve it's lenght but does not work.

num_detections.size
>> 1

Neither using the tensor lenght:

tf.size(num_detections, out_type=tf.int32)
>> 1

In my case the number of detected objects are more than one.

7
  • Can you say what error you get when you write num_detections.size? Commented Aug 2, 2017 at 23:29
  • It returns 1 but the detected objects are more than one. Commented Aug 2, 2017 at 23:31
  • Try num_detections[0]? Commented Aug 2, 2017 at 23:51
  • num_detections always return 1 as size. To get the number of objects detected I used boxes.shape[0] with a condition about score precision. Thanks for you help Commented Aug 3, 2017 at 14:25
  • It appears that you actually need to count the number of bounding boxes after passing it through a non-maximum suppression function. I don't know what the num_detections is supposed to represent. Commented Mar 7, 2018 at 23:29

1 Answer 1

1

I've been doing a similar process but a different workflow. I rewrote the tutorial code in the object_detection notebook like below. It captures the output of the detection boxes, class, and probability and disregards any of the image processing past the inference. For my situation, I dump the results into a JSON file and import them into another program to complete my analysis (determine numbers of detections) but if python is your thing I'd think you could process "myResults" to get your answer.

myResults = collections.defaultdict(list)
for image_path in TEST_IMAGE_PATHS:
  if os.path.exists(image_path):  
    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.
    output_dict = run_inference_for_single_image(image_np, detection_graph)
    # Visualization of the results of a detection.
    op=get_scores(
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      min_score_thresh=.2)
    myResults[image_path].append(op)  
Sign up to request clarification or add additional context in comments.

1 Comment

Where is get_scores method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.