LoginSignup
2
3

dlib に 私の顔認証モデルが採用された

Posted at

dlib の顔認証モデルに私の Taguchi model が採用されました。(2024/5/22)
dlib がトレーニングに使用した顔画像は300万枚です。
私がトレーニングに使用した顔画像は650万枚で、そのうち約47% が日本人の顔画像です。
従って、日本人の顔認証に限らず、欧米人の顔認証についても dlib と遜色ない精度があります。
MITライセンスなのでお気軽にご利用ください。

※↑最下段にある'Face recognition new model with Asian (mainly Japanese)' 参照

dlib の概略

Dlibは、C++言語で書かれた汎用目的のクロスプラットフォームソフトウェアライブラリである。契約プログラミングとコンポーネントベースソフトウェア工学の考えに強い影響を受けている。そのため、第一に、独立したソフトウェアコンポーネントの集合という構成になっている。Boost Software Licenseの元に公開されているオープンソースソフトウェアである。

2002年に開発が開始されて以降、Dlibは幅広い種類のツールに組み込まれるようになった。2016年時点では、nネットワーク、スレッド、GUI、データ構造、線形代数、機械学習、画像処理、データマイニング、XMLおよびテキストのパース、数理最適化、ベイジアンネットワークなど、幅広い分野の処理を行うためのソフトウェアコンポーネントが開発されている。近年は、統計的機械学習のための幅広いツールセットの開発に力を入れており、2009年には、DlibはJournal of Machine Learning Researchで論文を発表した[2]。以降、この論文は様々な分野で引用されている。--Wikipediaより

Taguchi model の概略

dlib には優秀な顔認証モデルがあります。しかし、残念ながら人種に偏りがあり、特に北東アジア人、とりわけ日本人に関してはまったく役に立たないと言っても過言ではありません。ですが、それは仕方のないことだと思います。訓練に使用した顔認証用のデータセットに偏りがあったためです。
そこで私は日本人を中心とした多くのデータセットを収集し、ゼロから訓練を行いました。これには途方もない時間を要しましたが、ある程度は実用に耐えうる基準に達したと思います。日本人用に訓練したとは言っても欧米人の顔認証に関しても dlib のモデルに近い結果となってます。dlib の example で用意されているハリウッドのアクション ヒーローの写真も dlib と同様に分類が可能です。

詳細な説明は下記をご覧ください

Python からの利用方法

これまで c++ と c# の使用例をご紹介してきました。

python からの簡単な使用例は dlib の python_examples に載っていましたのでご紹介いたします。
以下のソースコードで'dlib_face_recognition_resnet_model_v1.dat' の部分を 'taguchi_face_recognition_resnet_model_v1.dat' に変更するだけです。
画像は適当なものがなければ以下をご利用ください。
Actor images

#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
#   This example shows how to use dlib's face recognition tool.  This tool maps
#   an image of a human face to a 128 dimensional vector space where images of
#   the same person are near to each other and images from different people are
#   far apart.  Therefore, you can perform face recognition by mapping faces to
#   the 128D space and then checking if their Euclidean distance is small
#   enough. 
#
#   When using a distance threshold of 0.6, the dlib model obtains an accuracy
#   of 99.38% on the standard LFW face recognition benchmark, which is
#   comparable to other state-of-the-art methods for face recognition as of
#   February 2017. This accuracy means that, when presented with a pair of face
#   images, the tool will correctly identify if the pair belongs to the same
#   person or is from different people 99.38% of the time.
#
#   Finally, for an in-depth discussion of how dlib's tool works you should
#   refer to the C++ example program dnn_face_recognition_ex.cpp and the
#   attendant documentation referenced therein.
#
#
#
#
# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
#   You can install dlib using the command:
#       pip install dlib
#
#   Alternatively, if you want to compile dlib yourself then go into the dlib
#   root folder and run:
#       python setup.py install
#
#   Compiling dlib should work on any operating system so long as you have
#   CMake installed.  On Ubuntu, this can be done easily by running the
#   command:
#       sudo apt-get install cmake
#
#   Also note that this example requires Numpy which can be installed
#   via the command:
#       pip install numpy

import sys
import os
import dlib
import glob

if len(sys.argv) != 4:
    print(
        "Call this program like this:\n"
        "   ./face_recognition.py shape_predictor_5_face_landmarks.dat dlib_face_recognition_resnet_model_v1.dat ../examples/faces\n"
        "You can download a trained facial shape predictor and recognition model from:\n"
        "    http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2\n"
        "    http://dlib.net/files/taguchi_face_recognition_resnet_model_v1.dat.bz2")
    exit()

predictor_path = sys.argv[1]
face_rec_model_path = sys.argv[2]
faces_folder_path = sys.argv[3]

# Load all the models we need: a detector to find the faces, a shape predictor
# to find face landmarks so we can precisely localize the face, and finally the
# face recognition model.
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)
facerec = dlib.face_recognition_model_v1(face_rec_model_path)

win = dlib.image_window()

# Now process all the images
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
    print("Processing file: {}".format(f))
    img = dlib.load_rgb_image(f)

    win.clear_overlay()
    win.set_image(img)

    # Ask the detector to find the bounding boxes of each face. The 1 in the
    # second argument indicates that we should upsample the image 1 time. This
    # will make everything bigger and allow us to detect more faces.
    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))

    # Now process each face we found.
    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))
        # Get the landmarks/parts for the face in box d.
        shape = sp(img, d)
        # Draw the face landmarks on the screen so we can see what face is currently being processed.
        win.clear_overlay()
        win.add_overlay(d)
        win.add_overlay(shape)

        # Compute the 128D vector that describes the face in img identified by
        # shape.  In general, if two face descriptor vectors have a Euclidean
        # distance between them less than 0.6 then they are from the same
        # person, otherwise they are from different people. Here we just print
        # the vector to the screen.
        face_descriptor = facerec.compute_face_descriptor(img, shape)
        print(face_descriptor)
        # It should also be noted that you can also call this function like this:
        #  face_descriptor = facerec.compute_face_descriptor(img, shape, 100, 0.25)
        # The version of the call without the 100 gets 99.13% accuracy on LFW
        # while the version with 100 gets 99.38%.  However, the 100 makes the
        # call 100x slower to execute, so choose whatever version you like.  To
        # explain a little, the 3rd argument tells the code how many times to
        # jitter/resample the image.  When you set it to 100 it executes the
        # face descriptor extraction 100 times on slightly modified versions of
        # the face and returns the average result.  You could also pick a more
        # middle value, such as 10, which is only 10x slower but still gets an
        # LFW accuracy of 99.3%.
        # 4th value (0.25) is padding around the face. If padding == 0 then the chip will
        # be closely cropped around the face. Setting larger padding values will result a looser cropping.
        # In particular, a padding of 0.5 would double the width of the cropped area, a value of 1.
        # would triple it, and so forth.

        # There is another overload of compute_face_descriptor that can take
        # as an input an aligned image. 
        #
        # Note that it is important to generate the aligned image as
        # dlib.get_face_chip would do it i.e. the size must be 150x150, 
        # centered and scaled.
        #
        # Here is a sample usage of that

        print("Computing descriptor on aligned image ..")
        
        # Let's generate the aligned image using get_face_chip
        face_chip = dlib.get_face_chip(img, shape)        

        # Now we simply pass this chip (aligned image) to the api
        face_descriptor_from_prealigned_image = facerec.compute_face_descriptor(face_chip)                
        print(face_descriptor_from_prealigned_image)        
        
        dlib.hit_enter_to_continue()



'taguchi_face_recognition_resnet_model_v1.dat' は以下より入手してください。
Taguchi models
※拡張子が'.7z'となっていますので解凍してご利用ください。

最後に

メジャーな dlib の顔認証ライブラリに追加されたことで、気軽に利用することが可能となりました。
いろいろな楽しみ方があるでしょう。

例えば、こんな利用方法が考えられます。
・登録済みの人が映ったらメールを送信する
・未登録の人が映ったら録画する
・録画済みビデオのなかから登録された人を探し出す

ぜひ、あなただけの顔認証ソフトを作って楽しんでいただければと思います。

2
3
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
2
3