本文來源電子發(fā)燒友社區(qū),作者:碼農(nóng)愛學(xué)習(xí), 帖子地址:https://bbs.elecfans.com/jishu_2308719_1_1.html
上篇文章,測(cè)試了EASY EAI Nano的人臉檢測(cè)功能,本篇進(jìn)行人臉識(shí)別功能。
人臉檢測(cè),只是將圖像中的人臉的位置檢測(cè)出來,人臉識(shí)別,又增加了一部,在檢測(cè)到人臉后,還要識(shí)別出這張臉是誰的臉。
本篇參考官方文檔:https://www.easy-eai.com/document_details/3/110
1 API介紹
組件 | 頭文件以及庫路徑 | 描述 |
---|---|---|
系統(tǒng)操作組件 | easyeai-api/common_api/system_opt | 提供線程操作函數(shù) |
攝像頭組件 | easyeai-api/peripheral_api/camera | 提供攝像頭操作函數(shù) |
顯示屏組件 | easyeai-api/peripheral_api/display | 提供顯示屏操作函數(shù) |
平面幾何組件 | easyeai-api/algorithm_api/geometry | 提供簡(jiǎn)單幾何運(yùn)算函數(shù) |
人臉檢測(cè)組件 | easyeai-api/algorithm_api/face_detect | 提供人臉檢測(cè)操作函數(shù) |
人臉校正組件 | easyeai-api/algorithm_api/face_alignment | 提供人臉校正操作函數(shù) |
人臉識(shí)別組件 | easyeai-api/algorithm_api/face_recognition | 提供人臉識(shí)別操作函數(shù) |
主要來看下人臉識(shí)別組件。
face_recognition.h的主要內(nèi)容
//人臉識(shí)別初始化函數(shù)
int face_recognition_init(rknn_context *ctx, const char * path);
?
//人臉識(shí)別執(zhí)行函數(shù)
int face_recognition_run(rknn_context ctx, cv::Mat *face_image, float (*feature)[512]);
?
//人臉識(shí)別特征比對(duì)函數(shù)
float face_recognition_comparison(float *feature_1, float *feature_2, int output_len);
?
//人臉識(shí)別釋放函數(shù)
int face_recognition_release(rknn_context ctx);
一些參數(shù):
- ctx:輸入?yún)?shù),rknn_context句柄
- path:輸入?yún)?shù),算法模型路徑
- face_image:輸入?yún)?shù),圖像數(shù)據(jù)輸入(cv::Mat是Opencv的類型)
- feature:輸出參數(shù),算法輸出的人臉特征碼
face_alignment.h的主要內(nèi)容
cv::Mat face_alignment(cv::Mat img, cv::Point2f* points);
geometry.h的主要內(nèi)容
typedef struct{
float x;
float y;
}fPoint_t;
?
typedef struct{
float left; //x1
float top; //y1
float right; //x2
float bottom; //y2
}fRect_t;
?
typedef struct{
int32_t x;
int32_t y;
}s32Point_t;
?
typedef struct{
int32_t left; //x1
int32_t top; //y1
int32_t right; //x2
int32_t bottom; //y2
}s32Rect_t;
?
//判斷點(diǎn)是否在矩形內(nèi)
extern bool point_in_rect(s32Point_t point, s32Rect_t rect);
?
//計(jì)算矩形面積
extern int32_t calc_rect_square(s32Rect_t rect);
?
//找出面積較小矩形
extern s32Rect_t min_rect(s32Rect_t rect1, s32Rect_t rect2);
?
//找出面積較大矩形
extern s32Rect_t max_rect(s32Rect_t rect1, s32Rect_t rect2);
?
//判斷矩形是否相交或相切
extern bool rect_is_intersect(s32Rect_t rect1, s32Rect_t rect2);
?
//計(jì)算兩矩形相交部分面積(若相切,面積也為0)
extern int32_t calc_rect_intersect_square(s32Rect_t rect1, s32Rect_t rect2);
?
//計(jì)算[兩矩形相交部分面積]與[小矩形面積]之比
extern double calc_intersect_of_min_rect(s32Rect_t rect1, s32Rect_t rect2);
?
//計(jì)算兩矩形的交并比
extern double calc_intersect_of_union(s32Rect_t rect1, s32Rect_t rect2);
2 代碼分析與修改
主要的修改是將紅外攝像頭采集和活體檢測(cè)去掉,改用外接RGB攝像頭,另外,識(shí)別結(jié)果的顯示,將更多的信息(識(shí)別的id,人物名稱,耗時(shí)等)展示在屏幕上。
2.1 圖像采集與顯示線程(主線程)
重新定義識(shí)別結(jié)果:
typedef struct{
bool bHasFace; //是否檢測(cè)到人臉
bool bMatch; //是否與注冊(cè)的人臉匹配
char idStr[128]; //匹配的人臉的id
char nameStr[128]; //匹配的人臉的名稱
float similarity; //匹配的相似度
uint64_t useTime; //匹配用時(shí)
uint32_t x1; //識(shí)別到的人臉框的4個(gè)點(diǎn)
uint32_t y1;
uint32_t x2;
uint32_t y2;
}Result_t;
官方例程用到了紅外攝像頭,用于活體檢測(cè),此次測(cè)試,為了使用電腦屏幕上的人物圖片進(jìn)行人臉檢測(cè),去掉了活體檢測(cè)功能,并換做只使用USB攝像頭采集RGB圖像進(jìn)行人臉識(shí)別。
主函數(shù)邏輯如下,和上篇進(jìn)行人臉檢測(cè)的代碼邏輯類似。
#define COLOR_RED Scalar(255, 0, 0)
#define COLOR_GREEN Scalar(0, 255, 0)
?
int main(int argc, char **argv)
{
int ret = 0;
int rgbRet = 0;
disp_screen_t screen = {0};
char *pRGBbuf = NULL;
int skip = 0;
pthread_t mTid;
Result_t *pResult = NULL;
Mat image;
// 1.打開USB攝像頭
ret = usbcamera_init(USB2_0, USB_DIRECT, CAMERA_WIDTH, CAMERA_HEIGHT, 180);
if (ret) {
printf("error: %s, %dn", __func__, __LINE__);
goto exit_donothing;
}
pRGBbuf = NULL;
pRGBbuf = (char *)malloc(IMAGE_SIZE);
if (!pRGBbuf) {
printf("error: %s, %dn", __func__, __LINE__);
ret = -1;
goto exit_freeusb;
}
// 跳過前10幀
skip = 10;
while(skip--) {
ret = usbcamera_getframe(USB2_0, USB_DIRECT, pRGBbuf);
if (ret) {
printf("error: %s, %dn", __func__, __LINE__);
goto exit_freeusb_freebuf;
}
}
?
// 2.創(chuàng)建識(shí)別線程,以及圖像互斥鎖
pthread_mutex_init(&img_lock, NULL);
pResult = (Result_t *)malloc(sizeof(Result_t));
if(NULL == pResult){
goto exit_free_all;
}
memset(pResult, 0, sizeof(Result_t));
if(0 != CreateNormalThread(detect_thread_entry, pResult, &mTid)){
free(pResult);
}
?
// 3.顯示初始化
screen.screen_width = SCREEN_WIDTH;
screen.screen_height = SCREEN_HEIGHT;
screen.wins[0].enable = 1;
screen.wins[0].in_fmt = IMAGE_TYPE_RGB888;
screen.wins[0].in_w = CAMERA_WIDTH;
screen.wins[0].in_h = CAMERA_HEIGHT;
screen.wins[0].rotation = 90;
screen.wins[0].win_x = 0;
screen.wins[0].win_y = 0;
screen.wins[0].win_w = 720;
screen.wins[0].win_h = 1280;
ret = disp_init_pro(&screen);
if (ret) {
printf("error: %s, %dn", __func__, __LINE__);
goto exit_free_all;
}
// 4.(取流 + 顯示)循環(huán)
while(1)
{
// 4.1、取流
pthread_mutex_lock(&img_lock);
ret = usbcamera_getframe(USB2_0, USB_DIRECT, pRGBbuf);
if (0 != rgbRet)
{
printf("error: %s, %dn", __func__, __LINE__);
pthread_mutex_unlock(&img_lock);
continue;
}
algorithm_image = Mat(CAMERA_HEIGHT, CAMERA_WIDTH, CV_8UC3, pRGBbuf);
image = algorithm_image.clone();
pthread_mutex_unlock(&img_lock);
?
// 4.2、顯示
if (pResult->bHasFace) //檢測(cè)到人臉
{
Scalar color;
//識(shí)別到已注冊(cè)的人臉
if(pResult->bMatch)
{
color = COLOR_GREEN;
cv::putText(image, std::string("idStr: ") + std::string(pResult->idStr), cv::Point2f(30, 50), cv::FONT_HERSHEY_SIMPLEX, 1.45, CV_RGB(255,0,0),3.0);
cv::putText(image, std::string("name: ") + std::string(pResult->nameStr), cv::Point2f(30, 100), cv::FONT_HERSHEY_SIMPLEX, 1.45, CV_RGB(255,0,0),3.0);
cv::putText(image, std::string("similarity: ") + std::to_string(pResult->similarity), cv::Point2f(30, 150), cv::FONT_HERSHEY_SIMPLEX, 1.45, CV_RGB(255,0,0),3.0);
cv::putText(image, std::string("use time: ") + std::to_string(pResult->useTime), cv::Point2f(30, 200), cv::FONT_HERSHEY_SIMPLEX, 1.45, CV_RGB(255,0,0),3.0);
}
else
{
color = COLOR_RED;
cv::putText(image, std::string("unknow face"), cv::Point2f(30, 50), cv::FONT_HERSHEY_SIMPLEX, 1.45, CV_RGB(0,0,255),3.0);
}
// 畫框
rectangle(image, Point(pResult->x1, pResult->y1), Point(pResult->x2, pResult->y2), color, 3);
}
disp_commit(image.data, IMAGE_SIZE);
usleep(20*1000);
}
?
exit_free_all:
pthread_mutex_destroy(&img_lock);
exit_freeusb_freebuf:
free(pRGBbuf);
pRGBbuf = NULL;
exit_freeusb:
usbcamera_exit(USB2_0, USB_DIRECT);
exit_donothing:
return ret;
}
2.2 人臉識(shí)別處理
去掉紅外攝像頭的活體檢測(cè)邏輯,只使用USB攝像頭的圖像進(jìn)行人臉識(shí)別
void *detect_thread_entry(void *para)
{
int ret;
uint64_t start_time,end_time;
Result_t *pResult = (Result_t *)para;
// 初始化人臉檢測(cè)
rknn_context detect_ctx;
std::vector detect_result;
Point2f points[5];
s32Rect_t rgbRect;
printf("face detect init!n");
ret = face_detect_init(&detect_ctx, "./face_detect.model");
if( ret < 0)
{
printf("face_detect fail! ret=%dn", ret);
return NULL;
}
// 初始化人臉識(shí)別
rknn_context recognition_ctx;
float face_feature[512];
printf("face recognition init!n");
ret = face_recognition_init(&recognition_ctx, "./face_recognition.model");
if( ret < 0)
{
printf("face_recognition fail! ret=%dn", ret);
return NULL;
}
?
// 初始化數(shù)據(jù)庫
database_init();
// 同步數(shù)據(jù)庫所有數(shù)據(jù)到內(nèi)存
faceData_t *pFaceData = (faceData_t *)malloc(MAX_USER_NUM * sizeof(faceData_t));
memset(pFaceData, 0, MAX_USER_NUM * sizeof(faceData_t));
int peopleNum = database_getData_to_memory(pFaceData);
// 初始化按鍵事件
keyEvent_init();
set_event_handle(dataBase_opt_handle);
Mat image;
Mat face_algin;
float similarity; //特征值相似度比對(duì)
int face_index = 0;
while(1)
{
if(g_delete_all_record)
{
g_delete_all_record = false;
// 刪除庫
database_delete_all_record();
// 重載數(shù)據(jù)庫
peopleNum = database_getData_to_memory(pFaceData);
}
if(algorithm_image.empty())
{
usleep(5);
continue;
}
pthread_mutex_lock(&img_lock);
image = algorithm_image.clone();
pthread_mutex_unlock(&img_lock);
// 人臉檢測(cè),計(jì)算出人臉位置
ret = face_detect_run(detect_ctx, image, detect_result);
if(ret <= 0)
{
// 識(shí)別結(jié)果數(shù)據(jù),復(fù)位
memset(pResult, 0 , sizeof(Result_t));
g_input_feature = false;
usleep(1000);
continue;
}
rgbRect.left = (uint32_t)(detect_result[0].box.x);
rgbRect.top = (uint32_t)(detect_result[0].box.y);
rgbRect.right = (uint32_t)(detect_result[0].box.x + detect_result[0].box.width);
rgbRect.bottom = (uint32_t)(detect_result[0].box.y + detect_result[0].box.height);
pResult->bHasFace = true;
pResult->x1 = rgbRect.left;
pResult->y1 = rgbRect.top;
pResult->x2 = rgbRect.right;
pResult->y2 = rgbRect.bottom;
for (int i = 0; i < (int)detect_result[0].landmarks.size(); ++i) {
points[i].x = (int)detect_result[0].landmarks[i].x;
points[i].y = (int)detect_result[0].landmarks[i].y;
}
// 人臉校正(從圖像中裁出人臉)
face_algin = face_alignment(image, points);
// 人臉識(shí)別,計(jì)算特征值
start_time = get_timeval_ms();
face_recognition_run(recognition_ctx, &face_algin, &face_feature);
end_time = get_timeval_ms();
pResult->useTime = end_time - start_time;
printf("n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>n");
printf("face_recognition_run use time: %llun", pResult->useTime); // 【打印耗時(shí)】
// 特征值比對(duì),得出id
similarity = -0.5;
if(peopleNum > 0)
{
for(face_index = 0; face_index < peopleNum; ++face_index)
{
similarity = face_recognition_comparison(face_feature, (float *)((pFaceData + face_index)->feature), 512);
if(similarity > 0.5)
{
break;
}
}
}
pResult->similarity = similarity;
printf("similarity:%fn", similarity); //【打印得分】
if((face_index < peopleNum)&&(similarity > 0.5))
{
pResult->bMatch = true;
// 用id,找名字
memcpy(pResult->idStr, (pFaceData + face_index)->id, 128);
printf("idStr : %sn", pResult->idStr); //【打印id】
database_id_is_exist(pResult->idStr, pResult->nameStr, sizeof(pResult->nameStr));
printf("person name : %sn", pResult->nameStr); //【打印名字】
// 按鍵被按下,更新特征值
if(g_input_feature)
{
g_input_feature = false;
// 特征值入庫
database_add_record(pResult->idStr, pResult->nameStr, (char *)face_feature, sizeof(face_feature));
// 重載數(shù)據(jù)庫
peopleNum = database_getData_to_memory(pFaceData);
}
}
else
{
pResult->bMatch = false;
// 按鍵被按下,錄入特征值
if(g_input_feature)
{
g_input_feature = false;
char idStr[32]={0};
char nameStr[32]={0};
sprintf(idStr, "%05d", face_index+1);
sprintf(nameStr, "people_%d", face_index+1);
// 特征值入庫
database_add_record(idStr, nameStr, (char *)face_feature, sizeof(face_feature));
// 重載數(shù)據(jù)庫
peopleNum = database_getData_to_memory(pFaceData);
}
}
usleep(16*1000);
}
/* 數(shù)據(jù)庫內(nèi)存數(shù)據(jù)釋放 */
free(pFaceData);
/* 數(shù)據(jù)庫釋放 */
database_exit();
/* 人臉識(shí)別釋放 */
face_recognition_release(recognition_ctx);
/* 人臉檢測(cè)釋放 */
face_detect_release(detect_ctx);
return NULL;
}
3 測(cè)試
測(cè)試視頻見文章底部視頻,這里再放一張測(cè)試圖片:
4 總結(jié)
本篇介紹了EASY EAI Nano的人臉識(shí)別功能,與上篇的人臉檢測(cè)相比,在檢測(cè)到有人臉的基礎(chǔ)上,通過先錄入人臉顯示到數(shù)據(jù)庫,可以對(duì)比當(dāng)前識(shí)別的誰的臉,實(shí)際測(cè)試,去掉活體檢測(cè)功能后,通過外接USB攝像頭來識(shí)別電腦屏幕上的3個(gè)人的多張不同人臉,可以分辨出不同人的人臉。
-----------
附上EASY EAI Nano多人臉識(shí)別測(cè)試【視頻】,詳見作者原帖子內(nèi)容。
-
AI
+關(guān)注
關(guān)注
87文章
31262瀏覽量
269625 -
人臉識(shí)別
+關(guān)注
關(guān)注
76文章
4014瀏覽量
82083 -
靈眸
+關(guān)注
關(guān)注
0文章
19瀏覽量
3213
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論