コンテンツにスキップ

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

1. 人員検出概要

人員検出は、ディープラーニングに基づく人員の位置特定および検出手法です。セキュリティ、生産安全、その他のシナリオにおいて幅広く応用されており、境界侵入検知、境界横断検知、群衆集積検知、徘徊検知、転倒検知など、多くのアルゴリズムの基礎となるアルゴリズムです。

この人員検出アルゴリズムのデータセットにおける性能を以下に示します。

人員検出アルゴリズムmAP@0.5
PERSON0.79

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

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

2. クイックスタート

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

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

docker development environment startup

図 2-1

2.1 ソースコードの取得

Docker開発環境で、ソースコードリポジトリを保存する管理ディレクトリを作成します。gitツールを使用して、管理ディレクトリ内にリモートリポジトリをクローンします。

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

2.2 モデル配置

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

https://dl.dragonwake.com/download/rv1126b/AI/demo/03_person_detect.zip

person detect model release files

図 2-2

2.3 サンプルのビルド

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

Terminal window
cd rv1126b-ai-toolkit/Demos/algorithm-person_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

person detect build terminal output

図 2-3

2.4 サンプルの実行と結果

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

Terminal window
cp Release/* /mnt/userdata/Demo/algorithm-person/
cd /userdata/Demo/algorithm-person/
./test-person_detect person_detect.model test.jpg

person detect board run output

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

person detect result image

図 2-4

3. 人員検出API説明

3.1 リンク方法

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

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

3.2 人員検出初期化関数

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

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

3.3 人員検出実行関数

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

int person_detect_run(rknn_context ctx, cv::Mat input_image, person_detect_result_group_t *detect_result_group);
項目説明
関数名person_detect_run()
ヘッダーファイルperson_detect.h
入力パラメータctx: rknn_context handle
入力パラメータinput_image: cv::Mat input image
出力パラメータdetect_result_group: personnel detection result group
戻り値成功時の戻り値:0 / 失敗時の戻り値:-1
説明なし

3.4 人員検出解放関数

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

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

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

サンプルディレクトリは Demos/algorithm-person/test-person_detect.cpp です。処理フローは次のとおりです。

person detect sample flow

図 4-1

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

#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <sys/time.h>
#include"person_detect.h"
using namespace cv;
using namespace std;
static Scalar colorArray[10]={
Scalar(255, 0, 0, 255),
Scalar(0, 255, 0, 255),
Scalar(0,0,139,255),
Scalar(0,100,0,255),
Scalar(139,139,0,255),
Scalar(209,206,0,255),
Scalar(0,127,255,255),
Scalar(139,61,72,255),
Scalar(0,255,0,255),
Scalar(255,0,0,255),
};
int plot_one_box(Mat src, int x1, int x2, int y1, int y2, char *label, char colour)
{
int tl = round(0.002 * (src.rows + src.cols) / 2) + 1;
rectangle(src, cv::Point(x1, y1), cv::Point(x2, y2), colorArray[(unsigned char)colour], 3);
int tf = max(tl -1, 1);
int base_line = 0;
cv::Size t_size = getTextSize(label, FONT_HERSHEY_SIMPLEX, (float)tl/3, tf, &base_line);
int x3 = x1 + t_size.width;
int y3 = y1 - t_size.height - 3;
rectangle(src, cv::Point(x1, y1), cv::Point(x3, y3), colorArray[(unsigned char)colour], -1);
putText(src, label, cv::Point(x1, y1 - 2), FONT_HERSHEY_SIMPLEX, (float)tl/3, cv::Scalar(255, 255, 255, 255), tf, 8);
return 0;
}
int main(int argc, char **argv)
{
if (argc != 3)
{
printf("%s <model_path> <image_path>\n", argv[0]);
return -1;
}
const char *model_path = argv[1];
const char *image_path = argv[2];
/* パラメータ初期化 */
detect_result_group_t detect_result_group;
/* アルゴリズムモデル初期化 */
rknn_context ctx;
person_detect_init(&ctx, model_path);
/* アルゴリズム実行 */
cv::Mat src;
src = cv::imread(image_path, 1);
struct timeval start;
struct timeval end;
float time_use=0;
gettimeofday(&start,NULL);
person_detect_run(ctx, src, &detect_result_group);
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);
/* アルゴリズム結果を画像上に描画して保存します */
// Draw Objects
char text[256];
for (int i = 0; i < detect_result_group.count; i++)
{
detect_result_t* det_result = &(detect_result_group.results[i]);
if( det_result->prop < 0.4)
{
continue;
}
sprintf(text, "%s %.1f%%", det_result->name, det_result->prop * 100);
printf("%s @ (%d %d %d %d) %f\n", det_result->name, det_result->box.left, det_result->box.top,
det_result->box.right, det_result->box.bottom, det_result->prop);
int x1 = det_result->box.left;
int y1 = det_result->box.top;
int x2 = det_result->box.right;
int y2 = det_result->box.bottom;
/*
rectangle(src, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(255, 0, 0, 255), 3);
putText(src, text, cv::Point(x1, y1 + 12), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
*/
plot_one_box(src, x1, x2, y1, y2, text, i%10);
}
cv::imwrite("result.jpg", src);
/* アルゴリズムモデルのリソースを解放します */
person_detect_release(ctx);
return 0;
}