Remove apis related to IInferenceEngineVision 48/214448/1 accepted/tizen_5.5_unified accepted/tizen_5.5_unified_mobile_hotfix accepted/tizen_5.5_unified_wearable_hotfix tizen_5.5 tizen_5.5_mobile_hotfix tizen_5.5_tv tizen_5.5_wearable_hotfix accepted/tizen/5.5/unified/20191031.025140 accepted/tizen/5.5/unified/mobile/hotfix/20201027.084831 accepted/tizen/5.5/unified/wearable/hotfix/20201027.115504 accepted/tizen/unified/20190925.042226 submit/tizen/20190924.061550 submit/tizen/20190925.014740 submit/tizen_5.5/20191031.000003 submit/tizen_5.5_mobile_hotfix/20201026.185103 submit/tizen_5.5_wearable_hotfix/20201026.184303 tizen_5.5.m2_release
authorTae-Young Chung <ty83.chung@samsung.com>
Mon, 23 Sep 2019 06:07:55 +0000 (15:07 +0900)
committerTae-Young Chung <ty83.chung@samsung.com>
Mon, 23 Sep 2019 06:07:57 +0000 (15:07 +0900)
inference-engine-opencv is a plugin to provide inference only.
Thus, domain specific functions such as vision should be removed
even though it is OpenCV.
Instead, add apis GetInputLayerAttrType(), SetInputDataBuffer(), GetInputDataPtr()
which can be used to access to memory.

Change-Id: I8abaa91b10396a2687b25e10d50a31d70405c2ed
Signed-off-by: Tae-Young Chung <ty83.chung@samsung.com>
CMakeLists.txt
packaging/inference-engine-opencv.spec
src/inference_engine_opencv.cpp
src/inference_engine_opencv_private.h

index 60a1e98..0b2dd01 100644 (file)
@@ -8,7 +8,7 @@ SET(CMAKE_INSTALL_PREFIX /usr)
 SET(PREFIX ${CMAKE_INSTALL_PREFIX})
 
 SET(INC_DIR "${PROJECT_SOURCE_DIR}/include")
-SET(dependents "dlog inference-engine-interface-vision inference-engine-interface-common")
+SET(dependents "dlog inference-engine-interface-common")
 
 INCLUDE(FindPkgConfig)
 pkg_check_modules(${fw_name} REQUIRED ${dependents})
@@ -96,4 +96,4 @@ ADD_CUSTOM_COMMAND(
         VERBATIM
 )
 
-ENDIF(UNIX)
\ No newline at end of file
+ENDIF(UNIX)
index 285a6bd..518500b 100644 (file)
@@ -1,7 +1,7 @@
 Name:       inference-engine-opencv
 Summary:    OpenCV based implementation of inference-engine-interface
 Version:    0.0.1
-Release:    5
+Release:    6
 Group:      Multimedia/Libraries
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
@@ -10,7 +10,6 @@ Requires(postun): /sbin/ldconfig
 BuildRequires: cmake
 BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(opencv) >= 3.4.1
-BuildRequires: pkgconfig(inference-engine-interface-vision)
 BuildRequires: pkgconfig(inference-engine-interface-common)
 
 %description
index 5cd4423..d0cfdc4 100644 (file)
 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");
@@ -43,50 +42,12 @@ InferenceOpenCV::~InferenceOpenCV()
     ;
 }
 
-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);
@@ -98,25 +59,6 @@ int InferenceOpenCV::SetOutputTensorParam()
     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();
@@ -158,12 +100,6 @@ int InferenceOpenCV::Load()
                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);
@@ -181,10 +117,6 @@ int InferenceOpenCV::Load()
         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;
 }
 
@@ -198,207 +130,46 @@ int InferenceOpenCV::CreateInputLayerPassage()
     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();
@@ -408,41 +179,18 @@ int InferenceOpenCV::GetInferenceResult(std::vector<std::vector<int>>& dimInfo,
             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;
 }
 
index f1d071d..c2992c7 100644 (file)
@@ -17,7 +17,7 @@
 #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();
 
@@ -55,19 +53,9 @@ public:
 
     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;
@@ -76,30 +64,18 @@ public:
 
     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;
@@ -107,23 +83,14 @@ private:
 
     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__ */