1回答

1收藏

[大赛作品提交] 树莓派基于opencv和百度ai平台的宿舍环境检测系统(二)

Raspberry Pi Raspberry Pi 5967 人阅读 | 1 人回复 | 2017-12-30

本帖最后由 ky123 于 2018-1-31 14:12 编辑

感谢e络盟提供的助赛基金。本帖主要描述百度ai人脸识别的调用
(一)百度ai人脸识别sdk
通过调用百度ai的sdk可以方便地实现高精度的人脸识别,并且可以摆脱树莓派性能的弊端

(二)jsoncpp安装
在更新过的人脸库中调用,即可返回识别结果的json结构,通过jsoncpp库解码可以获得结构的string字符串,因而首先要安装jsoncpp库,debain的系统下apt可以获得:
  1. sudo apt-get install libjsoncpp-dev
复制代码
(三)sdk调用的小demo
探索的过程中写了个百度ai人脸识别sdk的小demo:(关于app_id\api_key等私人性的数据用了***代替)
  1. #include <QCoreApplication>
  2. #include <face.h>           // 人脸识别交互类
  3. std::string app_id = "*****";
  4. std::string api_key = "******";
  5. std::string secret_key = "******";

  6. aip::Face client(app_id, api_key, secret_key);

  7. //**************hong declear****************
  8. std::string group_id = "group_tinnu";
  9. std::string uid1 = "jie";
  10. std::string user_info1 = "jie_info";

  11. //**************discount****************
  12. Json::Value add_user_one();             //从人脸库中新增用户,可以设定多个用户所在组,及组内用户的人脸图片
  13. Json::Value check_user();               //查询人脸库中某用户的详细信息
  14. Json::Value check_group();              //查询用户组的列表
  15. Json::Value check_all_user_of_group();  //查询指定用户组中的用户列表
  16. Json::Value delete_user_in_a_group();   //将用户从某个组中删除,但不会删除用户在其它组的信息

  17. int main(int argc, char *argv[])
  18. {
  19.     QCoreApplication a(argc, argv);

  20.     Json::Value result;

  21.     std::string image;
  22.     aip::get_file_content("frame.jpg", &image);

  23.     // 调用人脸识别
  24.     result = client.identify(group_id, image, aip::null);
  25.     Json::Value middle_num =  result["result"][0]["scores"][0];
  26.     double num = middle_num.asDouble();

  27.     std::cout << result << std::endl
  28.               << result["result"][0]["scores"][0] << std::endl
  29.               << num << std::endl;

  30.     return 0;
  31. }


  32. //**************hanshu****************
  33. Json::Value add_user_one()
  34. {
  35.     Json::Value result;

  36.     std::string image;
  37.     aip::get_file_content("<路径>/face1.jpg", &image);

  38.     // 调用人脸注册
  39.     result = client.user_add(uid1, user_info1, group_id, image, aip::null);

  40.     std::cout << result << std::endl;

  41.     return result;
  42. }

  43. Json::Value check_user()
  44. {
  45.     Json::Value result;

  46.     std::string uid = uid1;

  47.     // 调用用户信息查询
  48.     result = client.user_get(uid, aip::null);

  49.     /*// 如果有可选参数
  50.     std::map<std::string, std::string> options;
  51.     options["group_id"] = "group1";

  52.     // 带参数调用用户信息查询
  53.     result = client.user_get(uid, options);*/

  54.     std::cout << result << std::endl;

  55.     return result;
  56. }

  57. Json::Value check_group()
  58. {
  59.     Json::Value result;

  60.     // 调用组列表查询
  61.     result = client.group_getlist( aip::null);

  62.     // 如果有可选参数
  63.     /*std::map<std::string, std::string> options;
  64.     options["start"] = "0";
  65.     options["end"] = "50";

  66.     // 带参数调用组列表查询
  67.     result = client.group_getlist(, options);*/

  68.     std::cout << result << std::endl;

  69.     return result;
  70. }

  71. Json::Value check_all_user_of_group()
  72. {
  73.     Json::Value result;

  74.     //std::string group_id = "group_try";

  75.     // 调用组内用户列表查询
  76.     result = client.group_getusers(group_id, aip::null);

  77.     /*// 如果有可选参数
  78.     std::map<std::string, std::string> options;
  79.     options["start"] = "0";
  80.     options["end"] = "50";

  81.     // 带参数调用组内用户列表查询
  82.     result = client.group_getusers(group_id, options);*/

  83.     std::cout << result << std::endl;

  84.     return result;
  85. }

  86. //将用户从某个组中删除,但不会删除用户在其它组的信息
  87. Json::Value delete_user_in_a_group()
  88. {
  89.     Json::Value result;

  90.     std::string group_id = "group_try";

  91.     std::string uid = "name_try";

  92.     // 调用组内删除用户
  93.     result = client.group_deleteuser(group_id, uid, aip::null);

  94.     std::cout << result << std::endl;

  95.     return result;
  96. }
复制代码
pro文件也非常讲究:包含包裹jsoncpp在内的几个库,还有数个头文件以及用c++11编译,至于opencv的库倒是没有用到,不过为了方便后面程序的使用还是加上了。
  1. QT += core
  2. QT -= gui

  3. TARGET = face_check
  4. CONFIG += console
  5. CONFIG -= app_bundle

  6. TEMPLATE = app

  7. SOURCES += main.cpp \
  8.     opencv_operate.cpp

  9. HEADERS += \
  10.     base/base.h \
  11.     base/base64.h \
  12.     base/http.h \
  13.     base/utils.h \
  14.     other/face.h \
  15.     other/image_censor.h \
  16.     other/image_classify.h \
  17.     other/image_search.h \
  18.     other/kg.h \
  19.     other/nlp.h \
  20.     other/ocr.h \
  21.     other/speech.h \
  22.     opencv_operate.h

  23. LIBS += /usr/local/lib/libopencv_highgui.so \
  24.         /usr/local/lib/libopencv_core.so    \
  25.         /usr/local/lib/libopencv_features2d.so  \
  26.         /usr/local/lib/libopencv_calib3d.so \
  27.         /usr/local/lib/libopencv_xfeatures2d.so \
  28.         /usr/local/lib/libopencv_tracking.so    \
  29.         /usr/local/lib/libopencv_videoio.so \
  30.         /usr/local/lib/libopencv_video.so   \
  31.         /usr/local/lib/libopencv_imgproc.so \
  32.         /usr/local/lib/libopencv_ml.so      \
  33.         /usr/local/lib/libopencv_flann.so   \
  34.         /usr/local/lib/libopencv_objdetect.so   \
  35.         /usr/local/lib/libopencv_imgcodecs.so

  36. DISTFILES += \
  37.     other/out_put_message \
  38.     other/README.md



  39. OTHER_FILES += \
  40.     other/README.md

  41. INCLUDEPATH += /usr/local/include   \
  42.                 /usr/local/include/opencv   \
  43.                 /usr/local/include/opencv2  \
  44.                 /usr/include/jsoncpp \
  45.                 base    \
  46.                 other

  47. LIBS += -lcurl -lcrypto -ljsoncpp

  48. CONFIG += c++11

  49. DISTFILES += \
  50.     ../other/README.md \
  51.     other/out_put_message
复制代码
(四)调用函数
通过sdk中的identify函数可以获得与人脸库对比的结果:
  1. void* check( void *ptr )
  2. {
  3.     Json::Value result;

  4.     std::string image;
  5.     aip::get_file_content("frame.jpg", &image);

  6.     // 调用人脸识别
  7.     result = client.identify(group_id, image, aip::null);
  8.     double num = result["result"][0]["scores"][0].asDouble();

  9.     std::cout << result << num << std::endl;

  10.     if(num >= 70) check_flag = 1;
  11.     thread_num = 0;

  12.     return ptr;
  13. }
复制代码
由于访问百度ai云端会导致阻塞,因此开辟一个子线程调用是必要的,否则会遗失关键画面:
  1. //************** pthread ****************
  2. pthread_t thread;
  3. bool thread_num=0;
  4. void* check( void *ptr );

  5. //************** main ****************
  6. int main()
  7. {
  8.     int counter = 0;
  9.     int calcul = 0;

  10.     VideoCapture capture;
  11.     capture.open("/home/tinnu/baidu_ai/face_picture/4.mp4");
  12.     Mat frame;

  13.     while(!check_flag)
  14.     {
  15.         /*VideoCapture结构体,保存图像信息,open()参数为int index(0为默认摄像头),读入摄像头视频,
  16.              *                           open()参数为路径,读入视频文件*/


  17.         //double rate = capture.get(CV_CAP_PROP_FPS); //获取视频文件的帧率
  18.         //int delay = cvRound(1000.000 / rate);       //延时参数

  19.         while(1)
  20.         {
  21.             /*采用>>的方式读入视频*/
  22.             capture >> frame;
  23.             if(frame.empty())
  24.                 break;
  25.             if(face_detect(frame))
  26.             {
  27.                 std::cout << "people\t" << std::endl;
  28.                 counter++;
  29.             }
  30.             else
  31.                 counter = 0;

  32.             std::cout << calcul++ << '\t';

  33.             if(counter >= 5)
  34.             {
  35.                 counter = 0;
  36.                 break;
  37.             }

  38.             /*imshow()在窗口中显示*/
  39.             imshow("read",frame);

  40.             /*WaitKey()控制帧率*/
  41.             //waitKey(1);
  42.         }



  43.         std::cout << "judge";

  44.         imwrite("frame.jpg", frame);   //  将image图像保存为my.jpg

  45.         if(!thread_num)
  46.         {
  47.             pthread_create(&thread, NULL, check, NULL);
  48.             thread_num = 1;      //多线程资源占用
  49.         }

  50.     }

  51.     capture.release();
  52.     std::cout << "finish";

  53.     return 0;
  54. }
复制代码
(五)结果展示

关注下面的标签,发现更多相似文章
分享到:
回复

使用道具 举报

回答|共 1 个

倒序浏览

沙发

雪泥鸿爪

发表于 2017-12-31 10:27:57 | 只看该作者

树莓派基于OpenCV的和百度的AI平台的宿舍环境检测系统(一):https://www.cirmall.com/bbs/thread-98485-1-1.html
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条