TA的每日心情 | 开心 2018-5-25 23:35 |
---|
签到天数: 6 天 连续签到: 1 天 [LV.2]偶尔看看I
|
本帖最后由 ky123 于 2018-1-31 14:12 编辑
感谢e络盟提供的助赛基金。本帖主要描述百度ai人脸识别的调用
(一)百度ai人脸识别sdk
通过调用百度ai的sdk可以方便地实现高精度的人脸识别,并且可以摆脱树莓派性能的弊端
(二)jsoncpp安装
在更新过的人脸库中调用,即可返回识别结果的json结构,通过jsoncpp库解码可以获得结构的string字符串,因而首先要安装jsoncpp库,debain的系统下apt可以获得:- sudo apt-get install libjsoncpp-dev
复制代码 (三)sdk调用的小demo
探索的过程中写了个百度ai人脸识别sdk的小demo:(关于app_id\api_key等私人性的数据用了***代替)- #include <QCoreApplication>
- #include <face.h> // 人脸识别交互类
- std::string app_id = "*****";
- std::string api_key = "******";
- std::string secret_key = "******";
- aip::Face client(app_id, api_key, secret_key);
- //**************hong declear****************
- std::string group_id = "group_tinnu";
- std::string uid1 = "jie";
- std::string user_info1 = "jie_info";
- //**************discount****************
- Json::Value add_user_one(); //从人脸库中新增用户,可以设定多个用户所在组,及组内用户的人脸图片
- Json::Value check_user(); //查询人脸库中某用户的详细信息
- Json::Value check_group(); //查询用户组的列表
- Json::Value check_all_user_of_group(); //查询指定用户组中的用户列表
- Json::Value delete_user_in_a_group(); //将用户从某个组中删除,但不会删除用户在其它组的信息
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- Json::Value result;
- std::string image;
- aip::get_file_content("frame.jpg", &image);
- // 调用人脸识别
- result = client.identify(group_id, image, aip::null);
- Json::Value middle_num = result["result"][0]["scores"][0];
- double num = middle_num.asDouble();
- std::cout << result << std::endl
- << result["result"][0]["scores"][0] << std::endl
- << num << std::endl;
- return 0;
- }
- //**************hanshu****************
- Json::Value add_user_one()
- {
- Json::Value result;
- std::string image;
- aip::get_file_content("<路径>/face1.jpg", &image);
- // 调用人脸注册
- result = client.user_add(uid1, user_info1, group_id, image, aip::null);
- std::cout << result << std::endl;
- return result;
- }
- Json::Value check_user()
- {
- Json::Value result;
- std::string uid = uid1;
- // 调用用户信息查询
- result = client.user_get(uid, aip::null);
- /*// 如果有可选参数
- std::map<std::string, std::string> options;
- options["group_id"] = "group1";
- // 带参数调用用户信息查询
- result = client.user_get(uid, options);*/
- std::cout << result << std::endl;
- return result;
- }
- Json::Value check_group()
- {
- Json::Value result;
- // 调用组列表查询
- result = client.group_getlist( aip::null);
- // 如果有可选参数
- /*std::map<std::string, std::string> options;
- options["start"] = "0";
- options["end"] = "50";
- // 带参数调用组列表查询
- result = client.group_getlist(, options);*/
- std::cout << result << std::endl;
- return result;
- }
- Json::Value check_all_user_of_group()
- {
- Json::Value result;
- //std::string group_id = "group_try";
- // 调用组内用户列表查询
- result = client.group_getusers(group_id, aip::null);
- /*// 如果有可选参数
- std::map<std::string, std::string> options;
- options["start"] = "0";
- options["end"] = "50";
- // 带参数调用组内用户列表查询
- result = client.group_getusers(group_id, options);*/
- std::cout << result << std::endl;
- return result;
- }
- //将用户从某个组中删除,但不会删除用户在其它组的信息
- Json::Value delete_user_in_a_group()
- {
- Json::Value result;
- std::string group_id = "group_try";
- std::string uid = "name_try";
- // 调用组内删除用户
- result = client.group_deleteuser(group_id, uid, aip::null);
- std::cout << result << std::endl;
- return result;
- }
复制代码 pro文件也非常讲究:包含包裹jsoncpp在内的几个库,还有数个头文件以及用c++11编译,至于opencv的库倒是没有用到,不过为了方便后面程序的使用还是加上了。- QT += core
- QT -= gui
- TARGET = face_check
- CONFIG += console
- CONFIG -= app_bundle
- TEMPLATE = app
- SOURCES += main.cpp \
- opencv_operate.cpp
- HEADERS += \
- base/base.h \
- base/base64.h \
- base/http.h \
- base/utils.h \
- other/face.h \
- other/image_censor.h \
- other/image_classify.h \
- other/image_search.h \
- other/kg.h \
- other/nlp.h \
- other/ocr.h \
- other/speech.h \
- opencv_operate.h
- LIBS += /usr/local/lib/libopencv_highgui.so \
- /usr/local/lib/libopencv_core.so \
- /usr/local/lib/libopencv_features2d.so \
- /usr/local/lib/libopencv_calib3d.so \
- /usr/local/lib/libopencv_xfeatures2d.so \
- /usr/local/lib/libopencv_tracking.so \
- /usr/local/lib/libopencv_videoio.so \
- /usr/local/lib/libopencv_video.so \
- /usr/local/lib/libopencv_imgproc.so \
- /usr/local/lib/libopencv_ml.so \
- /usr/local/lib/libopencv_flann.so \
- /usr/local/lib/libopencv_objdetect.so \
- /usr/local/lib/libopencv_imgcodecs.so
- DISTFILES += \
- other/out_put_message \
- other/README.md
- OTHER_FILES += \
- other/README.md
- INCLUDEPATH += /usr/local/include \
- /usr/local/include/opencv \
- /usr/local/include/opencv2 \
- /usr/include/jsoncpp \
- base \
- other
- LIBS += -lcurl -lcrypto -ljsoncpp
- CONFIG += c++11
- DISTFILES += \
- ../other/README.md \
- other/out_put_message
复制代码 (四)调用函数
通过sdk中的identify函数可以获得与人脸库对比的结果:- void* check( void *ptr )
- {
- Json::Value result;
- std::string image;
- aip::get_file_content("frame.jpg", &image);
- // 调用人脸识别
- result = client.identify(group_id, image, aip::null);
- double num = result["result"][0]["scores"][0].asDouble();
- std::cout << result << num << std::endl;
- if(num >= 70) check_flag = 1;
- thread_num = 0;
- return ptr;
- }
复制代码 由于访问百度ai云端会导致阻塞,因此开辟一个子线程调用是必要的,否则会遗失关键画面:- //************** pthread ****************
- pthread_t thread;
- bool thread_num=0;
- void* check( void *ptr );
- //************** main ****************
- int main()
- {
- int counter = 0;
- int calcul = 0;
- VideoCapture capture;
- capture.open("/home/tinnu/baidu_ai/face_picture/4.mp4");
- Mat frame;
- while(!check_flag)
- {
- /*VideoCapture结构体,保存图像信息,open()参数为int index(0为默认摄像头),读入摄像头视频,
- * open()参数为路径,读入视频文件*/
- //double rate = capture.get(CV_CAP_PROP_FPS); //获取视频文件的帧率
- //int delay = cvRound(1000.000 / rate); //延时参数
- while(1)
- {
- /*采用>>的方式读入视频*/
- capture >> frame;
- if(frame.empty())
- break;
- if(face_detect(frame))
- {
- std::cout << "people\t" << std::endl;
- counter++;
- }
- else
- counter = 0;
- std::cout << calcul++ << '\t';
- if(counter >= 5)
- {
- counter = 0;
- break;
- }
- /*imshow()在窗口中显示*/
- imshow("read",frame);
- /*WaitKey()控制帧率*/
- //waitKey(1);
- }
- std::cout << "judge";
- imwrite("frame.jpg", frame); // 将image图像保存为my.jpg
- if(!thread_num)
- {
- pthread_create(&thread, NULL, check, NULL);
- thread_num = 1; //多线程资源占用
- }
- }
- capture.release();
- std::cout << "finish";
- return 0;
- }
复制代码 (五)结果展示
|
|