以下使用了百度云服务
#include <iostream> // C++头文件
#include <pthread.h> //线程头文件
#include "opencv2/opencv.hpp" // opencv 头文件
#include "face.h" // 百度SDK头文件
using namespace std; // c++ 命名空间
using namespace cv; // opencv 命名空间
using namespace aip; // 百度sdk 命名空间
/*int main() // 主函数
{
Mat ColorImage; // 存储原始图像
Mat GrayImage; // 存储灰度图像
Mat OutImage; // 存储均衡化处理完的图像
Mat Image;
vector<Rect> AllFace;
vector<uchar> JpgFace;
string Base64Img;
Json::Value result;
time_t sec;
FILE * fp=NULL;
fp = fopen("./data.txt","a+");
// 第一步:打开摄像头,存储照片
VideoCapture Camera(0); // open the default camera
if (!Camera.isOpened()) // 判断摄像头是否打开
{ // 打开失败
cout << "Camera is failed!" << endl;
return 0;
}
cout << "Camera is success!!!" << endl;
CascadeClassifier Classifier("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml"); // 级联分类器
// 设置APPID/AK/SK
// 改成自己的
std::string app_id = "";
std::string api_key = "";
std::string secret_key = "";
aip::Face client(app_id, api_key, secret_key);
//aip::Face client("");
for (;;)
{
Camera >> ColorImage;// get a new ColorImage from camera
//Camera.read(Image);
//imencode(".jpg", Image, JpgImage);
//imwrite("./123.jpg", Image);
//line(ColorImage, Point(200,200), Point(200,100), Scalar(0,0,0),1);
//rectangle(ColorImage, Point(0,0), Point(100,100), CV_RGB(0,0,0),1);
//circle(ColorImage, Point(300,300), 100, CV_RGB(0,0,0),1);
//ellipse(ColorImage, Point(200,200), Size(50,100), 90, 0, 360, CV_RGB(0,0,0),1);
//putText(ColorImage, "hello word", Point(100,100), FONT_HERSHEY_SIMPLEX, 1, Scalar(0,0,255),1);
// 图像处理 非必要
cvtColor(ColorImage, GrayImage, CV_BGR2GRAY); // 把BGR图像转成灰度图
equalizeHist(GrayImage, OutImage); // 对灰度图进行均衡化处理
Classifier.detectMultiScale(OutImage, AllFace); // 检测输入图像中不同大小的对象。检测到的对象将以矩形列表的形式返回。
if (AllFace.size()) // 判断矩形列表的数据数 检测到脸才会执行下面代码
{
// rectangle(OutImage, AllFace[0], Scalar(255, 255, 255));
rectangle(ColorImage, AllFace[0], Scalar(255, 255, 255)); // 在显示的图像上把人脸绘制出来(矩形框)
Image = OutImage(AllFace[0]); // BGR 把检测出来的人脸从图像上截取出来
imencode(".jpg", Image, JpgFace); // BGR 图像转成JPG格式
Base64Img = base64_encode((char *)JpgFace.data(), JpgFace.size()); // 把jpg图像转成base64位
result = client.search(Base64Img, "BASE64", "me", aip::null); // 把人脸图像发送给百度云平台,并返回结果
// 对百度智能云平台返回结果的处理
if (!result["result"].isNull()) // 当返回信息不为空
{
if (result["result"]["user_list"][0]["score"].asInt() > 70) // 0-100 判断匹配度是否大于70
{
putText(ColorImage,
result["result"]["user_list"][0]["user_id"].asString(),
Point(AllFace[0].x, AllFace[0].y + AllFace[0].height + 30),
FONT_HERSHEY_SCRIPT_SIMPLEX, 1, CV_RGB(255, 255, 0)); // 在图像上输出文本信息
sec = time(NULL); // 时间 秒 距离1970年0:0:0 多少秒
//cout << result["result"]["user_list"][0]["user_id"].asString() << endl;
//cout << ctime(&sec) << endl; // 以时间戳格式输出日期时间
putText(ColorImage,
ctime(&sec),
Point(AllFace[0].x + 100, AllFace[0].y + AllFace[0].height + 30),
FONT_HERSHEY_SCRIPT_SIMPLEX, 1, CV_RGB(255, 255, 0));
fwrite(result["result"]["user_list"][0]["user_id"].asString().data(),
result["result"]["user_list"][0]["user_id"].asString().size(),
1,fp);
fwrite("\n",1,1,fp);
fwrite(ctime(&sec),strlen(ctime(&sec)),1,fp);
fflush(fp);
}
}
}
imshow("测试专用", ColorImage); // 在窗口显示图像
waitKey(30); // 等待一个按下的键。
}
fclose(fp);
return 0;
}
*/
//线程优化:
#include <iostream>
#include <pthread.h>
#include "opencv2/opencv.hpp"
#include "face.h"
using namespace std;
using namespace cv;
using namespace aip;
int detect_flag = 0,display_flag = 0;
int cnt = 0;
aip::Face client(“改成自己的”);
Mat ColorImage,DetectImage;
Mat GrayImage;
Mat OutImage;
Mat Image;
vector<Rect> AllFace;
vector<uchar> JpgFace;
string Base64Img;
Json::Value result;
time_t sec;
FILE * fp=NULL;
void * thread_display(void * arg)
{
VideoCapture Cap(0);
if( !Cap.isOpened() )
{
cout << "Cap is failed!" << endl;
return 0;
}
cout << "Cap is sucess!!!!!" << endl;
while(1)
{
Cap.read(ColorImage);
if(detect_flag == 0)
{
Cap.read(DetectImage);
detect_flag = 1;
}
if(display_flag == 1)
{
rectangle(ColorImage, AllFace[0], Scalar(255, 255, 255));
putText(ColorImage,result["result"]["user_list"][0]["user_id"].asString(), Point(AllFace[0].x,AllFace[0].y+AllFace[0].height+30),FONT_HERSHEY_PLAIN,5, CV_RGB(0,0,0));
sec = time(NULL);
cout << result["result"]["user_list"][0]["user_id"].asString() << endl;
cout << ctime(&sec) << endl;
putText(ColorImage,ctime(&sec), Point(AllFace[0].x+100,AllFace[0].y+AllFace[0].height+30),FONT_HERSHEY_PLAIN,1, CV_RGB(255,255,0));
fwrite("\n",1,1,fp);
fwrite(ctime(&sec),strlen(ctime(&sec)),1,fp);
fwrite("\n",1,1,fp);
fwrite(result["result"]["user_list"][0]["user_id"].asString().data(),result["result"]["user_list"][0]["user_id"].asString().size(),1,fp);
fflush(fp);
cnt ++;
if(cnt == 25)
{
display_flag = 0;
cnt = 0;
}
}
imshow("测试专用",ColorImage);
waitKey(1000/25);
}
pthread_exit(NULL);
}
void * thread_detect(void * arg)
{
CascadeClassifier FaceDetect;
FaceDetect.load( "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml");
while(1)
{
if(detect_flag == 1)
{
cvtColor(DetectImage, GrayImage, CV_BGR2GRAY);
equalizeHist(GrayImage, OutImage);
FaceDetect.detectMultiScale(GrayImage,AllFace);
if( AllFace.size() )
{
display_flag = 1;
Image = OutImage(AllFace[0]);
imencode(".jpg", Image, JpgFace);
Base64Img = base64_encode((char *)JpgFace.data(), JpgFace.size());
result = client.search(Base64Img,"BASE64","me", aip::null);
//cout << result << endl;
//cout << result["result"]["user_list"][0]["user_id"].asString() << endl;
if( !result["result"].isNull() )
{
if( result["result"]["user_list"][0]["score"].asInt() >70)
{
putText(ColorImage,result["result"]["user_list"][0]["user_id"].asString(), Point(AllFace[0].x,AllFace[0].y+AllFace[0].height+30),FONT_HERSHEY_PLAIN,5, CV_RGB(0,0,0));
sec = time(NULL);
cout << result["result"]["user_list"][0]["user_id"].asString() << endl;
cout << ctime(&sec) << endl;
putText(ColorImage,ctime(&sec), Point(AllFace[0].x+100,AllFace[0].y+AllFace[0].height+30),FONT_HERSHEY_PLAIN,1, CV_RGB(255,0,0));
fwrite("\n",1,1,fp);
fwrite(ctime(&sec),strlen(ctime(&sec)),1,fp);
fflush(fp);
}
}
}
detect_flag = 0;
}
}
pthread_exit(NULL);
}
int main()
{
fp = fopen("./data.txt","a+");
pthread_t thread_display_id,thread_detect_id;
pthread_create(&thread_display_id, NULL, thread_display, NULL);
pthread_create(&thread_detect_id, NULL, thread_detect, NULL);
pthread_join(thread_display_id, NULL);
pthread_join(thread_detect_id, NULL);
return 0;
}
关于opencv人脸识别构想代码
最新推荐文章于 2024-05-13 04:14:06 发布