People Counter 1 – Installing Python, OpenCV and trying it out

This is the first part of the People Counter tutorial. We´ll look into setting up our environment so we can start coding.

First of all, I’ll be using Windows. I’m doing it this way since I don’t have Linux installed right now on my personal computer but if you wan´t to use other OS it’s fine. You just need to make sure the required tools are installed. The code we write will work in many OS’s with minor modifications. I’ve run this code on my Windows computer, on a Ubuntu Virtual Machine and on my Raspberry Pi B, so don’t worry.

Let’s start! You’ll need to download the following programs:

  • Python 2.7 (lastest version, as of writing, is 2.7.11). Install the 64bit version.
  • OpenCV 3.xx (latest version as of writing is 3.1).
  • Pip (
  • numpy. Be sure to download the 64bit version.

Now let’s install the software:

  1. Install Python 2.7 (64bit version) on the default location “C:\Python27”. Make sure to check the “Add python.exe to Path” option.
  2. Run the openCV executable and extract to “C:\”, where a folder named “opencv” will be created.
  3. Navigate to “C:\opencv\build\python\2.7\x64” and copy the “cv2.pyd” file.
  4. Navigate to “C:\Python27\Lib\site-packages” and paste the file.
  5. Using CMD navigate to where you downloaded pip and run the following command “python“.
  6. The install numpy with the following command “pip install “numpy-1.10.4+mkl-cp27-cp27m-win_amd64.whl”“. The name of your numpy WHL file may vary. You may also need to disable your antivirus software in order for pip to install the package.

And that’s it! We’re set up to start coding the people counter but before that, let’s run an example code to see if everything’s running smooth.

  1. Download the following image to a directory of your choice.Lenna
  2. Open IDLE, the python editor installed by default.
  3. Open a New file (CTRL+N), and save it in the same directory as the image with the name “”.
  4. Add the following code:

    import cv2
    import numpy as np
    img = cv2.imread('Lenna.png') #load RGB image
    imgGS = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert RGB to grayscale
    cv2.imwrite("LennaGS.png",imgGS) #save grayscale image
    ret,imgBW = cv2.threshold(imgGS,127,255,cv2.THRESH_BINARY) #binarization
    cv2.imwrite("LennaBW.png",imgBW) #save binary image
    cv2.imshow('Original image', img) #Display original image
    cv2.imshow('Grayscale image', imgGS) #Display grayscale image
    cv2.imshow('Binary image', imgBW) #Display binarized image
    cv2.waitKey(0) #exit with 'Q' or ESC
  5. The run it with F5 in IDLE or with “python” in CMD.
  6. Three windows are show (exit pressing ‘Q’ or ESC key). Also look at the directory in which you ran the code, two new images are created.

This is the end of the first chapter. In the next one we’ll look into opening a video stream, from a video file or a webcam, and do some operations on it.

If you have any questions, suggestions, complaints, etc. , be sure to leave them on the comment section. 🙂

People Counter with OpenCV Python

Hello everyone, this post marks the start for a series covering the coding of a people counter using OpenCV in Python, just as seen in the following videos:

The series will consist (somewhat) of the following chapters:

  1. Installing Python, OpenCV and Hello World
  2. Opening a video stream/file
  3. Drawing in the stream
  4. Background substraction
  5. Basic filtering
  6. Finding contours
  7. Define a person
  8. Following movement
  9. Counting

I’ll update this post with links and if anything changes.

I´ll also try to post at least one chapter per week.

Let me know if you have any questions on comment section.

Hello world!

Hi! My name is Federico Mejía. I´m a recent graduate from ITESM CEM, where I mayored in Digital Systems and Robotics Engineering.

I’m starting this blog since many people have asked me for the code used in the following video:



To me it just doesn’t feel right to share the code as is, so I’ll be using this space to share a tutorial on it. I’ll also use it to share some of my other projects and maybe some tutorials on them.