OpenCV: Computer Vision for all!

Prepare you and your Pi for OpenCV

1. Connect the camera and test it

PiCamera Documentation

Go to this link and follow the directions to install your camera software.
Section 1 and 2 cover installation, testing and getting started.

Check out section 3 and learn how to capture an image to a file and save a video to a file using python.

NOTE: You can play an .h264 video with the omxplayer application. To use it, type omxplayer  somefilename.h264 from the command line. (Of course, replace somefilename with the name of your file, including the directory path if the file is not in your current directory)

 

Use python to take about a dozen still pictures of you and the room around you,
and also create and save a couple of 10-15 second videos. We can use those later when we do image processing and analysis.


You’ll have to do a fair amount of command line work to get your pi ready for OpenCV. Run the following commands, one at a time, from your command line prompt in the terminal. It may be easiest to copy each line, paste it into the terminal, and press [Enter].
Some of the following statements will show that your Pi is already up-to-date, but
run them anyway to be sure!

2. Install SimpleCV

Command line:

sudo apt-get update
sudo apt-get upgrade

sudo apt-get install ipython python-opencv python-scipy python-numpy python-setuptools python-pip
sudo pip install https://github.com/sightmachine/SimpleCV/zipball/master

2. Install OpenCV

Command line:

sudo pip install svgwrite

***reboot***

sudo apt-get install ipython python-setuptools python-pip
sudo apt-get install lsof
sudo apt-get install python-dev
sudo apt-get install python-opencv
sudo apt-get install libopencv-dev
sudo apt-get install python-numpy
sudo apt-get install python-scipy
sudo apt-get install libatlas-dev
sudo apt-get install libatlas3-base

sudo apt-get update
sudo apt-get upgrade

sudo apt-get install libjpeg-dev
pip install pillow


Run the following command each time you restart your Pi and want to use OpenCV.
It installs the driver for the Pi Camera (OpenCV by default like USB Cameras):

sudo modprobe bcm2835-v4l2

Note: You can add this to your startup procedure by going to your home directory and using:

sudo nano .bashrc

Add the line above (sudo modprobe bcm2835-v4l2) at the bottom of the file. Then reboot and it will run automatically every time you restart!

Tutorials


The following tutorials involve a lot of python code (much of which you can cut-and-paste). I recommend that for each tutorial you start a new python program, and name them in a sequential way, for example Image_01.py, Image_02.py, Video_01.py, etc. so that when camp is done you’ll be able to find the code that does certain things.
Also, many of these tutorials have video at the beginning which outlines what we’ll be doing. If you have earbuds, I encourage you to watch or skim over these videos as they’ll cover most of what you’ll be doing in code.

  • Learn how the Raspberry Pi camera works, and use a built-in program to take still pictures with the camera. Take a number of pictures of yourself and the room round you, for use later on when we do some image processing. Save them in a directory called photos in your home directory.Taking still pictures with the Pi Camera
  • Learn how to use the PiCam for video. Take a few 10-15 second videos of you and the room to use for video processing later. Save them into a directory called videos in your home directory.Capturing video with the Pi Camera
  • This tutorial shows how OpenCV can process images by breaking them down into gray-scale or color components. It also has (for example) line-detection capabilities, which are useful for analyzing the contents of an image.OpenCV with Python Intro and loading Images tutorial
  • Now, learn to do the same with videos: break them down to their components so we can analyze them with our computer tools! Try the following transformations: 
    • cv2.COLOR_BGR2GRAY
    • cv2.COLOR_BGR2HSV
    • cv2.COLOR_BGR2HLS
    • cv2.COLOR_BGR2LAB
    • cv2.COLOR_BGR2LUV
    • cv2.COLOR_BGR2YUV
    • cv2.COLOR_BGR2RGB

     

    Loading Video Source OpenCV Python Tutorial

  • The next tutorial lets us add markup (text and/or images) to our still photos and videos. This means we can overlay the image with circles, boxes, words etc without affecting the original image.Drawing and Writing on Image OpenCV Python Tutorial
  • This final tutorial brings it all together: How to use AI (Artificial Intelligence) to analyze a photo and detect all the faces within.OpenCV with Raspberry Pi Camera Face Detection Tutorial

Coming up next: Recognition!

Four-video sequence

Part 1
Part 2
Part 3
Part 4

and an added bonus: Object Recognition!

Part 1
Part 2
Part 3


Appendix: Useful methods in CV2

cv2.VideoCapture(0) returns cap
cv2.destroyAllWindows()
cv2.imshow(frametitle:string,img:Image)
cv2.cvtColor(img:Image,color:cv2ColorConstant)
cv2.waitKey(nMs:integer) returns char code:int

 

cap.set(3,width:int) set width of cap window
cap.set(4,height) set height of cap window
cap.release() releases capture
cap.read() returns an result, Image tuple from capture. Result is boolean

Intro to images
Getting started with videos

 

Drawing functions in CV

 

Image Processing in OpenCV

 

Changing color spaces in OpenCV


OpenCV and License Plate Recognition

Overall description
Youtube video
Published research paper (the gritty details of how it all works)

Install OpenALPR

  • Install the dependencies
sudo apt-get install autoconf automake libtool libleptonica-dev libicu-dev libpango1.0-dev libcairo2-dev cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev virtualenvwrapper liblog4cplus-dev  

Now run:

sudo apt-get update

and then

sudo apt-get install libcurl4-openssl-dev 

This part comes from my old post: Install OpenALPR on Raspberry PI 3

  • Install Leptonica
cd /usr/src  
sudo wget http://www.leptonica.org/source/leptonica-1.76.0.tar.gz  
sudo tar xf leptonica-1.76.0.tar.gz  

You may need to install these packages:

sudo apt-get install libjpeg-dev libtiff5-dev libpng12-dev gcc make


sudo apt-get update
sudo apt-get upgrade
  

Compile:

cd /usr/src/leptonica-1.76.0
sudo ./configure
sudo make  
sudo make install  
  • Install Tesseract

You also may need to install these packages:

sudoapt-get install ca-certificates git  
sudo apt-get install autoconf automake libtool  
sudo apt-get install autoconf-archive  
sudo apt-get install pkg-config  

If you plan to install the training tools, you also need the following libraries:

sudo apt-get install libicu-dev  
sudo apt-get install libpango1.0-dev  
sudo apt-get install libcairo2-dev 

 

Clone From GIT

cd /usr/src  
sudo git clone https://github.com/tesseract-ocr/tesseract.git  

Check available versions (tags)

cd /usr/src/tesseract  
git tag  

Checkout the version which we need:

sudo git checkout 3.04.01  

Run these commands:

cd /usr/src/tesseract  
sudo ./autogen.sh
sudo ./configure --enable-debug
sudo make  
sudo make install  

You will get the appropriate version:

root@openalpr-tst01:/usr/src/tesseract# tesseract -v  
tesseract 3.04.01  
 leptonica-1.71
  libjpeg 6b : libpng 1.2.50 : libtiff 4.0.3 : zlib 1.2.8
  • Install OpenCV

NOTE: You don’t need to do this if you already have OpenCV installed from Face Detection above!

Download and extract:

cd /usr/src  
sudo wget https://github.com/opencv/opencv/archive/2.4.13.zip  
sudo unzip  2.4.13.zip  

Compile:

cd opencv-2.4.13  
sudo mkdir release  
cd release  
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..  
sudo make  
sudo make install  
  • Install OpenALPR

These packages may be required. They only take a few minutes to install:

sudo apt-get install liblog4cplus-dev libcurl3-dev  
sudo apt-get install beanstalkd  
sudo apt-get install openjdk-7-jdk  
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64/  

Download

cd /usr/src  
sudo git clone https://github.com/openalpr/openalpr.git  

Build:

cd openalpr/src  
sudo mkdir build  
cd build  
sudo cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr -DCMAKE_INSTALL_SYSCONFDIR:PATH=/etc ..  
sudo make  
sudo make install  

Test:

cd ~
wget http://plates.openalpr.com/h786poj.jpg -O lp.jpg  
alpr lp.jpg  

The result must be something like this (Without any errors):

plate0: 8 results  
    - 786P0      confidence: 90.1703
    - 786PO      confidence: 85.579
    - 786PQ      confidence: 85.3442
    - 786PD      confidence: 84.4616
    - 7B6P0      confidence: 69.4531
    - 7B6PO      confidence: 64.8618
    - 7B6PQ      confidence: 64.627
    - 7B6PD      confidence: 63.7444

If you get any type of missing library error at any steps, run ldconfig command.

  • Create OpenALPR Library

cd /usr/src/openalpr/src/bindings/python
sudo python setup.py install

Here is some python code that will use the library to digitize plates in an image.

You may have to modify the part that matches the license plate image filenames.

from openalpr import Alpr
import sys
import glob

def collectFilenames(filepath, filespec):
     return glob.glob(filepath + filespec)



alpr = Alpr("us", "/etc/openalpr/openalpr.conf", "/usr/share/openalpr/runtime_data")
if not alpr.is_loaded():
     print("Error loading OpenALPR")
     sys.exit(1)

alpr.set_top_n(20)
alpr.set_default_region("md")

path = "/home/pi/"  #images are located here
filename = "IMG_19*.JPG" #match images with this name

names = collectFilenames(path,filename)
names.sort()
print names

for filename in names:
     results = alpr.recognize_file(filename)
     i = 0
     for plate in results['results']:
          i += 1
          if (i == 1):
               print("Image : " + filename)
               print("   %12s %12s" % ("Plate", "Confidence"))
               j = 0;
               for candidate in plate['candidates']:
                   j += 1
                   if (j == 1):
                        prefix = "-"
                        if candidate['matches_template']:
                            prefix = "*"

                        print("  %s %12s%12f" % (prefix, candidate['plate'], candidate['confidence']))

# Call when completely done to release memory
alpr.unload()

Here are some license plate images to get you started. Copy them into your home directory.


Installing