Person Counting System Using Opencv and Python

We are a team of IoT & Embedded Systems developers. We are working in Raspberry Pi and Arduino.

Intro: Person Counting System Using Opencv and Python

In this Person Counting System using Opencv and Python project, we are using one raspberry pi and one usb camera for this project.

Step 1: Project Description

People counting system can be implemented in various domains such as libraries, schools, airports, malls.People observation and counting is of interest in many commercial and non-commercial scenarios. The number of people entering and leaving shops, the occupancy of office buildings or the passenger count of commuter trains provide useful information to shop merchants and marketers,security officials or train operators. people counter is a device used to count the number of pedestrians walking through a door or corridor. Most of the time, this system is used at the entrance of a building so that the total number of visitors can be recorded.

Step 2: Here Are Some of the Benefits of Counting People:

When traffic is fluctuating, business is fluctuating. But do you always understand the factors that are affecting traffic.You may think sales reports and a walk around the shopp ing centre or museum tell you all about your visitors and customers. But a people counting system is like having an army of pe ople looking at your building, all the time, every day o f the year. We can help you see trends. We can help you "zoom out" and reach beyond today's sales or visitor figures.

Step 3: Software Used

1.Raspian OS:
This is the recommended os for raspberry pi. You can also installed other OS from third party. Raspbian OS is debian based OS. We can install it from noobs installer. you can get the link from here

2.Putty: PuTTY is an SSH and telnet client, developed originally by Simon Tatham for the Windows platform. PuTTY is open source software that is available with source code and is developed and supported by a group of volunteers. Here we are using putty for accessing our raspberry pi remotely. You can download PuTTY here

3.OpenCv: OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code. The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects and extract 3D models of objects.

Step 4: Hardware Used

You only need two hardware here:
1. Raspberry Pi: This is the latest version of raspberry pi. In this we have inbuilt Bluetooth and wi-fi, unlike previously we have to use Wi-Fi dongle in one of its usb port. There are total 40 pins in RPI3. Of the 40 pins, 26 are GPIO pins and the others are power or ground pins (plus two ID EEPROM pins.) There are 4 USB Port and 1 Ethernet slot, one HDMI port, 1 audio output port and 1 micro usb port and also many other things you can see the diagram on right side. And also we have one micro sd card slot wherein we have to installed the recommended Operating system on micro sd card. There are two ways to interact with your raspberry pi. Either you can interact directly through HDMI port by connecting HDMI to VGA cable, and keyboard and mouse or else you can interact from any system through SSH(Secure Shell). (For example in windows you can interact from putty ssh.) Figure is given above.

2) USB Cameras
USB Cameras are imaging cameras that use USB 2.0 or USB 3.0 technology to transfer image data. USB Cameras are designed to easily interface with dedicated computer systems by using the same USB technology that is found on most computers. The accessibility of USB technology in computer systems as well as the 480 Mb/s transfer rate of USB 2.0 makes USB Cameras ideal for many imaging applications. An increasing selection of USB 3.0 Cameras is also available with data transfer rates of up to 5 Gb/s.

Step 5: Installation of OpenCv

Here are the Installation steps for Python OpenCv on Raspberry Pi:
1) sudo apt-get update

2) sudo apt-get upgrade

3) sudo apt-get install build-essential

4) sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

5)sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

6) sudo apt-get install python-opencv

7) sudo apt-get install python-matplotlib

Step 6: Configure and Enable Camera

Steps description for enabling camera are given above:

Step 7: Project Code

Our source code are given below:

Congratulations you have successfully finished your project;
If have any doubt regarding this project feel free to comment us below or you can mail us on And if you want to learn more about these type of project then feel free to visit our youtube channel : here

Thanks & Regards,

Deligence Technologies



    • Plastics Contest

      Plastics Contest
    • Electronics Tips & Tricks Challenge

      Electronics Tips & Tricks Challenge
    • Audio Contest 2018

      Audio Contest 2018

    5 Discussions


    18 days ago

    Following error i am facing.
    I tried to solve it by different ways but everytime i am not be able to solve it due to lacking in python language knowledge. please guide.

    Traceback (most recent call last):

    File "/home/pi/", line 69, in

    cv2.line(frame, (width/ 2, 0), (width, 450), (250, 0, 1), 2) #blue line

    TypeError: integer argument expected, got float


    8 months ago

    i need the same execution for picamera
    instead of usb cam


    Question 8 months ago on Step 1

    i executed the code given below but the red and blue lines are not in their position as yours
    they moved to right corner


    9 months ago

    Great work!

    I'm wondering if you happen to have developed a version of the code using a Pi camera instead of a USB camera.

    1 reply

    Reply 8 months ago

    Hi, did you make it work ?? Can you help me ? When I run the program I receive this message :
    Traceback (most recent call last):
    File "", line 60, in
    _, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    ValueError: need more than 2 values to unpack