LoginSignup
33

More than 5 years have passed since last update.

Raspberry Pi で OpenCV(リベンジ)

Last updated at Posted at 2016-05-25

前回、OpenCV をインストール後、ブートできなくなってしまったので、Raspbian を再セットアップして、OpenCV のインストールに再チャレンジ

環境

  • Raspberry Pi 2 Model B
  • SDカード 16GB(Class10)
$ cat /etc/debian_version
8.0
$ cat /etc/issue
Raspbian GNU/Linux 8 \n \l

python はインストールされている

$ python
Python 2.7.9 (default, Mar  8 2015, 00:52:26)
[GCC 4.9.2] on linux2

OpenCV のインストール

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

動作確認

webカメラを接続

$ lsusb
Bus 001 Device 006: ID 046d:09a2 Logitech, Inc. QuickCam Communicate Deluxe/S7500

以下の記事のコードを使用して動作確認
Raspberry Piで画像処理ライブラリ”OpenCV”使って”顔認識”試してみた

camera.py
# coding:utf-8

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()

実行

$ python camera.py

webカメラの映像がリアルタイムに表示されるようになった

顔認識

上記の記事と同様に、顔認識を体験してみる

以下のサイトの手順8のファイルをダウンロードして解凍(unzipコマンド)

解凍した2つのファイル

  • facedetect.py
  • face.xml

を Raspberry Pi 上の適当なディレクトリに保存

実行

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

webカメラの映像が表示され、顔を認識すると、顔を赤線の四角で囲むことが確認できた

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33