hsuetsugu’s diary

ITの技術的なことに関して主に書きます。Rとpythonとd3.jsとAWSとRaspberryPiあたりを不自由なく使いこなせるようになりたいです。

(35連休)4日目:Raspberry Pi(OpenCVのインストール〜リアルタイム顔認識)

Raspberry Piでのロボット工作として、OpenCVの活用、Siriの活用を考えています。今回はまずOpenCV自体これまで使っていなかったので、インストールから簡単な画像処理までを実施しました。
一旦、顔認識するところまでは確認できたので、一旦OpenCVから離れてまた複合的に使っていきたいです。

OpenCVとは

引用:OpenCV - Wikipedia

OpenCV(オープンシーヴィ、英語: Open Source Computer Vision Library)とはインテルが開発・公開したオープンソースのコンピュータビジョン向けライブラリ[1]。 現在はWillow Garage(ウィロー・ガレージ)が開発を行っている

日本語のサイトはこちら。→OpenCV.jp

最近はscilit-imageなど、新しいものも出てきているみたいですが、OpenCVすら使っていないので、違いも何もわかりません。
scikit-image: Image processing in Python — scikit-image

インストール

OpneCV本体と、pythonから使うためのライブラリをインストール

sudo apt-get install libopencv-dev
sudo apt-get install python-opencv

参考URL:はんだ日和: Raspberry Pi にOpenCV をインストール

カメラ接続

実家に使っていないWEBカメラがあったので、それをもらってきました。USBで接続して、lsusbとコマンドをうつと、接続されている機器の情報がみれます。"Logitech, Inc. QuickCam IM/Connect"とでてきました。

root@raspberrypi:~# lsusb
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 1a40:0101 Terminus Technology Inc. 4-Port HUB
Bus 001 Device 005: ID 046d:08d9 Logitech, Inc. QuickCam IM/Connect

映像をみるためのviewerのインストール

sudo apt-get install luvcview 

カメラの映像をみる

pythonスクリプトの準備

camera.py

#!/usr/bin/python

import cv2.cv as cv
import time

cv.NamedWindow("camera", 1)

capture = cv.CaptureFromCAM(0)

# 画像サイズの指定
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,320)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,240)

while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    if cv.WaitKey(10) > 0:
        break
cv.DestroyAllWindows()

画面共有

VNCサーバを起動(tightvncserver)して、MACのファインダーからcommand+Kでvnc://192.168.11.8:5901

camera.pyを実行

home/pi/open-cv-testにcamera.pyを配置して下記コマンドをうつと、映像がでてきました。

cd open-cv-test
python camera.py

f:id:hsuetsugu:20140813121951p:plain

顔認識

この辺をみてそのまま実行しただけですが・・・。
TechnoLabsz: How to easily install OpenCV on Raspberry Pi

OpenCVにはいっているfacedetect.pyを実行。カメラを自分に向けたり、子供の写真に向けて、顔認識されました。
f:id:hsuetsugu:20140813131155p:plain

python facedetect.py --cascade=face.xml 0

facedetect.py

#!/usr/bin/python
"""
This program is demonstration for face and object detection using haar-like features.
The program finds faces in a camera image or video stream and displays a red box around them.

Original C implementation by:  ?
Python implementation by: Roman Stanchak, James Bowman
"""
import sys
import cv2.cv as cv
from optparse import OptionParser

# Parameters for haar detection
# From the API:
# The default parameters (scale_factor=2, min_neighbors=3, flags=0) are tuned 
# for accurate yet slow object detection. For a faster operation on real video 
# images the settings are: 
# scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING, 
# min_size=<minimum possible face size

min_size = (20, 20)
image_scale = 2
haar_scale = 1.2
min_neighbors = 2
haar_flags = 0

def detect_and_draw(img, cascade):
    # allocate temporary images
    gray = cv.CreateImage((img.width,img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(img.width / image_scale),
			       cv.Round (img.height / image_scale)), 8, 1)

    # convert color input image to grayscale
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    cv.EqualizeHist(small_img, small_img)

    if(cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
                                     haar_scale, min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.))
        if faces:
            for ((x, y, w, h), n) in faces:
                # the input to cv.HaarDetectObjects was resized, so scale the 
                # bounding box of each face and convert it to two CvPoints
                pt1 = (int(x * image_scale), int(y * image_scale))
                pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
                cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)

    cv.ShowImage("result", img)

if __name__ == '__main__':

    parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]")
    parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="Haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml")
    (options, args) = parser.parse_args()

    cascade = cv.Load(options.cascade)
    
    if len(args) != 1:
        parser.print_help()
        sys.exit(1)

    input_name = args[0]
    if input_name.isdigit():
        capture = cv.CreateCameraCapture(int(input_name))
    else:
        capture = None

    cv.NamedWindow("result", 1)

    width = 320 #leave None for auto-detection
    height = 240 #leave None for auto-detection

    if width is None:
    	width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
    else:
    	cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width)    

    if height is None:
	height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
    else:
	cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height) 

    if capture:
        frame_copy = None
        while True:

            frame = cv.QueryFrame(capture)
            if not frame:
                cv.WaitKey(0)
                break
            if not frame_copy:
                frame_copy = cv.CreateImage((frame.width,frame.height),
                                            cv.IPL_DEPTH_8U, frame.nChannels)

#                frame_copy = cv.CreateImage((frame.width,frame.height),
#                                            cv.IPL_DEPTH_8U, frame.nChannels)

            if frame.origin == cv.IPL_ORIGIN_TL:
                cv.Copy(frame, frame_copy)
            else:
                cv.Flip(frame, frame_copy, 0)
            
            detect_and_draw(frame_copy, cascade)

            if cv.WaitKey(10) >= 0:
                break
    else:
        image = cv.LoadImage(input_name, 1)
        detect_and_draw(image, cascade)
        cv.WaitKey(0)

    cv.DestroyWindow("result")

MJPG-streamer(ストリーミング配信用のアプリ)をインストール

※ここからエラーがでてうまくいかず・・・。

$ sudo apt-get install subversion
$ sudo apt-get install libjpeg-dev imagemagick
$ svn co https://svn.code.sf.net/p/mjpg-streamer/code/ mjpg-streamer
$ cd mjpg-streamer/mjpg-streamer
$ make
$ ./mjpg_streamer -i "./input_uvc.so -f 20 -r 320x240 -d /dev/video0 -y" -o "./output_http.so -w ./www -p 8080"