The Wayback Machine - https://web.archive.org/web/20240613123345/https://www.geeksforgeeks.org/computer-vision-module-application-for-finding-a-target-in-a-live-camera/
Open In App

Computer Vision module application for finding a target in a live camera

Last Updated : 19 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Sri

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: 

Direct Code Link: https://ide.geeksforgeeks.org/xfUet4 

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

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 processing capabilities without requiring additional setup.
4 min read
Top Applications of Computer Vision in the Retail Sector
Ecommerce is becoming more and more popular these days! After all, there's nothing more convenient than ordering something online and just getting it at your home in a few days. And if you don't like it, you can always return it. While online shopping has all these advantages, it doesn't mean that shopping at real-world stores has become any less p
13 min read
Top 7 Most Popular Computer Vision Tools in 2020
Open your phone and check the first image in your gallery? You can easily identify the people in the image and even describe it to your friend. That’s because it is very easy for humans to see things and describe what they are seeing but is that the same for computers? Not at all! It isn’t that easy for computers to “see” as it is for humans. And t
6 min read
Computer Vision - Introduction
Ever wondered how are we able to understand the things we see? Like we see someone walking, whether we realize it or not, using the prerequisite knowledge, our brain understands what is happening and stores it as information. Imagine we look at something and go completely blank. Into oblivion. Scary right? Well, the secret behind how our brain inte
3 min read
Applications of Computer Vision
Computer vision or machine vision is a field of science that enables computers or devices to recognize different objects just like human beings. the computers need to be trained to detect objects and also some patterns just like you teach a kid to identify the objects but the computers are more efficient as it takes very little time to be trained.
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 attributes. The entire process involves image acqui
3 min read
Solve Sudoku with Computer Vision and Constraint Satisfaction Algorithm
This article explains a program in python 2.7 to solve a Sudoku 9x9 of the Android application "Sudoku" of genina.com. To solve a sudoku of the Android application "Sudoku" of genina.com, a screenshot of the game is taken (a 720x1280 image is obtained), then the number found in each of the 81 squares is obtained using KNN algorithm, once each eleme
9 min read
Essential OpenCV Functions to Get Started into Computer Vision
Computer vision is a process by which we can understand the images and videos how they are stored and how we can manipulate and retrieve data from them. Computer Vision is the base or mostly used for Artificial Intelligence. Computer-Vision is playing a major role in self-driving cars, robotics as well as in photo correction apps. OpenCV is the hug
7 min read
Top 10 Computer Vision Trends To Watch in 2022
The COVID-19 pandemic initially tied the hands of businesses and industries, but sooner each sector started taking the turn of a new digital path. Since then, there has been a rapid change in technology and it’s going to expand more in the upcoming time. Although, with the introduction of technologies like computer vision, there's a significant imp
8 min read
What is Convolution in Computer Vision
In this article, we are going to see what is Convolution in Computer Vision. The Convolution Procedure We will see the basic example to understand the procedure of convolution Snake1: Bro this is an apple (FUSS FUSS) Snake2: Okay but can you give me any proof? (FUSS FUSS FUSS) Snake1: What do you mean, mamma snake told us (FUSS FUSS) Snake2: ������
5 min read