#include "FaceDetectServerI.h"
|
#include <basic/util/app/AppPreference.hpp>
|
#include <QtCore/QString>
|
#include <QtCore/QSharedMemory>
|
#include <basic/timer_counter/Clocktimer.h>
|
#include <THFaceImage_i.h>
|
#include <THFaceProperty_i.h>
|
|
FaceDetectServerI::FaceDetectServerI() {
|
long threadMax = appPref.getLongData("thread.max");
|
long gpuIndex = appPref.getLongData("gpu.index");
|
for (int i = 0; i < threadMax; i++) {
|
detectResourcesManager.pushResource(i);
|
}
|
for (int i = 0; i < threadMax; i++) {
|
propertyResourcesManager.pushResource(i);
|
}
|
if (gpuIndex < 0) {
|
THFI_Param *param = new THFI_Param[threadMax];
|
THFI_Create(threadMax, param);
|
delete[] param;
|
} else {
|
THFI_Param_Ex *param = new THFI_Param_Ex[threadMax];
|
THFI_Param detParam;
|
detParam.nMinFaceSize = 20;
|
detParam.nRollAngle = 60;
|
for (int i = 0; i < threadMax; i++) {
|
param[i].tp = detParam;
|
param[i].nDeviceID = gpuIndex;
|
}
|
THFI_Create_Ex(threadMax, param);
|
delete[] param;
|
}
|
THFP_Create(threadMax);
|
m_ampleSize =
|
appPref.getIntData("FaceDetectionSampleSize") > 0 ? appPref.getIntData("FaceDetectionSampleSize") : 640;
|
}
|
|
FaceDetectServerI::~FaceDetectServerI() {
|
THFI_Release();
|
THFP_Release();
|
}
|
|
FaceDetect::Faces FaceDetectServerI::faceDetect(Ice::Int width, Ice::Int height, const std::string &shareMemoryName,
|
const Ice::Current &) {
|
ClockTimer ct("FaceDetectServerI::faceDetect");
|
FaceDetect::Faces faces;
|
QSharedMemory shareMemory(QString(shareMemoryName.c_str()));
|
if (shareMemory.attach()) {
|
auto data = shareMemory.constData();
|
THFI_FacePos facesPos[30];
|
int channel = detectResourcesManager.getAvilableChannel(shareMemoryName);
|
int faceNum = THFI_DetectFace(channel, (BYTE *) data, 24, width, height, facesPos, 30, m_ampleSize);
|
if (faceNum > 0) {
|
for (int i = 0; i < faceNum; i++) {
|
FaceDetect::FacePos face;
|
auto &pos = facesPos[i];
|
memcpy(&face, &pos, sizeof(pos) - sizeof(pos.pFacialData));
|
face.pFacialData.resize(sizeof(pos.pFacialData));
|
memcpy(face.pFacialData.data(), pos.pFacialData, sizeof(pos.pFacialData));
|
// DBG(face.fAngle.confidence);
|
faces.push_back(face);
|
}
|
} else {
|
DBG("Face num is 0");
|
}
|
} else {
|
ERR("shareMemory error " << shareMemoryName);
|
}
|
return faces;
|
}
|
|
FaceDetect::ThftResult FaceDetectServerI::faceProperty(Ice::Int width, Ice::Int height, const FaceDetect::FacePos &pos,
|
const std::string &shareMemoryName, const Ice::Current &) {
|
QSharedMemory shareMemory(QString(shareMemoryName.c_str()));
|
if (shareMemory.attach()) {
|
FaceDetect::ThftResult thftResult;
|
THFP_Result_V2 propertys;
|
auto data = shareMemory.constData();
|
THFI_FacePos facesPos;
|
int channel = propertyResourcesManager.getAvilableChannel(shareMemoryName);
|
memcpy(&facesPos, &pos, sizeof(facesPos) - sizeof(facesPos.pFacialData));
|
memcpy(facesPos.pFacialData, pos.pFacialData.data(), sizeof(facesPos.pFacialData));
|
int ret = THFP_Execute_V2(channel, (BYTE *) data, width, height, &facesPos, &propertys);
|
if (ret == 0)memcpy(&thftResult, &propertys, sizeof(propertys));
|
else {
|
ERR("THFP_Execute_V2 faild, returns " << ret)
|
}
|
return thftResult;
|
} else {
|
throw std::runtime_error("shareMemory attach faild");
|
}
|
}
|