0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【機械学習】OpenCVを使った物体検出で顔を検出してみた

Last updated at Posted at 2020-02-01

OpenCVについて

OpenCVはインテルによって公開されたコンピュータビジョン向けのライブラリ。描画や画像の編集などの際によく使われるが、機能が非常に多く機械学習向けの機能も提供されている。以下で公開されているhaarcascadesファイル(カスケードファイル)を利用することで、特定の物体に限り、物体検出タスクを行うことができる。ディープラーニング程の検出精度は出ないものの、学習を行う必要がなく、GPU環境も不要なことから、手軽に物体検出を行うことができると言ったメリットがある。

haarcascades(カスケードファイル)について

カスケードファイルは以下githubで公開されている。

ファイル 検出対象
haarcascade_eye.xml
haarcascade_eye_tree_eyeglasses.xml メガネ
haarcascade_frontalcatface.xml 猫顔(正面)
haarcascade_frontalcatface_extended.xml 猫顔(正面)
haarcascade_frontalface_alt.xml 人顔(正面)
haarcascade_frontalface_alt2.xml 人顔(正面)
haarcascade_frontalface_alt_tree.xml 人顔(正面)
haarcascade_frontalface_default.xml 人顔(正面)
haarcascade_profileface.xml 人顔(プロフィール)
haarcascade_fullbody.xml 体全体
haarcascade_smile.xml 笑顔
haarcascade_upperbody.xml 上半身
haarcascade_lowerbody.xml 下半身
haarcascade_righteye_2splits.xml 右目
haarcascade_lefteye_2splits.xml 左目
haarcascade_russian_plate_number.xml ロシアのナンバープレート
haarcascade_licence_plate_rus_16stages.xml ロシアのナンバープレート

これらをopencvから呼び出すことで簡単に物体検出を行うことが可能

環境

  • Ubuntu16.04.3 LTS
  • Python:3.6

対象

どのような顔が認識可能か調べるため、以下のファイルで実行することとした。

対象画像 説明
001.png 複数人。乃木坂46の画像で検証
002.jpg 変顔。トランプ大統領の画像で検証
004.jpg 化粧。聖飢魔Ⅱの画像で検証
003.jpg マスク。ザ・グレートサスケの画像で検証

実装

ソースコードは以下。

import cv2
import glob

# パラメーター設定
ORGDIR = "./sample/"
RSTDIR = "./result/"

CS="../haarcascades/haarcascade_frontalface_default.xml"
SF=1.1   #各画像スケールにおける縮小量
MN=1     #信頼度に関するパラメーター


def detector ():

    #cascadeファイル読み込み
    cascade  = cv2.CascadeClassifier(CS)

    #対象ファイル読み込み
    tgtpaths=glob.glob(ORGDIR+"*")

    for tgtpath in tgtpaths:
        orgimg = cv2.imread(tgtpath, cv2.IMREAD_COLOR)
        grayimg = cv2.cvtColor(orgimg, cv2.COLOR_BGR2GRAY)

        boxes = cascade.detectMultiScale(grayimg,scaleFactor=SF,minNeighbors=MN)

        #矩形の描画
        for box in boxes:
            cv2.rectangle(orgimg,tuple(box[0:2]),tuple(box[0:2]+box[2:4]), (0, 0,255),thickness=1)

        rstpath=tgtpath.replace(ORGDIR,RSTDIR)
        print(rstpath)
        cv2.imwrite(rstpath, orgimg)


if __name__=="__main__":
    detector()

結果

画像 説明
001.png 誤検出があるものの全員の顔を検出
002.jpg 顔の傾きや、崩れがあるものの検出
004.jpg 一名除き検出に成功
003.jpg 検出できず。マスクをすることで顔の特徴を捉えられなかったものと推測。

まとめ

opencvを使用することで、簡単に顔の検出を行うことができた。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?