5月19日

在做图片的人脸识别检测将图片传入模型进行特征值提取,与数据库中的特征值进行比较,相关部分代码如下

#include "facework.h"
#include <string>
#include <QDateTime>
#include <QDebug>

using namespace cv;
using namespace std;
Facework::Facework(QObject *parent) : QObject(parent)
{
facedistinnet.load_param("/apps/VideoCaptureQt/data/mobilefacenet.param");
facedistinnet.load_model("/apps/VideoCaptureQt/data/mobilefacenet.bin");
ex = facedistinnet.create_extractor();
}

void Facework::runfaceone(QString sourcefacetext,cv::Mat &faceimage)
{
ex.set_light_mode(true);
QByteArray ba = sourcefacetext.toLatin1();
const char *tem_str = ba.data();
string tem_str2 = tem_str;
string tem_str3=tem_str2.substr(25);
const char *picname = tem_str3.c_str();//加const或等号右边用char*

cv::Mat m2 = cv::imread(tem_str, CV_LOAD_IMAGE_COLOR);


QDateTime start = QDateTime::currentDateTime();

ncnn::Mat feat1 = getFeatByMobileFaceNetNCNN(ex, faceimage);
ncnn::Mat feat2 = getFeatByMobileFaceNetNCNN(ex, m2);
float sim = calcSimilarity(feat1.channel(0), feat2.channel(0), 128);

QDateTime end1 = QDateTime::currentDateTime();
qint64 intervalTimeMS1 = start.msecsTo(end1);
cout<<"人脸识别time:"<<intervalTimeMS1<<endl;
facecount++;

//改变为置信度
cout<<"sim"<<sim<<endl;
if(sim>0.3&&facecount>=1){
facecount=0;
emit sendName(QString(picname));
}else if(sim<0.3&&facecount>=1){
facecount=0;
emit sendName(QString("not find"));
}
}

void Facework::stringToMat(QString str,ncnn::Mat& m)
{
QStringList list = str.split(",");
QString s;
float* ptr = m.channel(0);
for(int i = 0;i<list.length();i++)
{
s = list.at(i); //获取字符串集合的元素
ptr[i] = s.toFloat(); //mat赋值
}
}

void Facework::getZhixindu(int value)
{
this->zhixindu = value;
}

 

void Facework::runfaceall(Mat &faceimage)
{
ex.set_light_mode(true);

QDateTime start = QDateTime::currentDateTime();

ncnn::Mat feat1 = getFeatByMobileFaceNetNCNN(ex, faceimage);

ncnn::Mat feat2 = ncnn::Mat(128,1,1);

//数据库读取特征
helper.select("face");
allfeats = helper.selectFeats("face");
allnames = helper.selectNames("face");

float sim = 0;

for(int p = 0 ; p<allfeats.size();p++){
stringToMat(allfeats[p],feat2);
cout<<"分割"<<endl;
sim = calcSimilarity(feat1.channel(0), feat2.channel(0), 128);
qDebug()<<"sim"<<sim<<endl;

if(sim>=zhixindu*0.01){
facecount=0;
emit sendName(allnames[p]);
return;
}
}
if(sim<zhixindu)
{
facecount=0;
emit sendName(QString("not find"));
}

QDateTime end1 = QDateTime::currentDateTime();
qint64 intervalTimeMS1 = start.msecsTo(end1);
cout<<"人脸识别time:"<<intervalTimeMS1<<endl;
}

void Facework::runncnn(cv::Mat faceimage)
{
//单独
// runfaceone(QString("/apps/VideoCaptureQt/pic/3.jpg"),faceimage);

runfaceall(faceimage);
}

void Facework::getFeat(cv::Mat faceimage)
{
ncnn::Mat feat = getFeatByMobileFaceNetNCNN(ex, faceimage);
emit sendFeat(feat);
}

posted @ 2022-05-19 23:14  不咬牙  阅读(14)  评论(0编辑  收藏  举报