Using Pre Trained models for Object Detection

Follow our comprehensive guide with code examples to explore techniques such as object detection, image segmentation, motion analysis, and more. Unlock the potential of computer vision and start exploring the endless possibilities of image and video processing now!

Updated March 18, 2023

Learn more about OpenCV and Computer Vision from this YouTube Channel!

Welcome to this comprehensive tutorial on using pre-trained models for object detection! We’ll explore the theory behind how these models work and provide multiple code examples to demonstrate their capabilities. Our goal is to make this tutorial as engaging and accessible as possible, even for beginners. Let’s dive in!

Introduction to Pre-trained Models

In the world of deep learning, training models from scratch can be time-consuming and computationally expensive. Pre-trained models are a powerful solution to this problem. These models have already been trained on massive datasets and have learned useful features and patterns that can be applied to a variety of tasks, including object detection. By using pre-trained models, you can save significant time and resources while still achieving excellent results.

Object detection is the process of identifying and localizing objects within images or videos. This is an essential task in computer vision, with applications ranging from surveillance and autonomous vehicles to image tagging and content moderation.

In this tutorial, we’ll use pre-trained models from the TensorFlow Object Detection API to perform object detection with OpenCV.

Why Use Pre-trained Models for Object Detection?

There are several reasons to use pre-trained models for object detection:

  1. Faster development: By leveraging pre-trained models, you can start using state-of-the-art object detection techniques without investing time and resources in training a model from scratch.
  2. Reduced computational requirements: Training deep learning models requires powerful hardware, such as GPUs. Pre-trained models allow you to bypass this requirement, making object detection more accessible.
  3. Improved accuracy: Pre-trained models have been trained on large datasets, often consisting of millions of images. This extensive training typically results in models with higher accuracy than custom-trained models on smaller datasets.

Setting Up the Environment

Before we begin, ensure that you have OpenCV and TensorFlow installed. If you haven’t installed them yet, follow the instructions on the official OpenCV installation guide and the official TensorFlow installation guide.

Additionally, you’ll need to install the TensorFlow Object Detection API. Follow the instructions in the official installation guide.

Using Pre-trained Models with OpenCV

In this section, we’ll demonstrate how to use a pre-trained model for object detection with OpenCV.

Step 1: Download a Pre-trained Model

First, download a pre-trained model from the TensorFlow Model Zoo. For this tutorial, we’ll use the ssd_mobilenet_v2_coco model. Extract the downloaded model and note the path to the frozen_inference_graph.pb and mscoco_label_map.pbtxt files.

Step 2: Load the Model

Next, we’ll load the pre-trained model using TensorFlow and OpenCV.

import cv2
import numpy as np
import tensorflow as tf

# Load the pre-trained model
model_path = 'path/to/frozen_inference_graph.pb'
detection_graph = tf.Graph()

with detection_graph.as_default():
    od_graph_def = tf.compat.v1.GraphDef()
    with, 'rb') as fid:
        serialized_graph =
        tf.compat.v1.import_graph_def(od_graph_def, name='')

Step 3: Load the Label Map

Now, we’ll load the label map associated with the pre-trained model. The label map defines the mapping between class IDs and their corresponding class names.

import json
from pathlib import Path
from google.protobuf import text_format
from object_detection.protos import string_int_label_map_pb2

# Load the label map
label_map_path = 'path/to/mscoco_label_map.pbtxt'
label_map = string_int_label_map_pb2.StringIntLabelMap()

with open(label_map_path, 'r') as f:
    label_map_string =
    text_format.Merge(label_map_string, label_map)

# Convert the label map to a dictionary
label_map_dict = {}
for item in label_map.item:
    label_map_dict[] = item.display_name

Step 4: Perform Object Detection

Now that the model and label map are loaded, we’ll perform object detection on an input image.

def detect_objects(image_np, detection_graph):
    with detection_graph.as_default():
        with tf.compat.v1.Session(graph=detection_graph) as sess:
            # Get input and output tensors
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name('num_detections:0')

            # Perform object detection
            (boxes, scores, classes, num) =
                [detection_boxes, detection_scores, detection_classes, num_detections],
                feed_dict={image_tensor: np.expand_dims(image_np, axis=0)})

            # Remove batch dimension
            boxes = np.squeeze(boxes)
            scores = np.squeeze(scores)
            classes = np.squeeze(classes).astype(np.int32)

            return boxes, scores, classes

# Load the input image
input_image = cv2.imread('input_image.jpg')

# Perform object detection
boxes, scores, classes = detect_objects(input_image, detection_graph)

Step 5: Visualize the Results

Finally, we’ll visualize the detected objects on the input image using OpenCV.

def draw_boxes(image_np, boxes, classes, scores, label_map_dict, min_score_thresh=0.5):
    height, width, _ = image_np.shape

    for i in range(len(boxes)):
        if scores[i] > min_score_thresh:
            box = boxes[i]
            class_id = classes[i]
            class_name = label_map_dict[class_id]

            # Draw the bounding box
            y_min, x_min, y_max, x_max = box
            x_min, x_max, y_min, y_max = int(x_min * width), int(x_max * width), int(y_min * height), int(y_max * height)
            cv2.rectangle(image_np, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)

            # Draw the label
            label = f'{class_name}: {scores[i]:.2f}'
            cv2.putText(image_np, label, (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

    return image_np

# Draw the detected objects on the input image
output_image = draw_boxes(input_image, boxes, classes, scores, label_map_dict)

# Display the output image
Detection', output_image)

This code snippet will display the input image with detected objects and their corresponding class names and confidence scores.


In this tutorial, we explored the use of pre-trained models for object detection, discussing the advantages of using them and demonstrating how to use a pre-trained model from the TensorFlow Object Detection API with OpenCV. By leveraging pre-trained models, you can harness the power of state-of-the-art object detection techniques without the need for expensive hardware or extensive training time.

We hope you found this tutorial engaging, informative, and accessible. Keep experimenting with different pre-trained models and input images to further your understanding of object detection with pre-trained models. Happy coding!