TadaoYamaokaの開発日記

個人開発しているスマホアプリや将棋AIの開発ネタを中心に書いていきます。

dlibの顔のパーツ検出をマンガで試してみた(追試)

昨日の日記でdibでマンガの顔器官検出を試したころ、全く検出できなかった。

HOG特徴+SVMであらかじめ顔検出をして切り出した画像を対象とした場合どうなるか追加で検証を行った。

画像の切り出しは、顔検出スクリプト(detect_object_detector.py)を修正して、検出した領域を別ファイルに保存するようにした。

crop_object_detector.py
#!/usr/bin/python
import os
import sys
import glob

import dlib
from skimage import io


if len(sys.argv) != 3:
    print(
        "Give the path to the examples/faces directory as the argument to this "
        "program. For example, if you are in the python_examples folder then "
        "execute this program by running:\n"
        "    ./crop_object_detector.py ../examples/faces outdir")
    exit()
faces_folder = sys.argv[1]
out_folder = sys.argv[2]


detector = dlib.simple_object_detector("detector.svm")

print("Showing detections on the images in the faces folder...")
for f in glob.glob(os.path.join(faces_folder, "*.jpg")):
    print("Processing file: {}".format(f))
    img = io.imread(f)
    dets = detector(img)
    print("Number of faces detected: {}".format(len(dets)))
    num = 0
    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))
        cropped = img[d.top():d.bottom(), d.left():d.right()]
        outpath = os.path.join(out_folder, os.path.basename(f).replace(".jpg", "_"+str(num).zfill(2)+".jpg"))
        io.imsave(outpath, cropped)
        num += 1

このスクリプトを使用し以下のコマンドで顔画像の切り出しを行う。

./crop_object_detector.py targets cropped

croppedディレクトリに切り出した画像が保存される。

検出

顔検出した画像を切り出して保存した画像を対象に、顔器官検出を行った。

./predict_shape_predictor.py cropped

結果

前回と結果は変わらず、切り出した画像に対しても全く検出できなかった。

dlibの顔器官検出が形を見つけ出すロジックは、マンガに対して有効ではないようだ。

論文の内容とdlibの実装を理解すれば、もう少し理由が説明できそうだが、宿題としておこう。