-
大小: 5KB文件类型: .rar金币: 1下载: 0 次发布日期: 2021-05-20
- 语言: C/C++
- 标签: CTensorFLow facenet
资源简介
c++简单调用 facenet模型,并对比欧式距离.使用了opencv做人脸检测.其中一些参数暂时写死了.目前的效果一般般.因为 opencv对人脸的不同姿态检测并不是太准.(暂时依赖于Qt的QString 和 QImage )
代码片段和文件信息
#include “TensorFlow.h“
CTensorFlow::CTensorFlow()
{
m_strxmlPath = “E:/SmartCity2015/AI/testTensorFlow/opencv/lib/haarcascade_frontalface_default.xml“;
}
CTensorFlow::~CTensorFlow()
{
}
bool CTensorFlow::Create()
{
// m_pLabel1 = pLabel1;
// m_pLabel2 = pLabel2;
Scope m_root = Scope::NewRootScope();
Status status = NewSession(SessionOptions() &m_pSession);//创建新会话Session
QString strModelPath = “E:/SmartCity2015/AI/testTensorFlow/x64/Debug/20170512-110547.pb“;
GraphDef graphdef; //Graph Definition for current model
Status status_load = ReadBinaryProto(Env::Default() strModelPath.toStdString() &graphdef); //从pb文件中读取图模型;
if (!status_load.ok()) {
std::cout << “ERROR: Loading model failed...“ << strModelPath.toStdString() << std::endl;
std::cout << status_load.ToString() << “\n“;
return false;
}
Status status_create = m_pSession->Create(graphdef); //将模型导入会话Session中;
if (!status_create.ok()) {
std::cout << “ERROR: Creating graph in session failed...“ << status_create.ToString() << std::endl;
m_bIsSessionOk = false;
return false;
}
cout << “Session successfully created.“ << endl;
m_bIsSessionOk = true;
return true;
}
void CTensorFlow::Destroy()
{
if (m_pSession)
{
m_pSession->Close();
delete m_pSession;
m_pSession = NULL;
}
m_bIsSessionOk = false;
}
int CTensorFlow::Run(const string& strPicture1 const string& strPicture2 float& fDistance vector& arrFaces1 vector& arrFaces2)
{
if (m_bIsSessionOk == false)
{
return -1;
}
m_strPicture1 = strPicture1;
m_strPicture2 = strPicture2;
int nHeight = 160;
int nWidth = 160;
m_nTotalRecognition++;
Tensor tensorPic1;
Tensor tensorPic2;
bool bRet = GetPicTensor(strPicture1 nHeight nWidth tensorPic1 arrFaces1);
if (bRet == false)
{
m_nRecognitionFail++;
return 0;
}
bRet = GetPicTensor(strPicture2 nHeight nWidth tensorPic2 arrFaces2);
if (bRet == false)
{
m_nRecognitionFail++;
return 0;
}
vector arrData1;
GetValueFromTensor(tensorPic1 arrData1);
vector arrData2;
GetValueFromTensor(tensorPic2 arrData2);
fDistance = Euclidean(arrData1 arrData2);
std::cout << “Euclidean fDistance: “ << fDistance << std::endl;
if (fDistance > m_fThreshold)
{
m_nMoreThenThreshold++;
}
return 1;
}
int CTensorFlow::Run(const unsigned char* pPictureData1 const unsigned char* pPictureData2 float& fDistance)
{
if (m_bIsSessionOk == false)
{
return -1;
}
int nHeight = 160;
int nWidth = 160;
Tensor tensorPicture1;
Tensor tensorPicture2;
bool bRet = GetPicTensor(pPictureData1 nHeight nWidth tensorPicture1);
if (bRet == false)
{
m_nRecognitionFail++;
return 0;
}
bRet = GetPicTensor(pPictureData2 nHeight nWidth tensorPicture2);
if (bRet == false)
{
m_nRecognitionFail++;
return 0;
}
vector arrData1;
GetValueFromTensor(tensorPicture1
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 17626 2018-07-20 15:44 TensorFlow.cpp
文件 3739 2018-07-20 15:40 TensorFlow.h
----------- --------- ---------- ----- ----
21365 2
评论
共有 条评论