OpenCVを用いてカンタン顔認識(Python)

ライブラリ「OpenCV」を使って、顔認識をやってみました。

import cv2

# 入力画像の読み込み
image_file = "cross.jpg"
image_path = "./inputs/" + image_file
output_path = "./outputs/" + image_file

img = cv2.imread(image_path)

# カスケード型識別器の読み込み
cascade = cv2.CascadeClassifier(
    "~~\lbpcascade_animeface.xml")

# グレースケール変換
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# アニメ顔領域の探索
face = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3, minSize=(30, 30))

# 顔領域を赤色の矩形で囲む
for (x, y, w, h) in face:
    cv2.rectangle(img, (x, y), (x + w, y+h), (0, 0, 200), 3)

# 結果を出力
cv2.imwrite(output_path, img)

今回はイラストから顔を識別することから、https://github.com/nagadomi/lbpcascade_animefaceにて公開されているlbpcascade_animeface.xmlを用いて検出器を作成しました。

 以下実行結果

顔検出1
顔検出2

いい感じ!

参考にさせて頂いたサイト:https://qiita.com/mczkzk/items/fda37ac4f9ddab2d7f45


コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です