アニメ画像の顔検出+顔抽出
アニメ画像の顔検出と顔抽出をしていきます。
用意するもの
今回使用するカスケードは、このサイトで紹介されているものです。
GitHub - nagadomi/lbpcascade_animeface: A Face detector for anime/manga using OpenCV
...
プログラム
以前、公開した人の顔検出と同じです。

Python+OpenCV|顔検出と顔画像を抽出する方法
...
カスケードをアニメ画像用に変えただけです。
#検出・抽出したい画像をimread内に記述
origin_img = cv2.imread(“画像ファイル”)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import urllib.request as req import cv2 import matplotlib.pyplot as plt #検出・抽出したい画像をimread内に記述 origin_img = cv2.imread( "train/Jeanne/IMG_0419.JPG" ) img = origin_img.copy() plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.show() # カスケードファイルを指定して、検出器を作成 cascade_file = "lbpcascade_animeface.xml" cascade = cv2.CascadeClassifier(cascade_file) img_face = img # 画像の読み込んでグレースケール img_gray = cv2.cvtColor(img_face, cv2.COLOR_RGB2GRAY) # 顔認識 face_list = cascade.detectMultiScale(img_gray, minSize = ( 150 , 150 )) # 結果 if len (face_list) = = 0 : print ( "失敗" ) quit() for (x, y, w, h) in face_list: print ( "顔の座標 = " , x, y, w, h) red = ( 0 , 0 , 255 ) cv2.rectangle(img_face, (x, y), (x + w, y + h), red, thickness = 20 ) # 出力 plt.imshow(cv2.cvtColor(img_face, cv2.COLOR_BGR2RGB)) plt.show() cv2.imwrite( "out.jpg" , img) #切り取り img_face_ex = origin_img[y:y + h, x:x + w] plt.imshow(cv2.cvtColor(img_face_ex, cv2.COLOR_BGR2RGB)) plt.show() #保存 cv2.imwrite( "out_face.jpg" , img_face_ex) |
実行結果
実行結果では、入力画像と出力画像を示す。