namespace InferenceEngineImpl {
namespace OpenCVImpl {
-InferenceOpenCV::InferenceOpenCV(std::string protoFile, std::string weightFile,
- std::string userFile) :
+InferenceOpenCV::InferenceOpenCV(std::string protoFile, std::string weightFile) :
mNet(),
mConfigFile(protoFile),
mWeightFile(weightFile),
- mUserFile(userFile)
+ mInputData(cv::Mat())
{
LOGE("ENTER");
LOGE("LEAVE");
;
}
-int InferenceOpenCV::SetUserFile()
-{
- std::ifstream fp(mUserFile.c_str());
- if (!fp.is_open()) {
- return INFERENCE_ENGINE_ERROR_INVALID_PATH;
- }
-
- std::string userListName;
- while (!fp.eof()) {
- std::getline(fp, userListName);
- if (userListName.length())
- SetUserListName(userListName);
- }
-
- fp.close();
-
- return INFERENCE_ENGINE_ERROR_NONE;
-}
-
int InferenceOpenCV::SetInputTensorParam()
{
LOGE("Not supported");
return INFERENCE_ENGINE_ERROR_NOT_SUPPORTED;
}
-int InferenceOpenCV::SetInputTensorParamInput(int width, int height, int dim, int ch)
-{
- mCh = ch;
- mDim = dim;
- mInputSize = cv::Size(width, height);
-
- LOGE("InputSize is %d x %d\n", mInputSize.width, mInputSize.height);
-
- return INFERENCE_ENGINE_ERROR_NONE;
-}
-
-int InferenceOpenCV::SetInputTensorParamNorm(double deviation, double mean)
-{
- mDeviation = deviation;
- mMean = mean;
-
- return INFERENCE_ENGINE_ERROR_NONE;
-}
-
int InferenceOpenCV::SetInputTensorParamNode(std::string node)
{
mInputLayer = cv::String(node);
return INFERENCE_ENGINE_ERROR_NOT_SUPPORTED;
}
-int InferenceOpenCV::SetOutputTensorParamThresHold(double threshold)
-{
- mThreshold = threshold;
-
- return INFERENCE_ENGINE_ERROR_NONE;
-}
-
-int InferenceOpenCV::SetOutputTensorParamNumbers(int number)
-{
- mOutputNumbers = number;
-
- return INFERENCE_ENGINE_ERROR_NONE;
-}
-
-int InferenceOpenCV::SetOutputTensorParamType(int type)
-{
- return INFERENCE_ENGINE_ERROR_NONE;
-}
-
int InferenceOpenCV::SetOutputTensorParamNodes(std::vector<std::string> nodes)
{
mOutputLayer.clear();
return INFERENCE_ENGINE_ERROR_INVALID_PATH;
}
- size_t userFileLength = mUserFile.length();
- if (userFileLength > 0 && access(mUserFile.c_str(), F_OK)) {
- LOGE("userFilePath in [%s] ", mUserFile.c_str());
- return INFERENCE_ENGINE_ERROR_INVALID_PATH;
- }
-
// This call may be changed if OpenCV version would be upgraded
int nPos = mWeightFile.find_last_of(".");
std::string weightFileExt = mWeightFile.substr(nPos+1);
return INFERENCE_ENGINE_ERROR_INVALID_DATA;
}
- ret = (userFileLength > 0) ? SetUserFile() : INFERENCE_ENGINE_ERROR_NONE;
- if (ret != INFERENCE_ENGINE_ERROR_NONE)
- LOGE("Fail to read categoryList");
-
return ret;
}
return INFERENCE_ENGINE_ERROR_NONE;
}
-int InferenceOpenCV::PrepareInputLayerPassage(inference_input_type_e type)
-{
- /* Do Nothing
- * In OpenCV, don't need to call this PrepareInputLayerPassage()
- * because that it can uses cv::Mat directly
- */
- return INFERENCE_ENGINE_ERROR_NONE;
-}
-int InferenceOpenCV::Run(cv::Mat tensor)
+int InferenceOpenCV::GetInputLayerAttrType()
{
- double scaleVal = 1.0/mDeviation;
- mSourceSize = tensor.size();
- if (mInputSize == cv::Size(-1,-1)) {
- mInputSize = mSourceSize;
- }
-
- cv::Scalar meanScalar = cv::Scalar((float)mMean,(float)mMean, (float)mMean);
-
- mInputBlob = cv::dnn::blobFromImage(tensor, scaleVal, mInputSize, meanScalar, false, false);
-
- if (mInputBlob.empty()) {
- LOGE("Fail to get input blob");
- return INFERENCE_ENGINE_ERROR_INTERNAL;
- }
-
- mNet.setInput(mInputBlob, mInputLayer);
-
- /* foward */
- mNet.forward(mOutputProb, mOutputLayer);
-
- if (mOutputProb.empty()) {
- LOGE("OutputProb is empty");
- return INFERENCE_ENGINE_ERROR_INTERNAL;
- }
-
- return INFERENCE_ENGINE_ERROR_NONE;
+ return 1;
}
-int InferenceOpenCV::Run(std::vector<float> tensor)
+void * InferenceOpenCV::GetInputDataPtr()
{
- return INFERENCE_ENGINE_ERROR_NONE;
+ return static_cast<void*>(mInputData.data);
}
-int InferenceOpenCV::GetInferenceResult(ImageClassificationResults& results)
+int InferenceOpenCV::SetInputDataBuffer(tensor_t data)
{
- int dims = mOutputProb[0].dims;
- for (int k = 0; k < dims; ++k) {
- LOGE("%d: %d", k, mOutputProb[0].size[k]);
- }
- LOGI("dims: %d", dims);
- cv::Mat reShapedProb = mOutputProb[0].reshape(1,1);
-
-
- int classIdx = -1;
- double classProb = 0.0;
-
- cv::Mat outputIdx, outputProb;
-
- cv::sortIdx(reShapedProb, outputIdx, CV_SORT_EVERY_ROW + CV_SORT_DESCENDING);
-
- for (int idx = 0; idx < mOutputNumbers; ++idx) {
- classIdx = outputIdx.at<int>(0,idx);
- int y = classIdx / reShapedProb.cols;
- int x = classIdx % reShapedProb.cols;
-
- classProb = (double)reShapedProb.at<float>(y,x);
- results.indices.push_back(classIdx);
- results.confidences.push_back(classProb);
- LOGI("classIdx: %d", classIdx);
- LOGI("classProb: %lf", classProb);
- LOGI("test....");
- LOGI("names: %s", mUserListName[classIdx].c_str());
- results.names.push_back(mUserListName[classIdx]);
-
- }
-
- results.number_of_classes = mOutputNumbers;
-
+ mInputData = cv::Mat(data.dimInfo[0][2], data.dimInfo[0][3], data.dimInfo[0][1]);
return INFERENCE_ENGINE_ERROR_NONE;
}
-int InferenceOpenCV::GetInferenceResult(ObjectDetectionResults& results)
+int InferenceOpenCV::Run()
{
- int dims = mOutputProb[0].dims;
- for (int k = 0; k < dims; ++k) {
- LOGE("%d: %d", k, mOutputProb[0].size[k]);
- }
- LOGI("dims: %d", dims);
- cv::Mat detectionMat(mOutputProb[0].size[2], mOutputProb[0].size[3], CV_32F, mOutputProb[0].ptr<float>());
-
- float confidence;
- size_t objectClass;
- int left, top, right, bottom;
- cv::Rect loc;
- for(int i = 0; i < detectionMat.rows; i++) {
- confidence = detectionMat.at<float>(i, 2);
-
- if(confidence > (float)mThreshold) {
- objectClass = (size_t)(detectionMat.at<float>(i, 1));
-
- left = static_cast<int>(detectionMat.at<float>(i, 3) * mSourceSize.width);
- top = static_cast<int>(detectionMat.at<float>(i, 4) * mSourceSize.height);
- right = static_cast<int>(detectionMat.at<float>(i, 5) * mSourceSize.width);
- bottom = static_cast<int>(detectionMat.at<float>(i, 6) * mSourceSize.height);
-
- loc.x = left;
- loc.y = top;
- loc.width = right - left + 1;
- loc.height = bottom - top + 1;
-
- //classNames[objectClass];
- LOGE("objectClass: %d", objectClass);
- LOGE("confidence:%f", confidence);
- LOGE("left:%d, top:%d, right:%d, bottom:%d", left, top, right, bottom);
-
- results.indices.push_back(objectClass);
- results.confidences.push_back(confidence);
- results.names.push_back(mUserListName[objectClass]);
- results.locations.push_back(loc);
- }
- }
-
- results.number_of_objects = results.indices.size();
-
- return INFERENCE_ENGINE_ERROR_NONE;
-}
+ mInputBlob = cv::dnn::blobFromImage(mInputData, 1.0, cv::Size(), cv::Scalar(), false, false);
-int InferenceOpenCV::GetInferenceResult(FaceDetectionResults& results)
-{
- int dims = mOutputProb[0].dims;
- for (int k = 0; k < dims; ++k) {
- LOGE("%d: %d", k, mOutputProb[0].size[k]);
- }
- LOGI("dims: %d", dims);
-
- cv::Mat detectionMat(mOutputProb[0].size[2], mOutputProb[0].size[3], CV_32F, mOutputProb[0].ptr<float>());
-
- float confidence;
- int left, top, right, bottom;
- cv::Rect loc;
- for(int i = 0; i < detectionMat.rows; i++) {
- confidence = detectionMat.at<float>(i, 2);
-
- if(confidence > (float)mThreshold) {
- left = static_cast<int>(detectionMat.at<float>(i, 3) * mSourceSize.width);
- top = static_cast<int>(detectionMat.at<float>(i, 4) * mSourceSize.height);
- right = static_cast<int>(detectionMat.at<float>(i, 5) * mSourceSize.width);
- bottom = static_cast<int>(detectionMat.at<float>(i, 6) * mSourceSize.height);
-
- loc.x = left;
- loc.y = top;
- loc.width = right - left + 1;
- loc.height = bottom - top + 1;
+ mNet.setInput(mInputBlob, mInputLayer);
- //classNames[objectClass];
- LOGE("confidence:%f", confidence);
- LOGE("left:%d, top:%d, right:%d, bottom:%d", left, top, right, bottom);
+ mNet.forward(mOutputProb, mOutputLayer);
- results.confidences.push_back(confidence);
- results.locations.push_back(loc);
- }
+ if (mOutputProb.empty()) {
+ LOGE("OutputProb is empty");
+ return INFERENCE_ENGINE_ERROR_INTERNAL;
}
- results.number_of_faces = results.locations.size();
-
return INFERENCE_ENGINE_ERROR_NONE;
}
-int InferenceOpenCV::GetInferenceResult(FacialLandMarkDetectionResults& results)
+int InferenceOpenCV::Run(std::vector<float> tensor)
{
- int dims = mOutputProb[0].dims;
- for (int k = 0; k < dims; ++k) {
- LOGE("%d: %d", k, mOutputProb[0].size[k]);
- }
- LOGI("dims: %d", dims);
-
- cv::Mat outputs = mOutputProb[0].reshape(1,1);
-
- int number_of_landmarks = (outputs.cols >> 1);
-
- int pos = 0;
- cv::Point pint;
- for (int idx = 0; idx < number_of_landmarks; ++idx) {
- pos = (idx << 1);
- pint.x = static_cast<int>((outputs.at<float>(0, pos) + 0.5) * mSourceSize.width);
- pint.y = static_cast<int>((outputs.at<float>(0, pos+1) + 0.5) * mSourceSize.height);
-
- results.locations.push_back(pint);
- LOGI("x:%d, y:%d", pint.x, pint.y);
- }
-
- results.number_of_landmarks = number_of_landmarks;
-
return INFERENCE_ENGINE_ERROR_NONE;
}
-int InferenceOpenCV::GetInferenceResult(std::vector<std::vector<int>>& dimInfo, std::vector<float*>& results)
+int InferenceOpenCV::GetInferenceResult(tensor_t& results)
{
- dimInfo.clear();
- results.clear();
std::vector<int> tmpDimInfo;
LOGE("outputProb size: %d", mOutputProb.size());
for (std::vector<cv::Mat>::iterator iter = mOutputProb.begin();
tmpDimInfo.push_back((*iter).size[d]);
}
- dimInfo.push_back(tmpDimInfo);
- results.push_back((*iter).ptr<float>());
+ results.dimInfo.push_back(tmpDimInfo);
+ results.data.push_back((void*)(*iter).ptr<float>());
}
return INFERENCE_ENGINE_ERROR_NONE;
}
-int InferenceOpenCV::GetNumberOfOutputs()
-{
- return mOutputNumbers;
-}
-
-void InferenceOpenCV::SetUserListName(std::string userlist)
-{
- mUserListName.push_back(userlist);
-}
-
extern "C"
{
-class IInferenceEngineVision* EngineVisionInit(std::string protoFile, std::string weightFile,
- std::string userFile)
-{
- InferenceOpenCV *engine = new InferenceOpenCV(protoFile, weightFile, userFile);
- return engine;
-}
-
-void EngineVisionDestroy(class IInferenceEngineVision *engine)
-{
- delete engine;
-}
-
-class IInferenceEngineCommon* EngineCommonInit(std::string protoFile, std::string weightFile,
- std::string userFile)
+class IInferenceEngineCommon* EngineCommonInit(std::string protoFile, std::string weightFile)
{
- InferenceOpenCV *engine = new InferenceOpenCV(protoFile, weightFile, userFile);
+ InferenceOpenCV *engine = new InferenceOpenCV(protoFile, weightFile);
return engine;
}
#ifndef __INFERENCE_ENGINE_IMPL_OPENCV_H__
#define __INFERENCE_ENGINE_IMPL_OPENCV_H__
-#include <inference_engine_vision.h>
+#include <inference_engine_common.h>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#define LOG_TAG "INFERENCE_ENGINE_OPENCV"
-using namespace InferenceEngineInterface::Vision;
using namespace InferenceEngineInterface::Common;
namespace InferenceEngineImpl {
namespace OpenCVImpl {
-class InferenceOpenCV : public IInferenceEngineVision {
+class InferenceOpenCV : public IInferenceEngineCommon {
public:
InferenceOpenCV(std::string protoFile,
- std::string weightFile,
- std::string userFile);
+ std::string weightFile);
~InferenceOpenCV();
int SetInputTensorParamNode(std::string node = "input") override;
- int SetInputTensorParamInput(int width, int height, int dim, int ch) override;
-
- int SetInputTensorParamNorm(double deviation = 1.0, double mean = 0.0) override;
-
// Output Tensor Params
int SetOutputTensorParam() override;
- int SetOutputTensorParamThresHold(double threshold) override;
-
- int SetOutputTensorParamNumbers(int number) override;
-
- int SetOutputTensorParamType(int type) override;
-
int SetOutputTensorParamNodes(std::vector<std::string> nodes) override;
int SetTargetDevice(inference_target_type_e type) override;
int CreateInputLayerPassage() override;
- int PrepareInputLayerPassage(inference_input_type_e type) override;
-
- int Run(cv::Mat tensor) override;
-
- int Run(std::vector<float> tensor) override;
-
- int GetInferenceResult(ImageClassificationResults& results) override;
+ int GetInputLayerAttrType() override;
- int GetInferenceResult(ObjectDetectionResults& results) override;
+ void * GetInputDataPtr() override;
- int GetInferenceResult(FaceDetectionResults& results) override;
+ int SetInputDataBuffer(tensor_t data) override;
- int GetInferenceResult(FacialLandMarkDetectionResults& results) override;
+ int Run() override;
- int GetInferenceResult(std::vector<std::vector<int>>& dimInfo, std::vector<float*>& results);
-
- int GetNumberOfOutputs() override;
+ int Run(std::vector<float> tensor) override;
- void SetUserListName(std::string userList) override;
+ int GetInferenceResult(tensor_t& data) override;
-public:
- int SetUserFile();
- int setInput(cv::Mat cvImg);
-
private:
cv::Mat mInputBlob;
std::vector<cv::Mat> mOutputProb;
cv::String mInputLayer;
std::vector<cv::String> mOutputLayer;
- int mCh;
- int mDim;
- cv::Size mInputSize; /**< input tensor size */
-
- double mDeviation;
- double mMean;
- double mThreshold;
- int mOutputNumbers;
- cv::Size mSourceSize; /**< input image's size */
std::string mConfigFile;
std::string mWeightFile;
- std::string mUserFile;
- std::vector<std::string> mUserListName;
+
+ cv::Mat mInputData;
};
} /* InferenceEngineImpl */
-} /* TFImpl */
+} /* OpenCV */
-#endif /* __MEDIA_VISION_INFERENCE_OPENCV_H__ */
+#endif /* __INFERENCE_ENGINE_IMPL_OPENCV_H__ */