How to Draw a Bounding Box Prediction Label with Python

Computer vision models, by default, return the positions of predictions returned by the model, not a visual bounding box on an image. Drawing boxes showing the location of predictions, and adding labels to those boxes, is a post-processing step which requires additional code.
Once you have drawn bounding boxes – a topic about which we have written in the past – you may be wondering: how can I visualize a prediction? That’s the question we’re going to answer in this tutorial.
Let’s discuss how to draw a bounding box prediction using Python and the supervision library. Here is the end result we'll achieve:

How to Draw a Bounding Box Label in Python
If you don't already have a model, scroll down to the bottom of this article where we'll show using an example model from Roboflow Universe (opens in a new tab).
To draw a bounding box, you’ll need:
- The starting x position of the box.
- The starting y position of the box.
- The width of the prediction.
- The height of the prediction.
Here is the raw image to which we want to add bounding boxes and labels:

Below, we use the “predictions” list from the last section which contains all of this information. You can substitute the variables below as necessary to make the code work on your project. Here is the code we'll need to draw our bounding boxes and add labels above each bounding box:
import cv2
import supervision as sv
image = cv2.imread("image.jpg")
predictions = {"predictions": [...]} # add your predictions here
detections = sv.Detections.from_inference(predictions)
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
image = box_annotator.annotate(scene=image, detections=detections)
image = label_annotator.annotate(scene=image, detections=detections)
cv2.imwrite("example_with_bounding_boxes.jpg", image)This code converts the predictions into a supervision Detections object, draws a box around each prediction with BoxAnnotator, and adds a label with the predicted class name above each box with LabelAnnotator. Both annotators take optional arguments such as color, thickness, text_scale, and text_padding if you want to change how the boxes and labels look.
Let's run our code using the ladders detection model (discussed in the next section):

Now all of our bounding boxes show a label at the top.
Setting Up a Model (Optional)
In the guide above, we drew bounding box labels around a model that detects people and ladders. If you want to use the people and ladder detection models for testing, first sign up to Roboflow and then go to our People and Ladders Universe dataset (opens in a new tab). This dataset comes with a free-to-use trained model.

Click “Model” in the sidebar, scroll down and copy the code that you see into a Python file. The code will look something like this, with your API key:
from roboflow import Roboflow
rf = Roboflow(api_key="API_KEY")
project = rf.workspace().project("people-and-ladders")
model = project.version(4).modelThis code imports your model. Add a new line at the bottom to run inference on a local file:
predictions = model.predict(“image.jpg”, confidence=50).json()The “predictions” variable now contains all of the predictions returned by our model that were rated with a confidence level equal to or greater than 50%. Now we have a model on whose predictions we can draw bounding boxes and labels.
Conclusion
In this tutorial, we have discussed:
- How to use Roboflow Universe to use a trained public model;
- How to retrieve predictions from the model;
- How to draw bounding boxes around a model and;
- How to draw labels above each bounding box.
Now you have all of the information you need to represent numerical coordinates data visually in your computer vision work. Happy creating!