Real-Time Human Body Detection OpenCV Python With Source Code

Real-Time Human Body Detection OpenCV Python With Source Code

The Real-Time Human Body Detection OpenCV Python was developed using Python Programming language, this Human Detection OpenCV Python will show how to build your own “smart” video camera. It will show how to take an image from the frame of a web camera, detect if there is a human in the frame, at the same time the system count if how many people or person detect on the video camera.

A Human Body Detection Python OpenCV is an intermediate level deep learning project on computer vision, which will help you to master the concepts and make you an expert in the field of Data Science. Let’s build an exciting project. In this Full Body Detection OpenCV Python, we are going to build the Human Detection and Counting System through Webcam or you can give your own video or images.

There are a variety of computer applications that identify human body in digital images, like – pedestrian crossing, criminal identification, healthcare and so on. The detection program allows us to identify and locate objects. It is very important in area of research, where the detected object can be count, accurately determined. The Detection OpenCV Python library functions are mainly aimed at real-time computer vision. In this article, we are going to develop a Pedestrian detection program. The OpenCV library has inbuilt methods to detect person’s

In this Full Body Detection Python OpenCV also includes a downloadable source code for free, just find the downloadable source code below and click to start downloading.

By the way if you are new to python programming and you don’t know what would be the the Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, Mac OS that will suit for you. I also have here How to Download and Install Latest Version of Python on Windows.

To start creating a Real-Time Human Detection OpenCV Python, make sure that you have installed Python in your computer.

Real-Time Human Body Detection OpenCV Python : Project Prerequisites

The project in Python requires you to have basic knowledge of python programming and the OpenCV library. We will be needing following libraries:

  • OpenCV – A strong library used for machine learning
  • Imutils – To Image Processing
  • Numpy – Used for Scientific Computing. Image is stored in a numpy array.
  • Argparse – Used to give input in command line.

To install the required library, run the following code in your terminal.

Histogram of Oriented Gradient Descriptor

HOG is a feature descriptor used in computer vision and image processing for the purpose of object detection. This is one of the most popular techniques for object detection, to our fortune, OpenCV has already been implemented in an efficient way to combine the HOG Descriptor algorithm with Support Vector Machine or SVM.

Real-Time Human Body Detection OpenCV Python With Source Code : Steps on how to build the project

Time needed: 5 minutes.

These are the steps on how to build Real-Time Human Body Detection OpenCV Python With Source Code

  • Step 1: Import the libraries.

    human body detection import libraries

  • Step 2: Create a model which will detect Humans.

    As discussed earlier, We will use HOGDescriptor with SVM already implemented in OpenCV.  Below code will do this work.
    human body detection create model
    cv2.HOGDescriptor_getDefaultPeopleDetector() calls the pre-trained model for Human detection of OpenCV and then we will feed our support vector machine with it.

  • Step 3: Detect() method.

    A video combines a sequence of images to form a moving picture. We call these images as Frame. So in general we will detect the person in the frame. And show it one after another that it looks like a video.
    That is exactly what our Detect() method will do.  It will take a frame to detect a person in it. Make a box around a person and show the frame..and return the frame with person bounded by a green box.
    human body detection detect method

  • Step 4: HumanDetector() method.

    There are two ways of getting Video.

    1.)Web Camera
    2.)Path of file stored

    In this deep learning project, we can take images also. So our method will check if a path is given then search for the video or image in the given path and operate. Otherwise, it will open the webCam.
    human body detection human detector

  • Step 5: DetectByCamera() method.

    human body detection detect camera
    cv2.VideoCapture(0) passing 0 in this function means we want to record from a webcam. read frame by frame. It returns a check which is True if this was able to read a frame otherwise False.
    Now, For each Frame, we will call detect() method. Then we write the frame in our output file.

  • Step 6: DetectByPathVideo() method.

    This method is very similar to the previous method except we will give a path to the Video. First, we check if the video on the provided path is found or not.
    Note* – A full path must be given.
    human body detection detect by video
    The implementation is similar to the previous function except for each frame we will check that it successfully reads the frame or not. At the end when the frame is not read we will end the loop.

  • Step 7: DetectByPathimage() method.

    This method is used if a person needs to be detected from an image.
    human body detection detect by image

  • Step 8: Argparse() method.

    The function argparse() simply parses and returns as a dictionary the arguments passed through your terminal to our script. There will be Three arguments within the Parser.

    1.)Image: The path to the image file inside your system
    2.)Video: The path to the Video file inside your system
    3.)Camera: A variable that if set to ‘true’ will call the cameraDetect() method.
    human body detection argpasre method

  • Step 9: Main function.

    end of our project.
    human body detection main function
    Instead of declaring our model above, we can declare it in our main function.

Run the Human Body Detection Project

To run the human detection deep learning project, please run below-mentioned commands as per requirements

1. To give video file as input

2. To give image file as input

3. To use the camera

4. To save the output

Real-Time Human Body Detection OpenCV Python : Complete Source Code

Run Quick Virus Scan for secure Download

Run Quick Scan for secure Download

Download Source Code below


In this deep learning project, we have learned how to create a people counter using HOG and OpenCV to generate an efficient people counter. We developed the project where you can supply the input as: video, image, or even live camera. This is an intermediate level project, which will surely help you in mastering python and deep learning libraries.

Related Articles


If you have any questions or suggestions about Real-Time Human Body Detection OpenCV Python With Source Code, please feel free to leave a comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.