LoginSignup
339

機械学習のライブラリ dlib

Last updated at Posted at 2016-04-09

機械学習のC++ Pythonのライブラリの1つdlibに気づいた。
PythonにはScikit-learnという強力なライブラリがあるが、
選択肢の1つとして考えておこう。

機械学習のライブラリ dlibのアルゴリズムの選択ガイド

機械学習のライブラリ dlibのアルゴリズムの選択ガイドが
図にしてありました。

Machine Learning Guide

こちらはscikit-learnのガイド
![Choosing the right estimator]
(http://scikit-learn.org/stable/_static/ml_map.png)

dlibの記事

C++

バージョン19.0で新しく入ったDeep Learning APIについて公式ブログで説明されている

Python

C#

SlideShare 20160417dlibによる顔器官検出

顔器官検出は、目・鼻・口などの位置を得ることで、表情や、まばたき、口の動きなどを知ることに使われます。人物の顔認証を行う際には必須の機能として知られています。オープンソースのライブラリでここまで充実している時代になったのです。

Linux では以下のサイトの手順でインストールできます。
https://github.com/davisking/dlib

しかし、Windowsでも使いたい人がいるかと思うので、以下の手順を記します。

インストールから顔器官検出するまで(Pythonの場合)

PIP によるインストール

dlib 19.24.0 リリース日: 2022年5月8日

pip install dlib

環境:
Windows7

手順:

  1. dlibのpython バインディングをインストールする。
    コマンドプロンプト >pip install dlib
    というやり方でWindowsでもインストールできました。

Python のプロンプト>>> import dlib
でエラーを生じないようなったら、インストールは成功です。

2. 標準のサンプルプログラムなどをgithubから[DownloadZip]でダウンロード
https://github.com/davisking/dlib
 ダウンロードしたzipファイルを展開

3.追加のデータのダウンロード
face_landmark_detection.pyを動作させる場合には
http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2
をダウンロードして(圧縮済みのデータファイルしても61.1MBあります。)
7-zipなどで展開します。
展開後のファイルをpython_exampleのディレクトリに移動します。

4.python_exampleのスクリプトの実行
コマンドプロンプト> cd python_example
コマンドプロンプト> python face_detector.py 顔のある画像ファイル名

コマンドプロンプト> python face_landmark_detection.py shape_predictor_68_face_landmarks.dat ..\examples\faces

shape_predictor_68_face_landmarks.dat
のファイルサイズはとても大きいのでコマンド実行の直後しばらく待つことになります。

5. 他の画像で性能を見てみたい場合、
 ..\examples\faces
 にjpgファイルを置くのが一番簡単なテスト方法です。

face_landmark_detection.py
https://github.com/mit-nlp/MITIE/blob/master/dlib/python_examples/face_landmark_detection.py
を見ると、各画像で見つかった顔ごとにする処理(高性能な顔器官検出の結果の表示)が
プログラムのわかりやすさをそこなっていないのがわかります。

その他のpython_examplesの例題を触れてみよう。

まず、次のようにコマンドをうちこむと、大概の場合、正しい使い方を表示してくれる。

$ python face_alignment.py 
Call this program like this:
   ./face_alignment.py shape_predictor_5_face_landmarks.dat ../examples/faces/bald_guys.jpg
You can download a trained facial shape predictor from:
    http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2

$

データファイルを入手しなさいとあるので、wgetで入手する。その後解凍する。

$ wget http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2
--2017-11-08 00:19:30--  http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2
Resolving dlib.net (dlib.net)... 107.180.26.78
Connecting to dlib.net (dlib.net)|107.180.26.78|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5706710 (5.4M)
Saving to: ‘shape_predictor_5_face_landmarks.dat.bz2’

shape_predictor_5_f 100%[===================>]   5.44M   164KB/s    in 38s     

2017-11-08 00:20:09 (145 KB/s) - ‘shape_predictor_5_face_landmarks.dat.bz2’ saved [5706710/5706710]
$ 

その後、例題の引数でpythonスクリプトを動作させるとよい。

./face_alignment.py shape_predictor_5_face_landmarks.dat ../examples/faces/bald_guys.jpg

その後、入力画像を差し替えるとよい。

このようにすることで、python_exampleを気軽に試してみることができます。
https://github.com/davisking/dlib/blob/master/python_examples/face_alignment.py

以下は、そのようなスクリプトのコマンドラインでの使い方の表示の部分です。

$ ./cnn_face_detector.py 
Call this program like this:
   ./cnn_face_detector.py mmod_human_face_detector.dat ../examples/faces/*.jpg
You can get the mmod_human_face_detector.dat file from:
    http://dlib.net/files/mmod_human_face_detector.dat.bz2
$ ./face_alignment.py 
Call this program like this:
   ./face_alignment.py shape_predictor_5_face_landmarks.dat ../examples/faces/bald_guys.jpg
You can download a trained facial shape predictor from:
    http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2

$ ./face_clustering.py 
Call this program like this:
   ./face_clustering.py shape_predictor_5_face_landmarks.dat dlib_face_recognition_resnet_model_v1.dat ../examples/faces output_folder
You can download a trained facial shape predictor and recognition model from:
    http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2
    http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2

$ ./face_landmark_detection.py 
Give the path to the trained shape predictor model as the first argument and then the directory containing the facial images.
For example, if you are in the python_examples folder then execute this program by running:
    ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat ../examples/faces
You can download a trained facial shape predictor from:
    http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

face_alignment.py は
顔を検出して
顔器官点をだして
画像の位置正規化して、windowに表示する。リターンキーを出すと次の顔画像を表示する。

$ ./face_alignment.py shape_predictor_5_face_landmarks.dat ../examples/faces/bald_guys.jpg

face_clustering.py は
入力画像フォルダにある画像の中で顔を検出してそれぞれ特徴量をだして、
その特徴量に基づいてクラスタリングして
一番大きなクラスタに入った画像を出力フォルダに保存する。

$ ./face_clustering.py shape_predictor_5_face_landmarks.dat dlib_face_recognition_resnet_model_v1.dat ../examples/faces output_folder
Processing file: ../examples/faces/bald_guys.jpg
Number of faces detected: 24
Processing file: ../examples/faces/2009_004587.jpg
Number of faces detected: 2
Processing file: ../examples/faces/2008_002506.jpg
Number of faces detected: 3
Processing file: ../examples/faces/2008_002079.jpg
Number of faces detected: 6
Processing file: ../examples/faces/2008_004176.jpg
Number of faces detected: 7
Processing file: ../examples/faces/dogs.jpg
Number of faces detected: 0
Processing file: ../examples/faces/2008_007676.jpg
Number of faces detected: 7
Processing file: ../examples/faces/2008_002470.jpg
Number of faces detected: 6
Processing file: ../examples/faces/2008_001009.jpg
Number of faces detected: 2
Processing file: ../examples/faces/2007_007763.jpg
Number of faces detected: 7
Processing file: ../examples/faces/2008_001322.jpg
Number of faces detected: 3
Number of clusters: 45
Biggest cluster id number: 0
Number of faces in biggest cluster: 6
Indices of images in the biggest cluster: [0, 1, 5, 11, 21, 22]
Saving faces in largest cluster to output folder...
$

###dlibのpython API
dlib documentation
http://dlib.net/python/index.html
http://dlib.net/
dlibのドキュメンテーションはどんどん整備されてきているようだ。

顔検出器の使い方は次の2つがあります。

顔検出器の使い方の例1

detector = dlib.get_frontal_face_detector()
dets = detector(img, 1)

顔検出器の使い方の例2

detector = dlib.get_frontal_face_detector()
# RGB変換 (opencv形式からskimage形式に変換)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# frontal_face_detectorクラスは矩形, スコア, サブ検出器の結果を返す
dets, scores, idx = detector.run(img_rgb, 0)
for det in dets:
    cv2.rectangle(img, (det.left(), det.top()), (det.right(), det.bottom()), (0, 0, 255))

このdetsは
cv::Rect()とも、cv2の場合の(x,y,w,h)
と違うことに注意することです。

dlib.rectangle(left, top, right, bottom))

のようにして、このデータ型に変換するようです。

dlibでの顔検出の最小の検出サイズについて

以下の記述によれば,detector.run(img_rgb, numUpSampling)
のnumUpSampling=0での最小の検出サイズは80x80 pixelのようです。
http://dlib.net/face_detection_ex.cpp.html

40x40 pixelで検出したいときは numUpSampling を 1に
20x20 pixelで検出したいときは numUpSampling を 2に
する必要があるようです。
numUpSampling を1ふやすたびに、画像の面積が4倍になり、処理時間も4倍になります。

dlib自体のface_recognition.pyについて

https://github.com/davisking/dlib/blob/master/python_examples/face_recognition.py
では顔照合のための特徴量を計算をしているようですが、
顔照合自体の計算がみあたりません。

それが理由で、顔照合を行うライブラリが以下のサイトで別途開発されています。
スクリプトファイル名が同じだったりするので、注意してください。
https://github.com/ageitgey/face_recognition

ドキュメンテーションブラウザからAPIを知る方法

dlibのPython実装について、日本語での解説ブログがなくても、ひるむ必要はありません。
スタートメニュー[Python(x,y)][Documentation][Python documentation server]を起動します。
[open browser]
を実行すると、
ブラウザ上で
(Pythonのインストール先)\site-packages
の中からdlibを見つけていけば、Pythonインタフェースに用意されている機能をすべて見ることができます。

Python(x,y) ではなく、Linuxを使っている場合には
$ /usr/lib/python2.7/pydoc.py -p ポート番号 &

としてサーバーを起動し、
それをブラウザで開くことができます。

###オブジェクト検出器を作るには
dlibにはオブジェクトの検出器を作るためのツールが用意されています(注)。
http://dlib.net/train_object_detector.py.html
にある記述をご覧ください。
./train_object_detector.py ../examples/faces

../examples/faces のディレクトリにはtraining.xml という名前のファイルが
次のようなファイル形式で置かれておることを想定しています。
このxmlファイルをブラウザで表示させると、画像のどの位置に顔枠が表示されます。
https://github.com/davisking/dlib/blob/master/examples/faces/training.xml

画像も../examples/facesのディレクトリに置かれています。

dlib.train_simple_object_detector(training_xml_path, "detector.svm", options)

のようにして、detector.svm ファイルとして学習結果のファイルができあがります。

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

のようにして、検出された長方形のリストを得ることができます。

その一連の学習作業については、次の記事が解説してくれている。

dlibの物体検出は内部でサンプリングもしてくれるので、正データとなる画像と矩形だけ集めれば良いし、精度も良いので結構良い。
パラメータも少なく物体検出できる方だと思う。
メモリとCPUはバカ食いするけど愛嬌がある。

さらには、実際に自分の画像データでオブジェクト検出器を作ってみたいだろう。
その手順が次の記事に舁いてあります。


なお、学習済みのモデルファイルは以下の場所にあります。

[dlib-models]
(https://github.com/davisking/dlib-models#dlib-models)

mmod_front_and_rear_end_vehicle_detector.dat.bz2
mmod_rear_end_vehicle_detector.dat.bz2
mmod_dog_hipsterizer.dat.bz2

HOGとSVMによる物体検出とその学習についての記事

顔器官点の学習

Dlib自体の顔器官学習がどのようにされているのかはスクリプト自体のコメントに書かれています。
http://dlib.net/face_landmark_detection.py.html

#   The face detector we use is made using the classic Histogram of Oriented
#   Gradients (HOG) feature combined with a linear classifier, an image pyramid,
#   and sliding window detection scheme.  The pose estimator was created by
#   using dlib's implementation of the paper:
#      One Millisecond Face Alignment with an Ensemble of Regression Trees by
#      Vahid Kazemi and Josephine Sullivan, CVPR 2014
#   and was trained on the iBUG 300-W face landmark dataset (see
#   https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/):  
#      C. Sagonas, E. Antonakos, G, Tzimiropoulos, S. Zafeiriou, M. Pantic. 
#      300 faces In-the-wild challenge: Database and results. 
#      Image and Vision Computing (IMAVIS), Special Issue on Facial Landmark Localisation "In-The-Wild". 2016.
#   You can get the trained model file from:
#   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2.
#   Note that the license for the iBUG 300-W dataset excludes commercial use.
#   So you should contact Imperial College London to find out if it's OK for
#   you to use this model file in a commercial product.

dlibの顔検出への課題

  • 横顔が検出されない
    • 横顔検出器はどこにある? 誰か作っていないものか?

dlibにもDNN

dlibベースの機能で他のライブラリと置き換え可能なもの

以下のスクリプトからの抜粋です。
https://github.com/davisking/dlib/blob/master/python_examples/face_recognition.py

次のインスタンス winとそのメソッドは, cv2.imshow(), cv2.rectangle()などで置き換え可能です。
dlibは、OpenCVを使わなくて済む部分は使わなくて済まそうという部分あります。

    win = dlib.image_window()
    img = io.imread(f)
    win.clear_overlay()
    win.set_image(img)
    dets = detector(img, 1)
    for k, d in enumerate(dets):
        shape = sp(img, d)
        win.clear_overlay()
        win.add_overlay(d)
    win.add_overlay(shape)
    dlib.hit_enter_to_continue()

dlib自体にも追跡用のライブラリがある

dlibベースの顔照合が公開されている

face_recognition 1.0.0

face_recognition 1.2.3
face_recognition 1.3.0

紹介記事dlib ベースの顔照合プログラム face_recognition

dlibのPythonバインディングの実装のしかた

以下のソースをみれば明らかなようにboost::pythonが用いられている。
https://github.com/davisking/dlib/blob/master/tools/python/src/object_detection.cpp

#include <boost/python/args.hpp>
using namespace boost::python;

顔関係でdlibを無視できない理由


dlibのblog サイト がある。
ここでは、実装されている機能について、解説が書かれている。

付記:

ここに書いていたOpenCV自体の顔照合のメモは独立した項目に変更しました。

「OpenCV 3.3.0 Face Recognition with OpenCV」への補足記事

顔向き推定について調査中
顔照合のオープンソースの実装について調査中

Dlib README.md の和訳

Ubuntu で Dlib 19.15 のビルドとインストール

追記 2020.02

この追記を書いている時点では、顔検出・顔照合についての実用的な選択肢が増えました。
この記事を最初に書いた時点では、OpenCV自体の顔照合関連の機能が、オープンソースのトップレベルと言い切るには不足している部分がありました。顔のランドマークを出し、顔照合を行う部分のOpenCV 自体のライブラリが深層学習の成果を反映しきったものではなかったためです。
しかし、2020年の時点では、OpenVinoの顔ライブラリが充実していること、OpenCVの枠組みでOpenVinoのModelOptimizerで変換済みの各種Model ZOOのモデルを利用できるようになっています。
そのことから、ある時期では、dlib一択に近い状況から、OpenVinoが無視できない状況になりつつあります。
今後どのようになっていくのか、動向をその都度みていってください。
東洋系の人の顔照合能力が、どの程度高まっていくのかは、それぞれのモデルの中で東洋系の人の顔を学習にどれくらい使っているのかに依存します。
オープン・ソースの実装の性能を高めておくことは、商用ライブラリの開発会社が、過度に社会的な影響を持つことに対する対抗策となりえます。

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
339