- g++ main.cpp -o main -lopencv_highgui -l opencv_core -lopencv_imgproc -lopencv_objdetect -std=c++11 -lcurl -lcrypto -ljsoncpp, 运行命令
- ./main >>log.txt输出重定向
3.源码
#include<iostream>
#include "opencv2/opencv.hpp"
#include"face.h"
using namespace std;
using namespace cv;
using namespace aip;
int main()
{
VideoCapture cap(0); //open camera the 1th
if(!cap.isOpened()) // check if we succeeded
{
cout<<"Camera open failed"<<endl;
return 0;
}
cout<<"Camera open succed"<<endl;
CascadeClassifier Classifier("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml");
std::string app_id = "xxx";
std::string api_key = "xxx";
std::string secret_key = "xxxxxxx";
aip::Face client(app_id, api_key, secret_key);
Mat ColorImage;//shen qing rong qi,save photo
Mat GrayImage;
vector<Rect> AllFace;
Mat MatFace;
vector<uchar> JpgFace;
string Base64Face;
Json::Value result;
time_t sec;
for(;;)
{
cap >> ColorImage; // get a new photo from camera
cvtColor(ColorImage, GrayImage, CV_BGR2GRAY);//gray chu li
equalizeHist(GrayImage, GrayImage);//more qingxi
Classifier.detectMultiScale(GrayImage, AllFace);//quan qi lai
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.search(Base64Face, "BASE64", "Teaching", aip::null);
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(GrayImage, result["result"]["user_list"][0]["user_id"].asString(), Point(0,50), FONT_HERSHEY_SIMPLEX, 1,Scalar(255,255,255));
putText(GrayImage, ctime(&sec), Point(0,100), FONT_HERSHEY_SIMPLEX, 1,Scalar(255,255,255));
}
}
}
imshow("video",GrayImage);
waitKey(40);
}
return 0;
}