Computer Vision : Object detection and tracking using OpenCV and Python

You are currently viewing Computer Vision : Object detection and tracking using OpenCV and Python
  • Post author:
  • Reading time:4 mins read

So what is Computer Vision ?

Computer Vision is the Machine learning technology to process images or videos captured by cameras in the real time.

The images are captured in the form of pixels and the computer vision software converts these pixels and processes this information as required by the application. Computer vision is required to perceive the objects similar to a human vision and then automate the processing tasks.

What is OpenCV ?

OpenCV is a open-source Computer Vision library which contains the programming functions for the image processing applications. OpenCV can be used with C++, Python and Java programming languages.

What is Python ?

Python is a general purpose programming language which has very simple and easily readable syntax, it is used mainly for data science and machine learning projects, though it is used for many other applications like web development, Scientific research, financial industry as well as entertainment industry.  

How to install python with OpenCV in Windows ?

It is very simple to install and start writing codes using python and OpenCV in Windows environment.

1.) Download and run the python from

2.) Open the Windows Command prompt and run the below commands :

3.) pip install opencv-python

4.) pip install numpy

5.) pip install matplotlib

You can search for Python IDLE, which is the IDE for Python wherein you can start writing and executing your codes.


Object detection and tracking code using Python and OpenCV :


# import the necessary packages
from __future__ import print_function
from import VideoStream
import argparse
import imutils
import time
import cv2
import os
import RPi.GPIO as GPIO
#define Servos GPIOs
panServo = 27
tiltServo = 17
# initialize LED GPIO
redLed = 21
GPIO.setup(redLed, GPIO.OUT)
#position servos 
def positionServo (servo, angle):
    os.system("python " + str(servo) + " " + str(angle))
    print("[INFO] Positioning servo at GPIO {0} to {1} degrees\n".format(servo, angle))
# position servos to present object at center of the frame
def mapServoPosition (x, y):
    global panAngle
    global tiltAngle
    if (x < 220):
        panAngle += 10
        if panAngle > 140:
            panAngle = 140
        positionServo (panServo, panAngle)
    if (x > 280):
        panAngle -= 10
        if panAngle < 40:
            panAngle = 40
        positionServo (panServo, panAngle)
    if (y < 160):
        tiltAngle += 10
        if tiltAngle > 140:
            tiltAngle = 140
        positionServo (tiltServo, tiltAngle)
    if (y > 210):
        tiltAngle -= 10
        if tiltAngle < 40:
            tiltAngle = 40
        positionServo (tiltServo, tiltAngle)
# initialize the video stream and allow the camera sensor to warmup
print("[INFO] waiting for camera to warmup...")
vs = VideoStream(0).start()
# define the lower and upper boundaries of the object
# to be tracked in the HSV color space
colorLower = (24, 100, 100)
colorUpper = (44, 255, 255)
# Start with LED off
GPIO.output(redLed, GPIO.LOW)
ledOn = False
# Initialize angle servos at 90-90 position
global panAngle
panAngle = 90
global tiltAngle
tiltAngle =90
# positioning Pan/Tilt servos at initial position
positionServo (panServo, panAngle)
positionServo (tiltServo, tiltAngle)
# loop over the frames from the video stream
while True:
	# grab the next frame from the video stream, Invert 180o, resize the
	# frame, and convert it to the HSV color space
	frame =
	frame = imutils.resize(frame, width=500)
	frame = imutils.rotate(frame, angle=180)
	hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
	# construct a mask for the object color, then perform
	# a series of dilations and erosions to remove any small
	# blobs left in the mask
	mask = cv2.inRange(hsv, colorLower, colorUpper)
	mask = cv2.erode(mask, None, iterations=2)
	mask = cv2.dilate(mask, None, iterations=2)
	# find contours in the mask and initialize the current
	# (x, y) center of the object
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
	cnts = cnts[0] if imutils.is_cv2() else cnts[1]
	center = None
	# only proceed if at least one contour was found
	if len(cnts) > 0:
		# find the largest contour in the mask, then use
		# it to compute the minimum enclosing circle and
		# centroid
		c = max(cnts, key=cv2.contourArea)
		((x, y), radius) = cv2.minEnclosingCircle(c)
		M = cv2.moments(c)
		center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
		# only proceed if the radius meets a minimum size
		if radius > 10:
			# draw the circle and centroid on the frame,
			# then update the list of tracked points, (int(x), int(y)), int(radius),
				(0, 255, 255), 2), center, 5, (0, 0, 255), -1)
			# position Servo at center of circle
			mapServoPosition(int(x), int(y))
			# if the led is not already on, turn the LED on
			if not ledOn:
				GPIO.output(redLed, GPIO.HIGH)
				ledOn = True
	# if the ball is not detected, turn the LED off
	elif ledOn:
		GPIO.output(redLed, GPIO.LOW)
		ledOn = False
	# show the frame to our screen
	cv2.imshow("Frame", frame)
	# if [ESC] key is pressed, stop the loop
	key = cv2.waitKey(1) & 0xFF
	if key == 27:
# do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff \n")
positionServo (panServo, 90)
positionServo (tiltServo, 90)

Farees Ahmed

The aim of this blog is to serve as a quick reference guide for the Curious. Appreciate your feedback and comments !