代码拉取完成,页面将自动刷新
千搜科技人脸识别-linux平台人脸识别Demo
SDK依赖opencv3.3
0x0000000000000001 (NEEDED) 共享库:[libopencv_highgui.so.3.3]
0x0000000000000001 (NEEDED) 共享库:[libopencv_videoio.so.3.3]
0x0000000000000001 (NEEDED) 共享库:[libopencv_imgcodecs.so.3.3]
0x0000000000000001 (NEEDED) 共享库:[libopencv_imgproc.so.3.3]
0x0000000000000001 (NEEDED) 共享库:[libopencv_core.so.3.3]
#include <iostream>
#include <string>
#include <array>
#include <map>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include "WisFaceEngineWrap.h"
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
typedef struct{
QsFace face;
std::string filename;
float score;
} FaceObj;
void getFiles(const char* path, vector<string>& files);
FaceObj queryFace(QsHANDLE engine, std::vector<FaceObj> &faceObjs, QsFace &face);
int main(int argc, char* argv[])
{
QsHANDLE engine = qs_Wis_Create();
if (!engine)
{
printf("create face engine failed.\n");
return 0;
}
printf("initial face engine success\r\n");
float threshold = 0.7;
//注册人脸照片
std::vector<FaceObj> faceObjs;
std::vector<std::string> GalleryImageFilename;
string folder = "./faces/";
getFiles(folder.c_str(), GalleryImageFilename);
int facenum = 0;
int ret = 0;
for( size_t i = 0; i < GalleryImageFilename.size(); ++i )
{
//register face into facedatabase
std::string &filename = GalleryImageFilename[i];
std::cerr << "Registering... " << filename << std::endl;
cv::Mat image = cv::imread( filename );
FaceObj obj;
facenum = qs_Wis_DetectFaces(engine, image.data, image.cols, image.rows,image.step.p[0], &obj.face, 1);
if(facenum < 1){
continue;
}
ret = qs_Wis_ExtractFeature(engine, image.data, image.cols, image.rows,image.step.p[0], &obj.face);
if (ret != 0) {
continue;
}
obj.filename = filename;
faceObjs.push_back(obj);
}
std::cout << "----open camera----" << std::endl;
// Open default USB camera
cv::VideoCapture capture;
capture.open(0);
cv::Mat frame;
while( capture.isOpened() ){
capture >> frame;
if( frame.empty() ) continue;
QsFace face1;
int facenum1 = qs_Wis_DetectFaces(engine, frame.data, frame.cols, frame.rows, frame.step.p[0], &face1, 1);
if (facenum1 > 0)
{
printf("facenum=%d,{left=%d,top=%d,right=%d,bottom=%d}\n",
facenum1, face1.rect.left, face1.rect.top, face1.rect.right, face1.rect.bottom);
cv::Rect facerect( face1.rect.left, face1.rect.top, face1.rect.right - face1.rect.left, face1.rect.bottom- face1.rect.top );
cv::rectangle( frame, facerect, CV_RGB( 128, 128, 255 ), 3 );
int tag = qs_Wis_ExtractFeature(engine, frame.data, frame.cols, frame.rows, frame.step.p[0], &face1);
if (tag == 0) {
printf("Extract face feature success.\n");
}else {
continue;
}
//人脸搜索
FaceObj ret = queryFace(engine, faceObjs, face1);
if (ret.score > threshold) {
cv::putText( frame, ret.filename, cv::Point( face1.rect.left, face1.rect.top - 5 ), 3, 1, CV_RGB( 255, 128, 128 ) );
}
}
cv::imshow( "Frame", frame );
auto key = cv::waitKey( 20 );
if( key == 27 )
{
break;
}
}
return 0;
}
FaceObj queryFace(QsHANDLE engine, std::vector<FaceObj> &faceObjs, QsFace &face)
{
float score = 0;
FaceObj res;
for( FaceObj &fo : faceObjs ){
float similar = qs_Wis_Compare2Feature(engine, fo.face.feature, face.feature);
if (similar > score) {
score = similar;
res = fo;
res.score = score;
}
}
return res;
}
void getFiles(const char* path, vector<string>& files){
const string path0 = path;
DIR* pDir;
struct dirent* ptr;
struct stat s;
lstat(path, &s);
if(!S_ISDIR(s.st_mode)){
cout << "not a valid directory: " << path << endl;
return;
}
if(!(pDir = opendir(path))){
cout << "opendir error: " << path << endl;
return;
}
int i = 0;
string subFile;
while((ptr = readdir(pDir)) != 0){
subFile = ptr -> d_name;
if(subFile == "." || subFile == "..")
continue;
subFile = path0 + subFile;
cout << ++i << ": " << subFile << endl;
files.push_back(subFile);
}
closedir(pDir);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。