コンテンツにスキップ

顔検出アルゴリズム実行ガイド

1. 顔検出概要

顔検出は、顔認識、顔属性分類、顔編集、顔追跡などのタスクに不可欠な初期処理です。その性能は、顔認識など後段タスクの有効性に直接影響します。過去数十年で非制約環境における顔検出は大きく進歩しましたが、実環境で高精度かつ高効率に顔を検出することは、現在も重要な課題です。これは、姿勢変化、表情、スケール、照明条件、画像の歪み、顔の遮蔽などの要因によるものです。一般的な物体検出と異なり、顔検出ではアスペクト比の変化は比較的小さい一方で、数ピクセルから数千ピクセルまでスケール変化が非常に大きいという特徴があります。

本顔検出アルゴリズムデータセットでの性能は次のとおりです。

顔検出アルゴリズムperformance
FDDB98.64%

face detection fddb performance curve

図 1-1

CSUN RV1126B基板での実行効率は次のとおりです。

アルゴリズム種別実行効率
face_detect24ms

2. クイックスタート

2.1 開発環境の準備

本ドキュメントを初めてお読みになる場合は、『入門ガイド/開発環境準備の準備と更新』を参照し、関連する手順に従ってコンパイル環境をデプロイしてください。PC側のUbuntuシステムで run スクリプトを実行し、Docker開発環境に入ります。

Terminal window
cd ~/linuxshare/work/rv1126b/jp/embedded/images/develop_environment
./run.sh 2204

docker development environment startup

図 1-2

2.2 ソースコードのダウンロード

Docker開発環境で、ソースコードリポジトリを保存する管理ディレクトリを作成します。gitツールを使用して、管理ディレクトリ内にリモートリポジトリをクローンします。GitHubのWebページからダウンロードする場合でも、リポジトリ全体をダウンロードしてください。本サンプルに対応するディレクトリだけを単独でダウンロードしないでください。

Terminal window
cd /opt/linuxshare/work/rv1126b/jp/AI/demo/ai-algorithm
git clone https://github.com/csunltd/rv1126b-ai-toolkit.git

2.3 モデルデプロイ

アルゴリズムDemoを実行するには、まず顔検出アルゴリズムモデルをダウンロードする必要があります。ダウンロードしたファイルを解凍し、顔検出アルゴリズムモデルを Release/ ディレクトリへコピーしてください。

https://dl.dragonwake.com/download/rv1126b/AI/demo/01_face-detect.zip

face detect model release files

図 2-1

2.4 サンプルのビルド

サンプルディレクトリに移動してビルドを実行します。依存ライブラリは開発ボード上に配置されているため、クロスコンパイル中は /mnt のマウントを保持する必要があります。build.sh cpres を指定すると、Release/ ディレクトリ内のリソースが開発ボードへコピーされます。

Terminal window
cd rv1126b-ai-toolkit/Demos/algorithm-face_detect
./build.sh cpres
Terminal window
sudo mount -t nfs -o vers=3,proto=tcp,mountproto=tcp,nolock,retrans=5,timeo=5 192.168.10.85:/ /mnt

face detect build terminal output

図 2-2

2.5 サンプル実行および結果

コンパイル済みのファイルを基板にコピーし、シリアルデバッグまたはSSHデバッグで開発ボードにログインしてサンプルを実行します。Dockerコンパイル環境で結果画像を取得できます。

Terminal window
cp Release/* /mnt/userdata/Demo/algorithm-face_detect/
cd /userdata/Demo/algorithm-face_detect/
./test-face-detect test.jpg

face detect release copy output

図 2-3

face detect board run output

Terminal window
cp /mnt/userdata/Demo/algorithm-face_detect/result.jpg .

face detect result image

図 2-4

3. 顔検出API説明

3.1 参照方法

ローカルプロジェクトからEASY EAI APIライブラリを直接呼び出す場合、以下のヘッダファイルディレクトリ、ライブラリディレクトリおよびリンクパラメータを追加します。

項目説明
ヘッダファイルディレクトリeasyeai-api/algorithm/face_detect
ライブラリファイルディレクトリeasyeai-api/algorithm/face_detect
ライブラリリンクパラメータ-lface_detect

3.2 顔検出初期化関数

プロトタイプは次のとおりです。

int face_detect_init(rknn_context *ctx, const char *path);

詳細は次のとおりです。

項目説明
関数名face_detect_init()
ヘッダファイルface_detect.h
入力パラメータctx: rknn_context handle
入力パラメータpath: algorithm model path
戻り値成功時の戻り値:0 / 失敗時の戻り値:-1
説明なし

3.3 顔検出実行関数

プロトタイプは次のとおりです。

int face_detect_run(rknn_context ctx, cv::Mat &input_image, std::vector<det> &result);
項目説明
関数名face_detect_run()
ヘッダファイルface_detect.h
入力パラメータctx: rknn_context handle
入力パラメータinput_image: cv::Mat input image
出力パラメータresult: face detection result
戻り値成功時の戻り値:0 / 失敗時の戻り値:-1
説明なし

3.4 顔検出解放関数

プロトタイプは次のとおりです。

int face_detect_release(rknn_context ctx);
項目説明
関数名face_detect_release()
ヘッダファイルface_detect.h
入力パラメータctx: rknn_context handle
戻り値成功時の戻り値:0 / 失敗時の戻り値:-1
説明なし

4. 顔検出アルゴリズムサンプル

サンプルディレクトリは Demos/algorithm-face_detect/test-face-detect.cpp です。操作フローは次のとおりです。

face detect sample flow

図 3-1

参考サンプルは次のとおりです。

#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <sys/time.h>
#include "face_detect.h"
using namespace cv;
int main(int argc, char **argv)
{
if( argc != 2)
{
printf("./test-face-detect xxx\n");
return -1;
}
struct timeval start;
struct timeval end;
float time_use=0;
rknn_context ctx;
std::vector<det> result;
Mat image;
image = cv::imread(argv[1], 1);
face_detect_init(&ctx, "face_detect.model");
gettimeofday(&start,NULL);
face_detect_run(ctx, image, result);
gettimeofday(&end,NULL);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//マイクロ秒
printf("time_use is %f\n",time_use/1000);
printf("face num:%d\n", (int)result.size());
for (int i = 0; i < (int)result.size(); i++)
{
int x = (int)(result[i].box.x);
int y = (int)(result[i].box.y);
int w = (int)(result[i].box.width);
int h = (int)(result[i].box.height);
rectangle(image, Rect(x, y, w, h), Scalar(0, 255, 0), 2, 8, 0);
for (int j = 0; j < (int)result[i].landmarks.size(); ++j)
{
cv::circle(image, cv::Point((int)result[i].landmarks[j].x, (int)result[i].landmarks[j].y), 2, cv::Scalar(225, 0, 225), 2, 8);
}
}
imwrite("result.jpg", image);
face_detect_release(ctx);
return 0;
}