2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __MEDIA_VISION_INFERENCE_H__
18 #define __MEDIA_VISION_INFERENCE_H__
23 #include "mv_common.h"
24 #include "inference_engine_error.h"
25 #include "inference_engine_common_impl.h"
26 #include "mv_inference_private.h"
27 #include <mv_inference_type.h>
28 #include <opencv2/core.hpp>
29 #include <opencv2/imgproc.hpp>
31 #include "PreProcess.h"
32 #include "PostProcess.h"
33 #include "TensorBuffer.h"
37 #define HUMAN_POSE_MAX_LANDMARKS 16
38 #define HUMAN_POSE_MAX_PARTS 6
42 * @brief This file contains the inference class definition which
43 * provides inference interface.
45 using namespace InferenceEngineInterface::Common;
47 typedef struct _ImageClassficationResults {
48 int number_of_classes;
49 std::vector<int> indices;
50 std::vector<std::string> names;
51 std::vector<float> confidences;
52 } ImageClassificationResults; /**< structure ImageClassificationResults */
54 typedef struct _ObjectDetectionResults {
55 int number_of_objects;
56 std::vector<int> indices;
57 std::vector<std::string> names;
58 std::vector<float> confidences;
59 std::vector<cv::Rect> locations;
60 } ObjectDetectionResults; /**< structure ObjectDetectionResults */
62 typedef struct _FaceDetectionResults {
64 std::vector<float> confidences;
65 std::vector<cv::Rect> locations;
66 } FaceDetectionResults; /**< structure FaceDetectionResults */
68 typedef struct _FacialLandMarkDetectionResults {
69 int number_of_landmarks;
70 std::vector<cv::Point> locations;
71 } FacialLandMarkDetectionResults; /**< structure FacialLandMarkDetectionResults */
73 typedef struct _PoseLandmarkDetectionResults {
74 int number_of_landmarks;
75 std::vector<cv::Point2f> locations;
76 std::vector<float> score;
77 } PoseLandmarkDetectionResults; /**< structure PoseLandmarkDetectionResults */
90 struct InferenceConfig {
92 * @brief Default constructor for the @ref InferenceConfig
98 std::string mConfigFilePath; /**< Path of a model configuration file */
100 std::string mWeightFilePath; /**< Path of a model weight file */
102 std::string mUserFilePath; /**< Path of model user file */
104 TensorInfo mTensorInfo; /**< Tensor information */
106 mv_inference_data_type_e mDataType; /**< Data type of a input tensor */
108 mv_inference_backend_type_e mBackedType; /**< Backed type of model files */
110 int mTargetTypes; /**< Target type to run inference */
112 double mConfidenceThresHold; /**< Confidence threshold value */
114 double mMeanValue; /**< The mean value for normalization */
116 double mStdValue; /**< The scale factor value for normalization */
118 int mMaxOutputNumbers;
120 std::vector<std::string> mInputLayerNames; /**< The input layer names */
121 std::vector<std::string> mOutputLayerNames; /**< The output layer names */
128 * @brief Creates an Inference class instance.
135 * @brief Destroys an Inference class instance including
143 * @brief Configure modelfiles
147 void ConfigureModelFiles(const std::string modelConfigFilePath,
148 const std::string modelWeightFilePath,
149 const std::string modelUserFilePath);
152 * @brief Configure input tensor information
155 * @remarks deprecated Replayced by ConfigureInputInfo
157 void ConfigureTensorInfo(int width, int height, int dim, int ch,
158 double stdValue, double meanValue);
161 * @brief Configure input information
165 void ConfigureInputInfo(int width, int height, int dim, int ch,
166 double stdValue, double meanValue, int dataType,
167 const std::vector<std::string> names);
169 void ConfigureOutputInfo(std::vector<std::string> names);
172 * @brief Configure inference backend type.
176 int ConfigureBackendType(const mv_inference_backend_type_e backendType);
179 * @brief Configure a inference target device type such as CPU, GPU or NPU. (only one type can be set)
180 * @details Internally, a given device type will be converted to new type.
181 * This API is just used for backward compatibility.
183 * @since_tizen 6.0 (Deprecated)
185 int ConfigureTargetTypes(int targetType, bool isNewVersion);
188 * @brief Configure inference target devices such as CPU, GPU or NPU. (one more types can be combined)
192 int ConfigureTargetDevices(const int targetDevices);
195 * @brief Check supported target devices
199 bool IsTargetDeviceSupported(const int targetDevices);
202 * @brief Configure the maximum number of inference results
206 void ConfigureOutput(const int maxOutputNumbers);
209 * @brief Configure the confidence threshold
213 void ConfigureThreshold(const double threshold);
216 * @brief Parses the metadata file path
220 int ParseMetadata(const std::string filePath);
223 * @brief Bind a backend engine
224 * @details Use this function to bind a backend engine for the inference.
225 * This creates a inference engine common class object, and loads a backend
226 * library which interfaces with a Neural Network runtime such as TF Lite,
227 * OpenCV, ARMNN and so on.
229 * Ps. The created inference engine common object will be released and its
230 * corresponding backend library will be unbound when deconstructor
231 * of Inference class will be called.
235 * @return @c 0 on success, otherwise a negative error value
236 * @retval #MEDIA_VISION_ERROR_NONE Successful
237 * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
242 * @brief Set default configuration for the inference
243 * @details Use this function to set default configuration given in json file by user.
245 * Ps. this callback should be called after Bind callback.
249 * @return @c 0 on success, otherwise a negative error value
250 * @retval #MEDIA_VISION_ERROR_NONE Successful
251 * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
256 * @brief Load model files
257 * @details Use this function to load given model files for the inference.
259 * Ps. this callback should be called after Prepare callback.
263 * @return @c 0 on success, otherwise a negative error value
264 * @retval #MEDIA_VISION_ERROR_NONE Successful
265 * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
270 * @brief Runs inference with a region of a given image
271 * @details Use this function to run forward pass with the given image.
272 * The given image is preprocessed and the region of the image is
273 * thrown to neural network. Then, the output tensor is returned.
274 * If roi is NULL, then full source will be analyzed.
277 * @return @c true on success, otherwise a negative error value
279 int Run(std::vector<mv_source_h> &mvSources,
280 std::vector<mv_rectangle_s> &rects);
283 * @brief Gets that given engine is supported or not
286 * @return @c true on success, otherwise a negative error value
288 std::pair<std::string, bool> GetSupportedInferenceBackend(int backend);
291 * @brief Gets the ImageClassificationResults
294 * @return @c true on success, otherwise a negative error value
296 int GetClassficationResults(ImageClassificationResults *results);
299 * @brief Gets the ObjectDetectioResults
302 * @return @c true on success, otherwise a negative error value
304 int GetObjectDetectionResults(ObjectDetectionResults *results);
307 * @brief Gets the FaceDetectioResults
310 * @return @c true on success, otherwise a negative error value
312 int GetFaceDetectionResults(FaceDetectionResults *results);
315 * @brief Gets the FacialLandmarkDetectionResults
318 * @return @c true on success, otherwise a negative error value
320 int GetFacialLandMarkDetectionResults(FacialLandMarkDetectionResults *results);
323 * @brief Gets the PoseLandmarkDetectionResults
326 * @return @c true on success, otherwise a negative error value
328 int GetPoseLandmarkDetectionResults(std::unique_ptr<mv_inference_pose_s> &detectionResults,
329 int width, int height);
331 mv_engine_config_h GetEngineConfig(void)
333 return engine_config;
336 void SetEngineConfig(mv_engine_config_h config)
338 engine_config = config;
343 return mConfig.mTargetTypes;
347 bool mCanRun; /**< The flag indicating ready to run Inference */
348 InferenceConfig mConfig;
349 inference_engine_capacity mBackendCapacity;
350 std::map<int, std::pair<std::string, bool> > mSupportedInferenceBackend;
354 cv::Size mSourceSize;
355 mv_engine_config_h engine_config;
356 InferenceEngineCommon *mBackend;
357 std::map<std::string, int> mModelFormats;
358 std::vector<std::string> mUserListName;
359 //std::map<std::string, inference_engine_tensor_buffer> mInputTensorBuffers;
360 TensorBuffer mInputTensorBuffers;
361 inference_engine_layer_property mInputLayerProperty;
362 //std::map<std::string, inference_engine_tensor_buffer> mOutputTensorBuffers;
363 TensorBuffer mOutputTensorBuffers;
364 inference_engine_layer_property mOutputLayerProperty;
370 void CheckSupportedInferenceBackend();
371 int ConvertEngineErrorToVisionError(int error);
372 int ConvertTargetTypes(int given_types);
373 int ConvertToCv(int given_type);
374 inference_tensor_data_type_e ConvertToIE(int given_type);
375 int Preprocess(cv::Mat cvImg, cv::Mat cvDst, int data_type);
376 int PrepareTenosrBuffers(void);
377 void CleanupTensorBuffers(void);
378 int SetUserFile(std::string filename);
379 int FillOutputResult(tensor_t &outputData);
386 #endif /* __MEDIA_VISION_INFERENCE_H__ */