・ここでは、べゼリーに内蔵したカメラで人間の顔を認識させてみます。
・顔の認識にはオープンソースの画像処理ライブラリ「Open CV(Open Source Computer Vision Library)」を使います。
準備
・「カメラを使ってみる」をクリアした状態(ラズパイカメラを接続し、HDMIディスプレイが接続された状態)にしておいてください。
サンプルプログラム sample_face1.py
・パソコンからの遠隔操作だと動画が表示されないことがあるので、ラズパイに直接つなげたキーボードとディスプレイで実行してください。(回避手段はありますが、ここでは説明しません)
$ cd ~/bezelie/edgar $ python sample_face1.py
・ディスプレイに表示されているカメラ画像の中に顔が収まると、赤い四角が表示され、べゼリーが喋るはずです。顔が画面からはみ出ていたり、傾いていたり、遠すぎると反応しません。
・終了したいときはキーボードの「q」ボタンを押してください。
ソースコード
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Bezelie Sample Code : Face Recognition Test
import picamera
import picamera.array
import cv2
import bezelie
cascade_path = "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"
cascade = cv2.CascadeClassifier(cascade_path)
# Get Started
bez = bezelie.Control() # べゼリー操作インスタンスの生成
bez.moveCenter() # サーボをセンタリング
# Main Loop
def main():
with picamera.PiCamera() as camera: # Open Pi-Camera as camera
with picamera.array.PiRGBArray(camera) as stream: # Open Video Stream from Pi-Camera as stream
camera.resolution = (600, 400) # Display Resolution
camera.hflip = True # Vertical Flip
camera.vflip = True # Horizontal Flip
while True:
camera.capture(stream, 'bgr', use_video_port=True) # Capture the Video Stream
gray = cv2.cvtColor(stream.array, cv2.COLOR_BGR2GRAY) # Convert BGR to Grayscale
facerect = cascade.detectMultiScale(gray, # Find face from gray
scaleFactor=1.9, # 1.1 - 1.9 :the bigger the quicker & less acurate
minNeighbors=1, # 3 - 6 : the smaller the more easy to detect
minSize=(60,100), # Minimam face size
maxSize=(400,400)) # Maximam face size
if len(facerect) > 0:
bez.moveHead (20)
for rect in facerect:
cv2.rectangle(stream.array, # Draw a red rectangle at face place
tuple(rect[0:2]), # Upper Left
tuple(rect[0:2]+rect[2:4]), # Lower Right
(0,0,255), thickness=2) # Color and thickness
cv2.imshow('frame', stream.array) # Display the stream
bez.moveHead (0)
if cv2.waitKey(1) & 0xFF == ord('q'): # Quit operation
break
stream.seek(0) # Reset the stream
stream.truncate()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
解説
・9~10行目:顔認識のための分類器を読み込んでいます。
・25行目:カメラの画像をRGBでキャプチャーしています。
・26行目:RGB動画をグレースケールに変換しています。
・27~31行目:顔を認識しています。
・33~39行目:顔が見つかった場合の処理です。
応用
・28~31行目が顔を認識するためのパラメータです。いろいろ数値をいじってみましょう。例えばminSizeは顔として認識する最低のサイズですので、これを小さくすると、遠くの顔も認識されるようになります。そのかわり、壁に貼ってある写真など、顔じゃないものを誤認識する確率が上がってしまうかもしれません。
Tips
・OpenCVのCVはComputer Visionの略です。
・べゼリー音声対話キットに付属しているmicroSDカードには予めOpen CVがインストールされていますが、自分でインストールする場合はコマンドラインから以下のコマンドを打ち込んでください。
$ sudo apt-get install libopencv-dev $ sudo apt-get install python-opencv
・途中で何か聞かれたら「y」と答えてください。