TensorFlow
Object Detection
API
Image Processing
Console Output

TensorFlow Object Detection API print objects found on image to console

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you only need to print the detected object names to the console, the TensorFlow object detector output is already enough. The essential workflow is: load the model, run inference on the image, read the class IDs and scores, map those IDs to labels, and print the detections above a confidence threshold.

Run the Detector and Inspect the Output Tensors

Exported TensorFlow detection models typically return a dictionary containing keys such as detection_classes, detection_scores, detection_boxes, and num_detections.

A minimal inference script looks like this:

python
1import numpy as np
2import tensorflow as tf
3from PIL import Image
4
5detect_fn = tf.saved_model.load("exported-model/saved_model")
6
7image = np.array(Image.open("image.jpg"))
8input_tensor = tf.convert_to_tensor(image[None, ...], dtype=tf.uint8)
9
10detections = detect_fn(input_tensor)
11num = int(detections["num_detections"][0])
12
13classes = detections["detection_classes"][0, :num].numpy().astype(int)
14scores = detections["detection_scores"][0, :num].numpy()

At this point you already have the raw predictions. The only thing missing is label decoding and thresholding.

Map Class IDs to Human-Readable Labels

The model returns numeric class IDs, not names. For a quick example, a small label map dictionary is enough:

python
1label_map = {
2    1: "person",
3    2: "bicycle",
4    3: "car",
5    17: "cat",
6    18: "dog",
7}

Then print detections above a score cutoff:

python
1threshold = 0.5
2
3for class_id, score in zip(classes, scores):
4    if score >= threshold:
5        label = label_map.get(class_id, f"class_{class_id}")
6        print(f"{label}: {score:.2f}")

That is the core answer to the question. You do not need to draw bounding boxes if your goal is only console output.

A Complete Runnable Example

Putting it together:

python
1import numpy as np
2import tensorflow as tf
3from PIL import Image
4
5label_map = {
6    1: "person",
7    2: "bicycle",
8    3: "car",
9    17: "cat",
10    18: "dog",
11}
12
13detect_fn = tf.saved_model.load("exported-model/saved_model")
14
15image = np.array(Image.open("image.jpg"))
16input_tensor = tf.convert_to_tensor(image[None, ...], dtype=tf.uint8)
17detections = detect_fn(input_tensor)
18
19num = int(detections["num_detections"][0])
20classes = detections["detection_classes"][0, :num].numpy().astype(int)
21scores = detections["detection_scores"][0, :num].numpy()
22
23for class_id, score in zip(classes, scores):
24    if score >= 0.5:
25        print(f"{label_map.get(class_id, class_id)}: {score:.2f}")

If the model sees a person and a dog, the console output might look like:

text
person: 0.98
dog: 0.87

If You Use a Full Label Map File

Real projects often load a label map from the model's metadata or a .pbtxt file rather than hardcoding a dictionary. That is the right move once you support many classes, but the logic stays the same: class ID in, display name out.

So the actual detection-printing path is always:

  1. run inference
  2. read class IDs and scores
  3. map IDs to labels
  4. print labels above the threshold

Modern vs Legacy TensorFlow Code

Older TensorFlow Object Detection API examples often used TensorFlow 1.x sessions, graphs, and utility helpers from notebook code. Modern exported models are more commonly run through tf.saved_model.load, which is much simpler if all you need is inference and console output.

That means you do not need to copy a large notebook pipeline just to print object names.

Common Pitfalls

The biggest pitfall is forgetting to convert detection_classes to integers before using them as label-map keys.

Another common issue is printing every detection regardless of score. Low-confidence predictions create noisy console output, so always apply a threshold.

People also mix the wrong label map with the wrong model. If class IDs and labels do not belong to the same model family, the console output is misleading even though the code runs.

Summary

  • Run the detector and read detection_classes, detection_scores, and num_detections.
  • Convert class IDs to integers and map them to human-readable labels.
  • Print only detections above a sensible confidence threshold.
  • Use tf.saved_model.load for a simple modern inference path.
  • Make sure the label map matches the model you exported.

Course illustration
Course illustration

All Rights Reserved.