Reputation: 1559
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.
Upvotes: 2
Views: 3004
Reputation: 53
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)
Upvotes: 1