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 (get-pip.py)
  • 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 get-pip.py“.
  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 “helloWorld.py”.
  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
    cv2.destroyAllWindows()
    
  5. The run it with F5 in IDLE or with “python helloWorld.py” 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. 🙂

9 thoughts on “People Counter 1 – Installing Python, OpenCV and trying it out”

  1. The counter works so perfectly! Could you briefly explain what algorithm did you employ here? I see that you are creating Blob ID’s. Could you explain how you did that?
    Thanks in advance!

    1. It’s not perfect. At the end of the video you can see it classifies two women that are close to each other as only one person.
      I’ll talk about how it works when we get there in the series.

  2. Pingback: Cialis kaufen
  3. Pingback: Buy cialis
  4. Pingback: Viagra canada

Leave a Reply