Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #40 from chaoli2/add_result_filtering
Browse files Browse the repository at this point in the history
result filter enabled
  • Loading branch information
chaoli2 authored Jul 18, 2019
2 parents 7c2b8b5 + 2fc6785 commit 385b2c3
Show file tree
Hide file tree
Showing 35 changed files with 992 additions and 318 deletions.
2 changes: 2 additions & 0 deletions dynamic_vino_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,13 @@ add_library(${PROJECT_NAME} SHARED
src/pipeline_params.cpp
src/pipeline_manager.cpp
src/engines/engine.cpp
src/inferences/base_filter.cpp
src/inferences/base_inference.cpp
src/inferences/emotions_detection.cpp
src/inferences/age_gender_detection.cpp
src/inferences/face_detection.cpp
src/inferences/head_pose_detection.cpp
src/inferences/object_detection.cpp
src/inferences/object_detection_ssd.cpp
src/inferences/object_detection_yolov2_voc.cpp
src/inferences/object_segmentation.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ class AgeGenderDetection : public BaseInference
* or ROS topic.
*/
const void observeOutput(
const std::shared_ptr<Outputs::BaseOutput>& output) override;
const std::shared_ptr<Outputs::BaseOutput>& output,
const std::string filter_conditions) override;

const std::vector<cv::Rect> getFilteredROIs(
const std::string filter_conditions) const override;


private:
std::shared_ptr<Models::AgeGenderDetectionModel> valid_model_;
Expand Down
184 changes: 184 additions & 0 deletions dynamic_vino_lib/include/dynamic_vino_lib/inferences/base_filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @brief A header file with declaration for BaseFilter Class
* @file base_filter.hpp
*/
#ifndef DYNAMIC_VINO_LIB__INFERENCES__BASE_FILTER_HPP_
#define DYNAMIC_VINO_LIB__INFERENCES__BASE_FILTER_HPP_

#include <string>
#include <vector>
#include <utility>
#include <stack>
#include "dynamic_vino_lib/inferences/base_inference.h"

namespace dynamic_vino_lib
{

/**
* @class BaseFilter
* @brief Base class for result filter.
*/
class BaseFilter
{
public:
BaseFilter();
/**
* @brief Initiate a result filter.
*/
virtual void init() = 0;

/**
* @brief Accept the filter conditions for filtering.
* @param[in] Filter conditions.
*/
void acceptFilterConditions(const std::string &);

/**
* @brief Decide whether the input string is a relational operator or not.
* @param[in] A string to be decided.
* @return True if the input string is a relational operator, false if not.
*/
bool isRelationOperator(const std::string &);

/**
* @brief Decide whether the input string is a logic operator or not.
* @param[in] A string to be decided.
* @return True if the input string is a logic operator, false if not.
*/
bool isLogicOperator(const std::string &);

/**
* @brief Decide whether the an operator has a higher priority than anthor.
* @param[in] The two operators.
* @return True if the first operator has higher priority, false if not.
*/
bool isPriorTo(const std::string &, const std::string &);

/**
* @brief Convert the input bool variable to a string type.
* @param[in] A bool type to be converted.
* @return A converted string result.
*/
std::string boolToStr(bool);

/**
* @brief Convert the input string variable to a bool type.
* @param[in] A string type to be converted.
* @return A converted bool result.
*/
bool strToBool(const std::string &);

/**
* @brief Get the filter conditions in the suffix order.
* @return A vector with suffix-order filter conditions.
*/
const std::vector<std::string> & getSuffixConditions() const;

/**
* @brief Do logic operation with the given bool values and the operator.
* @param[in] A bool string, an logic operator, the other bool string.
* @return The logic operation result.
*/
bool logicOperation(const std::string &, const std::string &, const std::string &);

/**
* @brief Compare two strings with a given relational operator.
* @param[in] A string, an relational operator, the other string.
* @return True if valid, false if not.
*/
static bool stringCompare(const std::string &, const std::string &, const std::string &);

/**
* @brief Compare two floats with a given relational operator.
* @param[in] A float number, an relational operator, the other float number.
* @return True if valid, false if not.
*/
static bool floatCompare(float, const std::string &, float);

/**
* @brief Convert a string into a float number.
* @param[in] A string to be converted.
* @return The converted float number, 0 if string is invalid.
*/
static float stringToFloat(const std::string &);

/**
* @brief A macro to decide whether a given result satisfies the filter condition.
* @param[in] A key to function mapping, a given result.
* @return True if valid, false if not.
*/
#define ISVALIDRESULT(key_to_function, result) \
{ \
std::vector<std::string> suffix_conditons = getSuffixConditions(); \
std::stack<std::string> result_stack; \
for (auto elem : suffix_conditons) { \
if (!isRelationOperator(elem) && !isLogicOperator(elem)) { \
result_stack.push(elem); \
} \
else { \
try { \
std::string str1 = result_stack.top(); \
result_stack.pop(); \
std::string str2 = result_stack.top(); \
result_stack.pop(); \
if (key_to_function.count(str2)) { \
result_stack.push(boolToStr(key_to_function[str2](result, elem, str1))); \
} \
else { \
result_stack.push(boolToStr(logicOperation(str1, elem, str2))); \
} \
} \
catch (...) { \
slog::err << "Invalid filter conditions format!" << slog::endl; \
} \
} \
} \
if (result_stack.empty()) { \
return true; \
} \
return strToBool(result_stack.top()); \
}

private:
/**
* @brief Parse the filter conditions and stores it into a vector.
* @param[in] A string form filter conditions.
* @return The vector form filter conditions.
*/
std::vector<std::string> split(const std::string & filter_conditions);

/**
* @brief Convert the infix expression into suffix expression.
* @param[in] The infix form filter conditions.
*/
void infixToSuffix(std::vector<std::string>&infix_conditions);

/**
* @brief Strip the extra space in a string.
* @param[in] A string to be striped.
* @return The striped string.
*/
std::string strip(const std::string & str);

std::string striped_conditions_ = "";
std::vector<std::string> suffix_conditons_;
std::vector<std::string> relation_operators_ = {"==", "!=", "<=", ">=", "<", ">"};
std::vector<std::string> logic_operators_ = {"&&", "||"};
};
} // namespace dynamic_vino_lib

#endif // DYNAMIC_VINO_LIB__INFERENCES__BASE_FILTER_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ class BaseInference
virtual bool submitRequest();

virtual const void observeOutput(
const std::shared_ptr<Outputs::BaseOutput>& output) = 0;
const std::shared_ptr<Outputs::BaseOutput>& output,
const std::string filter_conditions) = 0;

/**
* @brief This function will fetch the results of the previous inference and
Expand All @@ -161,6 +162,16 @@ class BaseInference
* @return The name of the Inference instance.
*/
virtual const std::string getName() const = 0;
/**
* @brief Get the max batch size for one inference.
*/
inline int getMaxBatchSize()
{
return max_batch_size_;
}

virtual const std::vector<cv::Rect> getFilteredROIs(
const std::string filter_conditions) const = 0;

protected:
/**
Expand Down Expand Up @@ -200,33 +211,6 @@ class BaseInference
int max_batch_size_ = 1;
bool results_fetched_ = false;
};

class ObjectDetectionResult : public Result {
public:
friend class ObjectDetection;
explicit ObjectDetectionResult(const cv::Rect& location);
std::string getLabel() const { return label_; }
/**
* @brief Get the confidence that the detected area is a face.
* @return The confidence value.
*/
float getConfidence() const { return confidence_; }
bool operator<(const ObjectDetectionResult &s2) const
{
return this->confidence_ > s2.confidence_;
}

std::string label_ = "";
float confidence_ = -1;
};

class ObjectDetection : public BaseInference
{
public:
ObjectDetection() {};
virtual void loadNetwork(std::shared_ptr<Models::ObjectDetectionModel>) = 0;
};

} // namespace dynamic_vino_lib

#endif // DYNAMIC_VINO_LIB_INFERENCES_BASE_INFERENCE_H
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ class EmotionsDetection : public BaseInference
* or ROS topic.
*/
const void observeOutput(
const std::shared_ptr<Outputs::BaseOutput>& output) override;
const std::shared_ptr<Outputs::BaseOutput>& output,
const std::string filter_conditions) override;

const std::vector<cv::Rect> getFilteredROIs(
const std::string filter_conditions) const override;

private:
std::shared_ptr<Models::EmotionDetectionModel> valid_model_;
std::vector<Result> results_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,17 @@ class FaceDetection : public BaseInference
* @brief Show the observed detection result either through image window
or ROS topic.
*/
const void observeOutput(const std::shared_ptr<Outputs::BaseOutput>& output);
const void observeOutput(const std::shared_ptr<Outputs::BaseOutput>& output,
const std::string filter_conditions);
/**
* @brief Get the name of the Inference instance.
* @return The name of the Inference instance.
*/
const std::string getName() const override;

const std::vector<cv::Rect> getFilteredROIs(
const std::string filter_conditions) const override;

private:
std::shared_ptr<Models::FaceDetectionModel> valid_model_;
std::vector<Result> results_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ class HeadPoseDetection : public BaseInference
or ROS topic.
*/
const void observeOutput(
const std::shared_ptr<Outputs::BaseOutput>& output) override;
const std::shared_ptr<Outputs::BaseOutput>& output,
const std::string filter_conditions) override;

const std::vector<cv::Rect> getFilteredROIs(
const std::string filter_conditions) const override;

private:
std::shared_ptr<Models::HeadPoseDetectionModel> valid_model_;
Expand Down
Loading

0 comments on commit 385b2c3

Please sign in to comment.