Get the bounding box coordinates in the TensorFlow object detection API tutorial
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When using the TensorFlow Object Detection API, the model returns predictions that include bounding box coordinates for each detected object. These coordinates are normalized values between 0 and 1, representing positions relative to the image dimensions. To draw boxes on the original image or use the detections in downstream logic, you need to convert these normalized values into actual pixel coordinates. This article walks through the entire process, from loading a model to extracting and converting bounding box coordinates, with complete code examples.
What Are Bounding Box Coordinates
A bounding box is a rectangle that surrounds a detected object in an image. The TensorFlow Object Detection API represents each box with four values: ymin, xmin, ymax, and xmax. These are normalized, meaning they fall between 0 and 1.
For example, a box with coordinates [0.1, 0.2, 0.5, 0.8] means:
- The top edge is at 10% of the image height from the top
- The left edge is at 20% of the image width from the left
- The bottom edge is at 50% of the image height
- The right edge is at 80% of the image width
To get pixel values, you multiply ymin and ymax by the image height, and xmin and xmax by the image width.
Loading the Model and Running Inference
First, load a pretrained model and run it on an input image:
The detections dictionary contains several keys. The ones relevant to bounding boxes are:
detection_boxes: A tensor of shape(1, N, 4)with normalized coordinatesdetection_scores: Confidence scores for each detectiondetection_classes: Class IDs for each detectionnum_detections: How many valid detections were found
Extracting and Converting Coordinates
Here is how to extract the bounding boxes and convert them to pixel coordinates:
The threshold of 0.5 filters out low-confidence detections. You can adjust this value based on your application. A lower threshold catches more objects but includes more false positives. A higher threshold is more selective but may miss valid detections.
Drawing Bounding Boxes on the Image
Once you have pixel coordinates, you can draw the boxes on the image using PIL or OpenCV:
For OpenCV, the equivalent code looks like this:
Extracting Cropped Objects
You can also crop each detected object from the original image for further processing:
This is useful for tasks like license plate reading, where you first detect the plate and then pass the cropped region to an OCR model.
Working with the Label Map
The detection_classes tensor contains integer IDs. To get human-readable class names, you need the label map file that came with the model:
Common Pitfalls
Confusing the coordinate order. TensorFlow uses [ymin, xmin, ymax, xmax], not [xmin, ymin, xmax, ymax]. Swapping x and y will produce boxes that are rotated or positioned incorrectly. Always double-check the order.
Forgetting that coordinates are normalized. If you use the raw values (0 to 1) as pixel coordinates, your boxes will be clustered in the top-left corner of the image. Always multiply by the image dimensions.
Not filtering by score. The model returns a fixed number of detections (often 100), most of which have very low confidence scores. Without a threshold filter, you will draw dozens of incorrect boxes.
Using the wrong image dimensions. If you resize the image before inference but use the original dimensions for coordinate conversion, the boxes will be misaligned. Always use the dimensions of the original image for the conversion, since the normalized coordinates are relative to whatever size the model saw.
Summary
The TensorFlow Object Detection API returns bounding boxes as normalized coordinates in [ymin, xmin, ymax, xmax] order. To convert them to pixel coordinates, multiply ymin and ymax by the image height, and xmin and xmax by the image width. Filter detections by confidence score, use the label map for class names, and be mindful of the coordinate order. With these steps, you can extract, visualize, and crop detected objects from any image.
Related reading
- Get the diagonal of a matrix in TensorFlow
- Get the last output of a dynamic_rnn in TensorFlow
- Get the value of some weights in a model trained by TensorFlow
- Getting around tf.argmax which is not differentiable
- Getting error Could not import PIL.Image. The use of array_to_img requires PIL.
- Getting image dimensions without reading the entire file
- Get the data received in a Flask request
- Get the data received in a Flask request

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.