OpenCV: Object Detection Technology

OpenCV Object Detection

A Guide to Object Detection with OpenCV

In the expansive canvas of computer vision, the ability to perceive and recognize objects is a groundbreaking capability. Welcome to the world of object detection with OpenCV, where algorithms breathe life into pixels, enabling machines to ‘see’ and understand the visual world. In this exploration, we delve into the intricate workings of object detection, from basic shapes to the identification and tracking of complex entities. Join us as we demystify the magic behind OpenCV, unveiling the unseen and opening doors to a future where machines comprehend the visual tapestry as we do.

What’s Covered:

  • What is Object Detection?
  • The SSD MobileNet v2 Convolutional Neural Network
  • Nvidia’s Object Detection Program.
  • Creating our own Object Detection Program.
  • Object Detection on Images.

What is Object Detection?

Object Detection uses computer vision and image processing to identify objects of a specific class. Classes such as humans, vehicles, and animals are regularly monitored using object detection software. Object detection can be used for many tasks such as, vehicle counting, people counting, face detection, image annotation, image retrieval, animal monitoring, monitoring consumer activities and habits, even tracking an objects movement like a ball or a bat in a sports match. Object detection is commonly used everywhere. The consumer giant Amazon uses a robotic arm called “Sparrow” which is programmed using computer vision technology to recognize and handle millions of different products efficiently without human intervention.

The SSD MobileNet v2 Convolutional Neural Network

I will be using the SSD MobileNet v2 neural network, which is a combination of two detection models, SSD and MobileNet. SSD (single shot detector) is a family of algorithms used to create classification and bounding boxes for detected objects. MobileNet v2 is a CNN (convolutional neural network) architecture that does classification, object detection and semantic segmentation. MobileNet v2 is designed to work well on low end devices such as mobile devices, raspberry pi’s and in my case the Jetson Nano.

Together, the MobileNet base works as a feature extractor for the SSD layer which will then classify the detected objects.

NVidia’s Object Detection Program

Below is NVidia’s Object Detection program. It’s very effective and has only 10 lines of code. But as this is a post about OpenCV, my aim is to replicate what Dusty from NVidia has done but using OpenCV instead. To run Dusty’s code, you will need to install the Jetson Inference library for Python.

Python Code:
import jetson.inference
import jetson.utils
net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5) camera = jetson.utils.videoSource('/dev/video0') display = jetson.utils.videoOutput() while True: img=camera.Capture() detections = net.Detect(img) print(detections) display.Render(img) display.SetStatus("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))
Object Detection program

Creating our own Object Detection Program

I tried to replicate NVidia’s Object detection with OpenCV as close as i could, and while they look very similar, mine would only display the data and bounding box around one detection at a time. This was mainly due to the way I used cv2.FILLED to create the bounding box filled with color. I won’t bother sharing the code for the replica yet, as it wasn’t as successful as I hoped.

Object Detection program
The Final Program

My code is full of comments describing each process, so I won’t go into too much detail about the code as we proceed through this post. If you have any questions, please leave them in the comment sections and I will answer them all as soon as possible.

To achieve multiple detections at a time I had to remove the color fill from the bounding boxes. I also changed the bounding box color depending on the confidence values.

Python Code:
import cv2
import jetson_inference
import jetson_utils
import time
import numpy as np

#Create Inference Object
net = jetson_inference.detectNet("ssd-mobilenet-v2",threshold=.5)

#Set Desired Font for Text

#Set up Camera

#Create Objects for FPS Results

while True:

#Grab Frame Height and Width

#Convert Frame to RGBA so it can be Processed by Cuda
img=cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA).astype(np.float32)

#Run Detections on Captured Frame

#Create Loop to Grab Detection Data
for obj in detections:

#Format the Confidence Values

#Create Bounding Box Color Based On Confidence
if myConf2>=75: #Green
elif myConf2<75 and myConf2>=55: #Yellow
elif myConf2<55: #Red

#Add Bounding Box and Text to Detected Object
cv2.rectangle(frame, (left,top), (right,bottom), (objColor), 2)
cv2.putText(frame, str(myConf2)+'%'+' '+ item, (left,top+20), font, .5, (255,255,255), 2)

#Add FPS to Frame
cv2.putText(frame, str(round(fpsAVG,1))+'fps',(10,20),font,.5,(0,0,0),2)

#Display Final Frame
cv2.imshow('Object Detection',frame)

if cv2.waitKey(1)==ord('q'):


As you can see from the images above, multiple detections were successful. If the detection had a confidence value above 75%, the bounding box would be green. If the value was above 55% and less than 75%, the bounding box would be yellow. If the value was less than 55%, the bounding box would be red. I was pleased with the end result, though I do wish I had figured out how to keep the color fill in the bounding boxes, but I will return and make this work.

Object Detection on Images

The code for detecting objects within images is slightly different than on live video, mainly because all the color conversions I had to do, to display the detections.

Python Code:
import cv2
import jetson_inference
import jetson_utils
import numpy as np

#Create Inference Object
net = jetson_inference.detectNet("ssd-mobilenet-v2",threshold=.5)

#Set Image Window Parameters

#Location of images

#Start with first image

while True:
  #read image

#resize image

#Convert image to RGBA so it can be processed by Cuda
imgRGB=cv2.cvtColor(resImg, cv2.COLOR_BGR2RGBA).astype(np.float32)

#Run detections on image
detections=net.Detect(imgRGB, width, height)

#Convert image from Cuda back to Numpy array

#Convert RGBA image to RGB
imgRGB=cv2.cvtColor(imgRGB, cv2.COLOR_RGBA2RGB).astype(np.uint8)

#Convert RGB image back to BGR
imgRGB=cv2.cvtColor(imgRGB, cv2.COLOR_RGB2BGR)

#Display image
cv2.imshow('image', imgRGB)

#Add Pause, press any key to continue

#Next image control

#Exit program if last image has been displayed
if image>8:
print('Program Exited Cleanly')


To my surprise I realized I didn’t need to add the bounding boxes and confidence values, as they appeared without my input, I’m guessing this was applied by the SSD side of the SSD MobileNet v2 neural network. Maybe this was the key to making the perfect replica of Dusty’s Object Detection program. I will hopefully return soon with an updated program.


As we conclude our expedition through the fascinating landscape of object detection with OpenCV, it’s evident that the ability to recognize and understand visual elements is a cornerstone of modern technology. From surveillance systems to autonomous vehicles, the applications are as diverse as the objects themselves. Armed with the insights gained from this journey, you now hold the key to unlocking the potential of object detection with OpenCV. As you venture into your own projects, remember that every detected object tells a story, and OpenCV empowers you to reveal and interpret these narratives.

In the next installment of our OpenCV for Beginners guide we will be learning Color Detection

That’s All Folks!

You can find all of our OpenCV guides here: OpenCV for Beginners

Luke Barber

Hello, fellow tech enthusiasts! I'm Luke, a passionate learner and explorer in the vast realms of technology. Welcome to my digital space where I share the insights and adventures gained from my journey into the fascinating worlds of Arduino, Python, Linux, Ethical Hacking, and beyond. Armed with qualifications including CompTIA A+, Sec+, Cisco CCNA, Unix/Linux and Bash Shell Scripting, JavaScript Application Programming, Python Programming and Ethical Hacking, I thrive in the ever-evolving landscape of coding, computers, and networks. As a tech enthusiast, I'm on a mission to simplify the complexities of technology through my blogs, offering a glimpse into the marvels of Arduino, Python, Linux, and Ethical Hacking techniques. Whether you're a fellow coder or a curious mind, I invite you to join me on this journey of continuous learning and discovery.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights