这是该项目的最后一部分
九、json数据解析
如果我们打印返回的result,则会发现返回了cached,log_id等不必要的信息,我们需要将返回的json数据进行筛选和解析,并获取系统时间。该部分涉及json相关知识,在此不再赘述,直接贴上代码:
time_t sec;
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);
cout<<ctime(&sec)<<endl;
}
}
十、信息的记录和显示
最后,我们需要将信息显示到视频窗口上。我们使用OpenCV的putText方法,该方法的官方接口为:C++: void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )
参数信息为:
Parameters: |
|
---|
相关代码为:
putText(equalizeImg,result["result"]["user_list"][0]["user_id"].asString(),Point(0,50),FONT_HERSHEY_SIMPLEX,0.8,Scalar(255,255,255));
putText(equalizeImg,result["result"]["user_list"][0]["score"].asString().substr(0,5),Point(0,80),FONT_HERSHEY_SIMPLEX,0.7,Scalar(255,255,255));
putText(equalizeImg,ctime(&sec),Point(0,100),FONT_HERSHEY_SIMPLEX,0.6,Scalar(255,255,255));
至此,项目已经实现其基本功能,编译运行即可成功。
最终全部代码为:
#include <iostream>
#include "opencv2/opencv.hpp"
#include "face.h"
using namespace std;
using namespace cv;
using namespace aip;
int main()
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
{
cout<<"Camera open failed!"<<endl;
return -1;
}
cout<<"Camera open successfully!"<<endl;
// 设置APPID/AK/SK
std::string app_id = "31638174";
std::string api_key = "F4wQbQbG26VL3Ls9R0gRbk2n";
std::string secret_key = "fSrA01khOwEeg8rLo2yQ8AgfM8RNUM71";
aip::Face client(app_id, api_key, secret_key);
Mat img;
Mat grayImg;
Mat equalizeImg;
vector<Rect> faces;
Mat faceImg;
vector<uchar> jpgBuf;
std::string base64Img;
Json::Value result;
time_t sec;
CascadeClassifier classifier("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml");
for(;;)
{
cap >> img; // get a new img from camera
cvtColor(img, grayImg, CV_BGR2GRAY); //灰度处理
equalizeHist(grayImg,equalizeImg); //均衡化处理
classifier.detectMultiScale(equalizeImg, faces); //检测人脸并返回在facas中
if(faces.size())
{
rectangle(equalizeImg, faces[0], Scalar(255,255,255)); //绘制矩形框
faceImg = equalizeImg(faces[0]);
imencode(".jpg", faceImg, jpgBuf); //将图片编码为jpg格式后存到jpgBuf中
base64Img = base64_encode((char *)jpgBuf.data(), jpgBuf.size()); //格式转换为base64
result = client.search(base64Img, "BASE64", "Student", aip::null); //这里的"Student"是你之前新建的人脸分组的名称
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);
cout<<ctime(&sec)<<endl;
putText(equalizeImg,result["result"]["user_list"][0]["user_id"].asString(),Point(0,50),FONT_HERSHEY_SIMPLEX,0.8,Scalar(255,255,255));
putText(equalizeImg,result["result"]["user_list"][0]["score"].asString().substr(0,5),Point(0,80),FONT_HERSHEY_SIMPLEX,0.7,Scalar(255,255,255));
putText(equalizeImg,ctime(&sec),Point(0,100),FONT_HERSHEY_SIMPLEX,0.6,Scalar(255,255,255));
}
}
}
else
cout<<"No face detected!"<<endl;
imshow("video", equalizeImg); //在video窗口中展示图片
waitKey(40); //设置帧率(40ms读取一帧)
}
return 0;
}
感谢您的观看!