• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomepcDuinoCameraInstall OpenCV on pcDuino and Two Sample Applicati ...
Previous Next

Install OpenCV on pcDuino and Two Sample Applications

Posted by: admin , December 6, 2014

OpenCV is an open source computer vision package based on Python. In this post, we are going to detail the steps to install OpenCV on pcDuino. Two examples are given, one is used to capture an image through the USB camera, the other is to use OpenCV to to face recognition.

Installation Steps:

$ sudo apt-get -y install build-essential cmake cmake-qt-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools
 
$sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils swig libv4l-0 libv4l-dev python-numpy libpython2.6 python2.6-dev libgtk2.0-dev pkg-config
 
$sudo apt-get install libopencv-dev python-opencv
 
$sudo apt-get install python-dev

$sudo apt-get install libpython2.7 python2.7-dev
$sudo apt-get install python-setuptools python-pip
 
$sudo ln -s /usr/lib/arm-linux-gnueabihf/libjpeg.so /usr/lib
$sudo ln -s /usr/lib/arm-linux-gnueabihf/libfreetype.so /usr/lib
$sudo ln -s /usr/lib/arm-linux-gnueabihf/libz.so /usr/lib 
 
$sudo easy_install PIL
$sudo pip install -v PIL



If the above comand shows error:

root@ubuntu:/home/ubuntu# sudo pip install -v PIL

Requirement already satisfied (use --upgrade to upgrade): PIL in /usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-armv7l.egg

Cleaning up...

 

Change the command to:

$sudo pip install -v PIL --upgrade

Example 1: Capture Image using OpenCV

#!/Users/brent/.virtualenvs/lumber/bin/python
 
import cv
 
cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
 
gx = gy = 1
grayscale = blur = canny = False
 
def repeat():
    global capture #declare as globals since we are assigning to them now
    global camera_index
    global gx, gy, grayscale, canny, blur
    frame = cv.QueryFrame(capture)
    # import pdb; pdb.set_trace()
 
    if grayscale:
        gray = cv.CreateImage(cv.GetSize(frame), frame.depth, 1)
        cv.CvtColor(frame, gray, cv.CV_RGB2GRAY)
        frame = gray
 
    if blur:
        g = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, frame.channels)
        cv.Smooth(frame, g, cv.CV_GAUSSIAN, gx, gy)
        frame = g
 
    if grayscale and canny:
        c = cv.CreateImage(cv.GetSize(frame), frame.depth, frame.channels)
        cv.Canny(frame, c, 10, 100, 3)
        frame = c
    cv.ShowImage("w1", frame)
 
    c = cv.WaitKey(10)
    if c==ord('='): #in "n" key is pressed while the popup window is in focus
        gx += 2
        gy += 2
    elif c == ord('-'):
        gx = max(1, gx-2)
        gy = max(1, gy-2)
    elif c == ord('x'):
        gx += 2
    elif c == ord('X'):
        gx = max(1, gx-2)
    elif c == ord('q'):
        exit(0)
 
    elif c == ord('b'):
        blur = not blur
    elif c == ord('g'):
        grayscale = not grayscale
    elif c == ord('c'):
        canny = not canny
 
while True:
    repeat()

Sample 2: Face Recognition

Input Image:

 

Download the above image and save as  “opencv_in.jpg”

 

Output Image:

Test:

#!/usr/bin/env python
#coding=utf-8
import os
from PIL import Image, ImageDraw
import cv

def detect_object(image):
    grayscale = cv.CreateImage((image.width, image.height), 8, 1)
    cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)

    cascade = cv.Load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml")
    rect = cv.HaarDetectObjects(grayscale, cascade, cv.CreateMemStorage(), 1.1, 2,
        cv.CV_HAAR_DO_CANNY_PRUNING, (20,20))

    result = []
    for r in rect:
        result.append((r[0][0], r[0][1], r[0][0]+r[0][2], r[0][1]+r[0][3]))

    return result

def process(infile):
    image = cv.LoadImage(infile);
    if image:
        faces = detect_object(image)

    im = Image.open(infile)
    path = os.path.abspath(infile)
    save_path = os.path.splitext(path)[0]+"_face"
    try:
        os.mkdir(save_path)
    except:
        pass
    if faces:
        draw = ImageDraw.Draw(im)
        count = 0
        for f in faces:
            count += 1
            draw.rectangle(f, outline=(255, 0, 0))
            a = im.crop(f)
            file_name = os.path.join(save_path,str(count)+".jpg")
     #       print file_name
            a.save(file_name)

        drow_save_path = os.path.join(save_path,"out.jpg")
        im.save(drow_save_path, "JPEG", quality=80)
    else:
        print "Error: cannot detect faces on %s" % infile

if __name__ == "__main__":
    process("./opencv_in.jpg")

 

Save the above code to a file named opencv_test.py.  After we run it, it will create a directory named opencv_in_face, and save the result pictures in that directory.

 

Share!
Tweet

admin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors