LoginSignup
22
22

More than 5 years have passed since last update.

顔認識と言えば,OpenCVですねっ!!

USBカメラのドライバを組み込む

 昨日の記事を参考に,LinuxのカーネルにUSBカメラのドライバを組み込んでください.
カーネルにUSBカメラのドライバを組み込む

 数時間差で,にゅーらるさんが手順を一から詳しく説明した記事を書いてくれているので,もっと詳しく知りたい人は、そっちもチェック!

OpenCVのインストール

 OpenCVは,opkgにパッケージがあります.

# opkg install opencv python-opencv

コードを書く

detectface.py
import cv2
import sys

cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)

video_capture = cv2.VideoCapture(0)

# Capture frame-by-frame
ret, frame = video_capture.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Save the resulting frame
cv2.imwrite('face.png', frame)


# When everything is done, release the capture
video_capture.release()

 スパっとこちらのコードに手を加えました.
 ありがとうございます.

結果

 このOpenCVは,Haar Cascadesという顔のかたちの明暗ライブラリを使用するので,どっかOpenCVをインストールしたコンピュータから持って来てください(適当).Ubuntuの場合は, /usr/share/opencv/haacascades/haacascade_frontalface_alt.xml にあります.これを使って,

# python ./detectface.py ./haacascade_frontalface_alt.xml

とすると,同一ディレクトリ内にface.pngという認識結果のファイルが生成されます.

Edisonで顔認識

22
22
0

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
22
22