(linux)人脸考勤(2)

百度智能云平台接入与项目验收

1、在智能云平台注册账号并创建一个应用
在左上角点击产品、然后点击人脸与人体,这里我们主要使用的是人脸搜索功能。
在这里插入图片描述
点击立即使用
在这里插入图片描述
在左边这里创建一个应用
在这里插入图片描述
创建完之后我们打开右边的人脸库,在人脸库中添加几张人脸照片
在这里插入图片描述

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/e6a209ab799f4161b40e7ac4dff9160b.png
2、SDK环境搭建
SDK下载地址:https://cloud.baidu.com/doc/FACE/s/Uk37c1r11
在这里插入图片描述
在这里插入图片描述
将SDK压缩包解压缩后的文件夹放到Ubuntu里面

SDK包相关依赖下载:安装依赖库libcurl(需要支持https), openssl,jsoncpp(>1.6.2版本,0.x版本将不被支持)。

libcurl(网络通信的协议库)

openssl(网络通信加密)

jsoncpp(提取信息)
安装:
sudo apt-get install libcurl4-openssl-dev

sudo apt-get install openssl

sudo apt-get install libjsoncpp-dev

sudo apt-get install libssl-dev
检查是否安装成功:dpkg -s libcurl4-openssl-dev

dpkg -s openssl

dpkg -s libjsoncpp-dev

sudo apt-get install libssl-dev`
编译(文件库支持C++11版本,同时需要加上第三方库)

将cpp文件放置于SDK文件夹里面,方便编译时找到头文件

编译文件时候在最后加入 ( -std=c++11 -lcurl -lcrypto -ljsoncpp)

g++ main.cpp -o main -lopencv_highgui -lopencv_core -lopencv_imgproc -lopencv_objdetect -std=c++11 -lcurl -lcrypto -ljsoncpp

修改 base.h 和 http.h 文件中json头文件的目录位置

    #include <jsoncpp/json/json.h>
    加入相关头文件和命名空间

    #include "face.h"

    using namespace aip;

3、 百度只能云平台的接入
百度提供的代码:` // 设置APPID/AK/SK
std::string app_id = “你的 App ID”;
std::string api_key = “你的 Api key”;
std::string secret_key = “你的 Secret Key”;
aip::Face client(app_id, api_key, secret_key);
也可以用下面的方式直接写
在这里插入图片描述
// 调用人脸搜索
result = client.face_search_v3(image, image_type, group_id_list, aip::null);
// 带参数调用人脸搜索
result = client.face_search_v3(image, image_type, group_id_list, options);

//此处options填你最终储存容器

// 如果有可选参数
std::map<std::string, std::string> options;
options[“match_threshold”] = “70”;
options[“quality_control”] = “NORMAL”;
options[“liveness_control”] = “LOW”;
options[“user_id”] = “233451”;
options[“max_user_num”] = “3”;

image :图片信息(总数据大小应小于10M)

image_type:图片类型 BASE64:(图片大小不超过2M),URL:图片的 URL地址,FACE_TOKEN: 人脸图片的唯一标识。

group_id_list:从指定的group中进行查找 用逗号分隔,上限10个
这里我们使用base64编码:
base64_encode(const char* bytes_to_encode, unsigned int int_len);
我们设定一个收集返回信息的容器
Json::Value result;
6.1Json数据解析
观察百度所给的返回数据形式

    我们要获取的目标是人脸所对应的人的信息,所以我们只需要从数据中提取特定信息
    1.判断是否检测到人脸
      opencv所给模型不太精确,会将疑似为人脸的物体也上传。百度会进行第二次检测是否有人脸。如果返回为空则不打印信息
    2.判断人脸匹配得分
     如果匹配得分很低说明只是有人脸,百度会返回得分最高的人,即使得分很低,所以结果会不准确。所以我们要控制得分在80以上
    3.只返回人脸所匹配的人的名字
       其他信息对我们获取人的身份信息没有什么作用,我们只需要获取匹配人的名字
#include <iostream>
#include "opencv2/opencv.hpp"
#include "face.h"

using namespace std;
using namespace cv;
using namespace aip;
int main()
{
   VideoCapture cap(0);
      if(!cap.isOpened())
      {
      cout<<"camera open failed"<<endl;
      return 0;
      }
   cout<<"camera open success"<<endl;

   CascadeClassifier Classfier("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml");

   aip::Face client("59463877", "ay4iGoMumjpQsEFqyLAIpHB0", "NKKSErwrmNFrWgP0AZ8Ukfyp6SuM5wTu");

   Mat ColorImage;//apply for a Container for storing photos
   Mat GrayImage;
   vector<Rect> Allface;
   Mat MatFace;
   vector<uchar> JpgFace;
   string Base64Face;
   Json::Value result;
   time_t sec;
      for(;;)
      {
      cap >> ColorImage; // store photos taken
      cvtColor(ColorImage, GrayImage, CV_BGR2GRAY );
      equalizeHist( GrayImage, GrayImage);
      Classfier.detectMultiScale(GrayImage, Allface);
        if(Allface.size())
        {	
        rectangle(GrayImage, Allface[0], Scalar(255,255,255));
        MatFace=GrayImage(Allface[0]);
        imencode(".jpg", MatFace, JpgFace);
        Base64Face = base64_encode((char *)JpgFace.data(), JpgFace.size());
 
        result=client.face_search_v3(Base64Face, "BASE64", "Teaching",result);
           if(!result["result"].isNull())
           {
                if(result["result"]["user_list"][0]["score"].asInt()>80)
                {
                 cout<<result["result"]["user_list"][0]["user_id"]<<endl;
                 sec=time(NULL);
                 ctime(&sec);
                 cout << ctime(&sec)<<endl;
                 putText(GrayImage, result["result"]["user_list"][0]["user_id"].asString(), Point(25,25), FONT_HERSHEY_SIMPLEX, 1, Scalar(255,255,255));
                 putText(GrayImage,ctime(&sec), Point(25,50), FONT_HERSHEY_SIMPLEX, 1, Scalar(255,255,255)); 
                }
           }

      

        }
   imshow("video",GrayImage);
   waitKey(100);
      }
   return 0;
}


7、记录考勤时间
时间容器:time_sec

时间获取 (从1970.1.1 0:0:0到现在的秒数) time(NULL);

时间转换(转换为正常时间) ctime()
在这里插入图片描述
库:core

函数:putText (图像上写字)

void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )

第一个参数:要写字的图像

第二个参数:要写的字

第三个参数:字在图像上的坐标

第四个参数:图像的字体(具体看官方文档)

第五个参数:字的大小

第六个参数:字的颜色
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值