Computer Vision module application for finding a target in a live camera
Last Updated :
18 Apr, 2025
NOTE: Using an online compiler is not going to work here. Please install Python 2.7x and cv2, argparse modules to actually try out this example.
For most of us, who are big fans of all those action movies, and games like Modern Warfare, Black Ops, and so on, it has always been a dream to have the opportunity of saying “Target acquired…Waiting for approval”.Team Alpha you are permitted to fire, Mission is a go. Let’s blow it guys!!! Hurrah!” Of course, well, you can’t always get what you want-But, at least now, I can get you close enough to your dream. All you need for this lesson is Python 2.7, cv2 module and it would be great if you have a nice video recorder with the help of which you can get the live video stream. Anyways, it won’t matter even if you don’t have one.
Step – 1: Check your weapons
Download Python 2.7 and ensure that you have the cv2 module (please note that the cv module is old and has been replaced by cv2 ) and the argparse module. For this :
import cv2 as cv
import argparse
If this does not give an error, then you are good to go…
Step – 2: Mission details
Now that you have your weapons with you, it’s time for you to ensure that you have all the mission details required. First, we need to specify our target. So, our target is:

Now that you have your target with you, it’s time to set up a test field. Get several printouts of the target and paste them at several places in your house. Now, if you really want to get the feel, make a quadcopter, fix a small camera in it, and record the whole house properly and ensure that you cover the places where you have pasted the targets. In case if you don’t want to go through all this trouble, just grab a camera and record your house yourself. I would recommend you keep the video short.
Step – 3: Buckle up! We have a mission to complete!
Okay. This is Alpha 1 reporting on duty! Send us the mission coordinates!
You have the target, now you need to acquire it! So, for this, we are going to use the Computer Vision module (cv2).Code Snippet:
Python
import argparse
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
args = vars(ap.parse_args())
# load the video
camVideo = cv2.VideoCapture(args["video"])
# keep looping
while True:
# grab the current frame and initialize the status text
(grabbed, frame) = camVideo.read()
status = "No Target in sight"
# check to see if we have reached the end of the
# video
if not grabbed:
break
# convert the frame to grayscale, blur it, and detect edges
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #grayscale
blurred = cv2.GaussianBlur(gray, (7, 7), 0) #blur
edged = cv2.Canny(blurred, 50, 150) #canny edge detection
# find contours in the edge map
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# loop over the contours
for cnt in cnts:
approx=cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),
True)
if len(approx)==5:
cv2.drawContours(frame, [approx], -1, (0, 0, 255), 4)
status = "Target(s) in sight!"
# draw the status text on the frame
cv2.putText(frame, status, (20, 30), cv2.FONT_HERSHEY_SIMPLEX,
0.5,(0, 0, 255), 2)
# show the frame and record if a key is pressed
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
# if the 's' key is pressed, stop the loop
if key == ord("s"):
break
# cleanup the input recorded video and close any open windows
camVideo.release()
cv2.destroyAllWindows()
Code explained:
We loop over each frame of the recorded video and for detection of our target we convert it to gray-scale, blur it, and finally use the canny edge detection method to find the outlined image.
Remember that, camVideo.read() will return a tuple with the first element specifying whether the frame was read successfully or not, the second element is the actual frame we will be working on!
Now, once you have the frame, we will use contour approximation and then check the number of elements in the output obtained from the previous step. If the number of elements is 5, then we have the regular pentagon that we were looking for, and hence we update the status.
Now this all was quite easy and basic. If you really want to build one such program then you should have a look at various filters to remove noise effects from the frame to get a more accurate result. The best thing you can do is keep experimenting!
Try this exercise at your house, record the video and share your results with us…
Signing off! Peace! Stay safe 🙂
About the author:
Vishwesh Shrimali is an Undergraduate Mechanical Engineering student at BITS Pilani. He fulfills all the requirements not taught in his branch- white hat hacker, network security operator, and an ex – Competitive Programmer. As a firm believer in the power of Python, his majority of work has been in the same language. Whenever he gets some time apart from programming, attending classes, watching CSI Cyber, he goes for a long walk and plays guitar in silence. His motto of life is – “Enjoy your life, ‘cause it’s worth enjoying!”
If you also wish to showcase your blog here, please see GBlog for guest blog writing on GeeksforGeeks.
Similar Reads
Computer Vision Libraries for Python: Features, Applications, and Suitability
Computer Vision allows machines to perceive and interpret the visual world. Computer vision captures images to understand the content and context of what is being seen and enables applications like autonomous driving, augmented reality, and more. Computer vision libraries are the backbone of these a
6 min read
Top 10 Sectors for Computer Vision Applications
Computer Vision has a wide-ranging applications across various domains, from healthcare and automotive industries to retail, agriculture, security, entertainment, education, and environmental monitoring. In this post, we will look into the top 10 sectors where Computer Vision plays a vital role, hig
8 min read
Computer Vision Applications in Robotics
Computer Vision Applications in Robotics have greatly improved what robots can do, allowing them to understand and interact with their surroundings better. This technology is being used in many industries, including manufacturing, healthcare, agriculture, and logistics, making work more efficient an
6 min read
Creating a Finger Counter Using Computer Vision and OpenCv in Python
In this article we are going to create a finger counter using Computer Vision and OpenCv. This is a simple project that can be applied in various fields such as gesture recognition, human-computer interaction and educational tools. By the end of this article you will have a working Python applicatio
5 min read
Applications of Computer Vision
Have you ever wondered how machines can "see" and understand the world around them, much like humans do? This is the magic of computer visionâa branch of artificial intelligence that enables computers to interpret and analyze digital images, videos, and other visual inputs. From self-driving cars to
6 min read
Top Alternatives to OpenCV for Computer Vision
OpenCV is one of the most popular and widely-used libraries for computer vision tasks. However, there are several other libraries and frameworks available that offer alternatives to OpenCV, each with its own set of features, strengths, and weaknesses. In this article, we'll explore some of the top a
3 min read
Use Cases of Computer Vision in Manufacturing
In this rapidly evolving landscape of manufacturing, leveraging advanced technologies is crucial for maintaining a competitive edge. One such technology, computer vision, is transforming the industry by automating and optimizing various processes. This article explores the diverse Use Cases of Compu
6 min read
Difference between Computer Vision API and Custom Vision API
In this article, we will explore Microsoft Azure's Computer Vision API and Custom Vision API, two robust tools for image analysis and processing. What is Computer Vision API?The Computer Vision API is a pre-trained, readily deployable solution that provides a variety of sophisticated image processin
4 min read
A Quick Overview to Computer Vision
Computer vision means the extraction of information from images, text, videos, etc. Sometimes computer vision tries to mimic human vision. Itâs a subset of computer-based intelligence or Artificial intelligence which collects information from digital images or videos and analyze them to define the a
3 min read
What is Object Detection in Computer Vision?
Now day Object Detection is very important for Computer vision domains, this concept(Object Detection) identifies and locates objects in images or videos. Object detection finds extensive applications across various sectors. The article aims to understand the fundamentals, of working, techniques, an
9 min read