天天看點

win7 x64 vs2015 c++ 使用 tensorflow - 2 使用 facenet 模型

    簡單調用 facenet模型,并對比歐式距離.使用了opencv做人臉檢測.其中一些參數暫時寫死了.目前的效果一般.因為 opencv對人臉的不同姿态檢測并不是太準.(暫時依賴于Qt的QString 和 QImage )

1/ create:

    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;

2 對比兩個圖檔的距離.1.06 作為分界線. <1.06為同一個人臉.

int CTensorFlow::Run(const string& strPicture1, const string& strPicture2, float& fDistance, vector<RectInfo>& arrFaces1, vector<RectInfo>& 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<float> arrData1;

    GetValueFromTensor(tensorPic1, arrData1);

    vector<float> arrData2;

    GetValueFromTensor(tensorPic2, arrData2);

    fDistance = Euclidean(arrData1, arrData2);

    std::cout << "Euclidean fDistance: " << fDistance << std::endl;

    if (fDistance > m_fThreshold)

    {

        m_nMoreThenThreshold++;

    }

    return 1;

}

完整代碼:

   https://download.csdn.net/download/zsyddl2/10563498

繼續閱讀