Tuesday, October 1, 2013

Playing with Python and OpenCV

For one of my products I need to work with Python and do some image processing. The goal is to do face detection. One of the best things about Python is the support available for it and the number of libraries ported for Python. It hardly took me a day to do face detection using Python and of course, it isn't because of me, but what is already available online. 

In this tutorial I'm going to take you step by step in setting up Python and then setting up OpenCV for python and running an example for loading an image and writing on it. Maybe in the next example I will explain face detection along with an example which by the way is available online from many sources.

Step 1: Downloading python for Windows

The first step is to setup python on your notebook / PC by downloading from the Download Page. In my case I have a 64 bit windows 7 machine so I downloaded the 64 bit version of Python 2.7.5. for windows. You can download for Mac Os or Linux as well. There are two current production versions of python which are 2.7.5 and 3.3.2. If you want to understand the difference between the both and decide which one to work with, have a look at this article


Step 2: Installing Python

The second step is to install Python. If you have downloaded the MSI package it simply installs Python in C:\PythonX where X stands for version number. In my case it is C:\Python27. I would recommend not to change this folder as many libraries that you may install look for this folder, but most detect the installation path of Python from the registry so changing the folder won't be that big a problem.

Step 3: Installing OpenCV

A good repository of python libraries has been maintained by Christoph Gohlke which have also been compiled for 64 bit architecture for windows. Although these are unofficial but they work fine, at least for me. So I downloaded the opencv-python 2.4.6 for python 2.7 compiled for 64 bit architecture (link). You can find your own link on the same page. The downloaded file is an MSI package so simply double clicking on it will run a setup which will automatically detect the Python installation path and install the opencv library in the appropriate directories. I ran into one problem while trying to run my first opencv example. You will see the following error if you don't have Numpy.

from cv2.cv import *
rtError:numpy.core.multiarray failed to import

It requires the Numpy-MKL 1.7.Once you install Numpy from the same repository, openCV will run just fine as it did for me.
  
Step 4: Writing the first example

 Here is a simple python example that will open an image file and write text on it. The example with more details is available (here)



  1. import cv          #Import functions from OpenCV
  2. cv.NamedWindow('a_window', cv.CV_WINDOW_AUTOSIZE)
  3. image=cv.LoadImage('image.png', cv.CV_LOAD_IMAGE_COLOR) #Load the image
  4. font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8) #Creates a font
  5. x = 10 #position of text
  6. y = 20 #position of text
  7. cv.PutText(image,"Hello World!!!", (x,y),font, 255) #Draw the text
  8. cv.ShowImage('a_window', image) #Show the image
  9. cv.Waitkey(10000)
  10. cv.SaveImage('image2.png', image) #Saves the images

Line 1 imports opencv into the program. If it hasn't installed correctly you will get an error at this line.
Line 2 creates a window which is named a_window. You can change it to any name and the second attribute of the function is self explanatory. It creates an auto sized window.
 Line 3 loads the Image image.png. You can either give the whole path or simply copy the image in the directory of your program. 
Line 4 initializes the font
Line 5 and Line 6 are two variables which contain the position of the text to be displayed on the picture
 Line 7 draws the text in double quotes onto the image. The first argument is the image source and the double quotes is the text, the thirst argument being the position, 4th being the font. 
Line 8 Shows this window onto your screen
Line 9 waits for a key to be pressed. If this line is missing the program simply exits.
Line 10 saves this image into a new file.



You can simply copy paste this code in a notepad application of Windows or I would recommend using  Notepad++. You can also use the IDLE IDE installed with python or there is another one called Pycharm which is licensed. You must save the file with the extension ".py". Windows automatically associates all ".py" extension files to be run with the Python interpreter. You can either run it by double clicking it or open the command prompt on windows, go into Python's directory (C:\Python27) and type "python progarm1.py" on your console (given you have saved the program with this filename). If you have added the installation path of python to your windows PATH variable then you can simple type "python programname.py" in any directory while in the command prompt window.

Once this example is run, the following image is saved as image2.png. Both the original and the modified images are shown below



No comments:

Post a Comment