Initial setup for CLion

This commit is contained in:
2026-03-28 16:54:11 +11:00
parent 239cc02591
commit 7b4134133c
1136 changed files with 811916 additions and 0 deletions

View File

@@ -0,0 +1,217 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <map>
#include <string>
#include <vector>
#include <opencv2/core/core.hpp>
#include "cnn.hpp"
#include "openvino/openvino.hpp"
/**
* @brief Class for detection with action info
*/
struct DetectedAction {
/** @brief BBox of detection */
cv::Rect rect;
/** @brief Action label */
int label;
/** @brief Confidence of detection */
float detection_conf;
/** @brief Confidence of predicted action */
float action_conf;
/**
* @brief Constructor
*/
DetectedAction(const cv::Rect& rect, int label,
float detection_conf, float action_conf)
: rect(rect), label(label), detection_conf(detection_conf),
action_conf(action_conf) {}
};
using DetectedActions = std::vector<DetectedAction>;
/**
* @brief Class to store SSD-based head info
*/
struct SSDHead {
/** @brief Step size for the head */
int step;
/** @brief Vector of anchors */
std::vector<cv::Size2f> anchors;
/**
* @brief Constructor
*/
SSDHead(int step, const std::vector<cv::Size2f>& anchors) : step(step), anchors(anchors) {}
};
using SSDHeads = std::vector<SSDHead>;
/**
* @brief Config for the Action Detection model
*/
struct ActionDetectorConfig : public CnnConfig {
explicit ActionDetectorConfig(const std::string& path_to_model, const std::string& model_type)
: CnnConfig(path_to_model, model_type) {}
/** @brief Name of output blob with location info */
std::string old_loc_blob_name{"mbox_loc1/out/conv/flat"};
/** @brief Name of output blob with detection confidence info */
std::string old_det_conf_blob_name{"mbox_main_conf/out/conv/flat/softmax/flat"};
/** @brief Prefix of name of output blob with action confidence info */
std::string old_action_conf_blob_name_prefix{"out/anchor"};
/** @brief Name of output blob with priorbox info */
std::string old_priorbox_blob_name{"mbox/priorbox"};
/** @brief Name of output blob with location info */
std::string new_loc_blob_name{"ActionNet/out_detection_loc"};
/** @brief Name of output blob with detection confidence info */
std::string new_det_conf_blob_name{"ActionNet/out_detection_conf"};
/** @brief Prefix of name of output blob with action confidence info */
std::string new_action_conf_blob_name_prefix{"ActionNet/action_heads/out_head_"};
/** @brief Suffix of name of output blob with action confidence info */
std::string new_action_conf_blob_name_suffix{"_anchor_"};
/** @brief Scale parameter for Soft-NMS algorithm */
float nms_sigma = 0.6f;
/** @brief Threshold for detected objects */
float detection_confidence_threshold = 0.4f;
/** @brief Threshold for recognized actions */
float action_confidence_threshold = 0.75f;
/** @brief Scale of action logits for the old network version */
float old_action_scale = 3.f;
/** @brief Scale of action logits for the new network version */
float new_action_scale = 16.f;
/** @brief Default action class label */
int default_action_id = 0;
/** @brief Number of top-score bboxes in output */
size_t keep_top_k = 200;
/** @brief Number of SSD anchors for the old network version */
std::vector<int> old_anchors{4};
/** @brief Number of SSD anchors for the new network version */
std::vector<int> new_anchors{1, 4};
/** @brief Number of actions to detect */
size_t num_action_classes = 3;
/** @brief Async execution flag */
bool is_async = true;
/** @brief SSD bbox encoding variances */
float variances[4]{0.1f, 0.1f, 0.2f, 0.2f};
SSDHeads new_det_heads{{8, {{26.17863728f, 58.670372f}}},
{16, {{35.36f, 81.829632f},
{45.8114572f, 107.651852f},
{63.31491832f, 142.595732f},
{93.5070856f, 201.107692f}}}};
};
class ActionDetection : public AsyncDetection<DetectedAction>, public BaseCnnDetection {
public:
explicit ActionDetection(const ActionDetectorConfig& config);
void submitRequest() override;
void enqueue(const cv::Mat& frame) override;
void wait() override { BaseCnnDetection::wait(); }
DetectedActions fetchResults() override;
private:
ActionDetectorConfig m_config;
ov::CompiledModel m_model;
ov::Layout m_modelLayout;
std::string m_input_name;
std::map<std::string, ov::Tensor> m_outputs;
int m_enqueued_frames = 0;
float m_width = 0;
float m_height = 0;
bool m_new_model = false;
std::vector<int> m_head_ranges;
std::vector<int> m_head_step_sizes;
std::vector<cv::Size> m_head_blob_sizes;
std::vector<std::vector<int>> m_glob_anchor_map;
std::vector<std::string> m_glob_anchor_names;
int m_num_glob_anchors = 0;
cv::Size m_network_input_size;
int m_num_candidates;
bool m_binary_task;
/**
* @brief BBox in normalized form (each coordinate is in range [0;1]).
*/
struct NormalizedBBox {
float xmin;
float ymin;
float xmax;
float ymax;
};
typedef std::vector<NormalizedBBox> NormalizedBBoxes;
/**
* @brief Translates the detections from the network outputs
*
* @param loc Location buffer
* @param main_conf Detection conf buffer
* @param add_conf Action conf buffer
* @param priorboxes Priorboxes buffer
* @param frame_size Size of input image (WxH)
* @return Detected objects
*/
DetectedActions GetDetections(const cv::Mat& loc,
const cv::Mat& main_conf,
const cv::Mat& priorboxes,
const std::vector<cv::Mat>& add_conf,
const cv::Size& frame_size) const;
/**
* @brief Translate input buffer to BBox
*
* @param data Input buffer
* @return BBox
*/
inline NormalizedBBox
ParseBBoxRecord(const float* data, bool inverse) const;
/**
* @brief Translate input buffer to BBox
*
* @param data Input buffer
* @return BBox
*/
inline NormalizedBBox
GeneratePriorBox(int pos, int step, const cv::Size2f& anchor, const cv::Size& blob_size) const;
/**
* @brief Translates input blobs in SSD format to bbox in CV_Rect
*
* @param prior_bbox Prior boxes in SSD format
* @param variances Variances of prior boxes in SSD format
* @param encoded_bbox BBox to decode
* @param frame_size Size of input image (WxH)
* @return BBox in CV_Rect format
*/
cv::Rect ConvertToRect(const NormalizedBBox& prior_bbox,
const NormalizedBBox& variances,
const NormalizedBBox& encoded_bbox,
const cv::Size& frame_size) const;
/**
* @brief Carry out Soft Non-Maximum Suppression algorithm under detected actions
*
* @param detections Detected actions
* @param sigma Scale parameter
* @param top_k Number of top-score bboxes
* @param min_det_conf Minimum detection confidence
* @param out_indices Out indices of valid detections
*/
void SoftNonMaxSuppression(const DetectedActions& detections,
const float sigma,
size_t top_k,
const float min_det_conf,
std::vector<int>* out_indices) const;
};

View File

@@ -0,0 +1,50 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <vector>
/**
* @brief Class for action info
*/
using Action = int;
/**
* @brief Class for events on a single frame with action info
*/
struct FrameEvent {
/** @brief Frame index */
int frame_id;
/** @brief Action label */
Action action;
/**
* @brief Constructor
*/
FrameEvent(int frame_id, Action action)
: frame_id(frame_id), action(action) {}
};
using FrameEventsTrack = std::vector<FrameEvent>;
/**
* @brief Class for range of the same event with action info
*/
struct RangeEvent {
/** @brief Start frame index */
int begin_frame_id;
/** @brief Next after the last valid frame index */
int end_frame_id;
/** @brief Action label */
Action action;
/**
* @brief Constructor
*/
RangeEvent(int begin_frame_id, int end_frame_id, Action action)
: begin_frame_id(begin_frame_id), end_frame_id(end_frame_id), action(action) {}
};
using RangeEventsTrack = std::vector<RangeEvent>;
enum ActionsType { STUDENT, TEACHER, TOP_K };

View File

@@ -0,0 +1,145 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <functional>
#include "openvino/openvino.hpp"
#include "utils/ocv_common.hpp"
/**
* @brief Base class of config for network
*/
struct CnnConfig {
explicit CnnConfig(const std::string& path_to_model, const std::string& model_type = "") :
m_path_to_model(path_to_model), m_model_type(model_type) {}
/** @brief Path to model description */
std::string m_path_to_model;
/** @brief Model type*/
std::string m_model_type;
/** @brief Maximal size of batch */
int m_max_batch_size{1};
/** @brief OpenVINO Core instance */
ov::Core m_core;
/** @brief Device name */
std::string m_deviceName;
};
/**
* @brief Base class of model
*/
class CnnDLSDKBase {
public:
using Config = CnnConfig;
/**
* @brief Constructor
*/
explicit CnnDLSDKBase(const Config& config);
/**
* @brief Descructor
*/
~CnnDLSDKBase() {}
/**
* @brief Loads network
*/
void Load();
protected:
/**
* @brief Run model in batch mode
*
* @param frames Vector of input images
* @param results_fetcher Callback to fetch inference results
*/
void InferBatch(const std::vector<cv::Mat>& frames,
const std::function<void(const std::map<std::string, ov::Tensor>&, size_t)>& results_fetcher);
/** @brief Config */
Config m_config;
/** @brief Model inputs info */
ov::OutputVector m_inInfo;
/** @brief Model outputs info */
ov::OutputVector m_outInfo_;
/** @brief Model layout */
ov::Layout m_desired_layout;
/** @brief Model input shape */
ov::Shape m_modelShape;
/** @brief Compled model */
ov::CompiledModel m_compiled_model;
/** @brief Inference request */
ov::InferRequest m_infer_request;
ov::Tensor m_in_tensor;
/** @brief Names of output tensors */
std::vector<std::string> m_output_tensors_names;
};
class VectorCNN : public CnnDLSDKBase {
public:
explicit VectorCNN(const CnnConfig& config);
void Compute(const cv::Mat& image,
cv::Mat* vector, cv::Size outp_shape = cv::Size());
void Compute(const std::vector<cv::Mat>& images,
std::vector<cv::Mat>* vectors, cv::Size outp_shape = cv::Size());
int maxBatchSize() const;
};
class AsyncAlgorithm {
public:
virtual ~AsyncAlgorithm() {}
virtual void enqueue(const cv::Mat& frame) = 0;
virtual void submitRequest() = 0;
virtual void wait() = 0;
};
template <typename T>
class AsyncDetection : public AsyncAlgorithm {
public:
virtual std::vector<T> fetchResults() = 0;
};
template <typename T>
class NullDetection : public AsyncDetection<T> {
public:
void enqueue(const cv::Mat&) override {}
void submitRequest() override {}
void wait() override {}
std::vector<T> fetchResults() override { return {}; }
};
class BaseCnnDetection : public AsyncAlgorithm {
protected:
std::shared_ptr<ov::InferRequest> m_request;
const bool m_isAsync;
std::string m_detectorName;
public:
explicit BaseCnnDetection(bool isAsync = false) : m_isAsync(isAsync) {}
void submitRequest() override {
if (m_request == nullptr)
return;
if (m_isAsync) {
m_request->start_async();
} else {
m_request->infer();
}
}
void wait() override {
if (!m_request || !m_isAsync)
return;
m_request->wait();
}
};

View File

@@ -0,0 +1,63 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <map>
#include <string>
#include <vector>
#include <opencv2/core/core.hpp>
#include "openvino/openvino.hpp"
#include "cnn.hpp"
namespace detection {
struct DetectedObject {
cv::Rect rect;
float confidence;
explicit DetectedObject(const cv::Rect& rect = cv::Rect(), float confidence = -1.0f) :
rect(rect), confidence(confidence) {}
};
using DetectedObjects = std::vector<DetectedObject>;
struct DetectorConfig : public CnnConfig {
explicit DetectorConfig(const std::string& path_to_model) :
CnnConfig(path_to_model) {}
float confidence_threshold{0.6f};
float increase_scale_x{1.15f};
float increase_scale_y{1.15f};
bool is_async = true;
int input_h = 600;
int input_w = 600;
};
class FaceDetection : public AsyncDetection<DetectedObject>, public BaseCnnDetection {
private:
DetectorConfig m_config;
ov::CompiledModel m_model;
std::string m_input_name;
std::string m_output_name;
int m_max_detections_count = 0;
int m_object_size = 0;
int m_enqueued_frames = 0;
float m_width = 0;
float m_height = 0;
public:
explicit FaceDetection(const DetectorConfig& config);
void submitRequest() override;
void enqueue(const cv::Mat& frame) override;
void wait() override { BaseCnnDetection::wait(); }
DetectedObjects fetchResults() override;
};
} // namespace detection

View File

@@ -0,0 +1,63 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <map>
#include <string>
#include <vector>
#include <opencv2/core/core.hpp>
#include "cnn.hpp"
#include "detector.hpp"
enum class RegistrationStatus {
SUCCESS,
FAILURE_LOW_QUALITY,
FAILURE_NOT_DETECTED,
};
struct GalleryObject {
std::vector<cv::Mat> embeddings;
std::string label;
int id;
GalleryObject(const std::vector<cv::Mat>& embeddings,
const std::string& label, int id) :
embeddings(embeddings), label(label), id(id) {}
};
class EmbeddingsGallery {
public:
static const char unknown_label[];
static const int unknown_id;
EmbeddingsGallery(const std::string& ids_list, double threshold, int min_size_fr,
bool crop_gallery, const detection::DetectorConfig& detector_config,
VectorCNN& landmarks_det,
VectorCNN& image_reid,
bool use_greedy_matcher=false);
size_t size() const;
std::vector<int> GetIDsByEmbeddings(const std::vector<cv::Mat>& embeddings) const;
std::string GetLabelByID(int id) const;
std::vector<std::string> GetIDToLabelMap() const;
bool LabelExists(const std::string& label) const;
private:
RegistrationStatus RegisterIdentity(const std::string& identity_label,
const cv::Mat& image,
int min_size_fr,
bool crop_gallery,
detection::FaceDetection& detector,
VectorCNN& landmarks_det,
VectorCNN& image_reid,
cv::Mat& embedding);
std::vector<int> idx_to_id;
double reid_threshold;
std::vector<GalleryObject> identities;
bool use_greedy_matcher;
};
void AlignFaces(std::vector<cv::Mat>* face_images,
std::vector<cv::Mat>* landmarks_vec);

View File

@@ -0,0 +1,62 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <fstream>
#include <iostream>
#include <map>
#include <vector>
#include <opencv2/opencv.hpp>
#include <utils/slog.hpp>
#include "actions.hpp"
#include "tracker/tracker.hpp"
class DetectionsLogger {
private:
bool m_write_logs;
std::ofstream m_act_stat_log_stream;
cv::FileStorage m_act_det_log_stream;
slog::LogStream& m_log_stream;
public:
explicit DetectionsLogger(slog::LogStream& stream, bool enabled,
const std::string& act_stat_log_file,
const std::string& act_det_log_file);
~DetectionsLogger();
void CreateNextFrameRecord(const std::string& path, const int frame_idx,
const size_t width, const size_t height);
void AddFaceToFrame(const cv::Rect& rect, const std::string& id, const std::string& action);
void AddPersonToFrame(const cv::Rect& rect, const std::string& action, const std::string& id);
void AddDetectionToFrame(const TrackedObject& object, const int frame_idx);
void FinalizeFrameRecord();
void DumpDetections(const std::string& video_path,
const cv::Size frame_size,
const size_t num_frames,
const std::vector<Track>& face_tracks,
const std::map<int, int>& track_id_to_label_faces,
const std::vector<std::string>& action_idx_to_label,
const std::vector<std::string>& person_id_to_label,
const std::vector<std::map<int, int>>& frame_face_obj_id_to_action_maps);
void DumpTracks(const std::map<int, RangeEventsTrack>& obj_id_to_events,
const std::vector<std::string>& action_idx_to_label,
const std::map<int, int>& track_id_to_label_faces,
const std::vector<std::string>& person_id_to_label);
};
#define SCR_CHECK(cond) CV_Assert(cond);
#define SCR_CHECK_BINARY(actual, expected, op) \
CV_Assert(actual op expected);
#define SCR_CHECK_EQ(actual, expected) SCR_CHECK_BINARY(actual, expected, ==)
#define SCR_CHECK_NE(actual, expected) SCR_CHECK_BINARY(actual, expected, !=)
#define SCR_CHECK_LT(actual, expected) SCR_CHECK_BINARY(actual, expected, <)
#define SCR_CHECK_GT(actual, expected) SCR_CHECK_BINARY(actual, expected, >)
#define SCR_CHECK_LE(actual, expected) SCR_CHECK_BINARY(actual, expected, <=)
#define SCR_CHECK_GE(actual, expected) SCR_CHECK_BINARY(actual, expected, >=)

View File

@@ -0,0 +1,627 @@
// Copyright (c) 2006, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ---
// Revamped and reorganized by Craig Silverstein
//
// This is the file that should be included by any file which declares
// or defines a command line flag or wants to parse command line flags
// or print a program usage message (which will include information about
// flags). Executive summary, in the form of an example foo.cc file:
//
// #include "foo.h" // foo.h has a line "DECLARE_int32(start);"
// #include "validators.h" // hypothetical file defining ValidateIsFile()
//
// DEFINE_int32(end, 1000, "The last record to read");
//
// DEFINE_string(filename, "my_file.txt", "The file to read");
// // Crash if the specified file does not exist.
// static bool dummy = RegisterFlagValidator(&FLAGS_filename,
// &ValidateIsFile);
//
// DECLARE_bool(verbose); // some other file has a DEFINE_bool(verbose, ...)
//
// void MyFunc() {
// if (FLAGS_verbose) printf("Records %d-%d\n", FLAGS_start, FLAGS_end);
// }
//
// Then, at the command-line:
// ./foo --noverbose --start=5 --end=100
//
// For more details, see
// doc/gflags.html
//
// --- A note about thread-safety:
//
// We describe many functions in this routine as being thread-hostile,
// thread-compatible, or thread-safe. Here are the meanings we use:
//
// thread-safe: it is safe for multiple threads to call this routine
// (or, when referring to a class, methods of this class)
// concurrently.
// thread-hostile: it is not safe for multiple threads to call this
// routine (or methods of this class) concurrently. In gflags,
// most thread-hostile routines are intended to be called early in,
// or even before, main() -- that is, before threads are spawned.
// thread-compatible: it is safe for multiple threads to read from
// this variable (when applied to variables), or to call const
// methods of this class (when applied to classes), as long as no
// other thread is writing to the variable or calling non-const
// methods of this class.
#ifndef GFLAGS_GFLAGS_H_
#define GFLAGS_GFLAGS_H_
#include <string>
#include <vector>
#include "gflags/gflags_declare.h" // IWYU pragma: export
// We always want to export variables defined in user code
#ifndef GFLAGS_DLL_DEFINE_FLAG
# if GFLAGS_IS_A_DLL && defined(_MSC_VER)
# define GFLAGS_DLL_DEFINE_FLAG __declspec(dllexport)
# else
# define GFLAGS_DLL_DEFINE_FLAG
# endif
#endif
namespace GFLAGS_NAMESPACE {
// --------------------------------------------------------------------
// To actually define a flag in a file, use DEFINE_bool,
// DEFINE_string, etc. at the bottom of this file. You may also find
// it useful to register a validator with the flag. This ensures that
// when the flag is parsed from the commandline, or is later set via
// SetCommandLineOption, we call the validation function. It is _not_
// called when you assign the value to the flag directly using the = operator.
//
// The validation function should return true if the flag value is valid, and
// false otherwise. If the function returns false for the new setting of the
// flag, the flag will retain its current value. If it returns false for the
// default value, ParseCommandLineFlags() will die.
//
// This function is safe to call at global construct time (as in the
// example below).
//
// Example use:
// static bool ValidatePort(const char* flagname, int32 value) {
// if (value > 0 && value < 32768) // value is ok
// return true;
// printf("Invalid value for --%s: %d\n", flagname, (int)value);
// return false;
// }
// DEFINE_int32(port, 0, "What port to listen on");
// static bool dummy = RegisterFlagValidator(&FLAGS_port, &ValidatePort);
// Returns true if successfully registered, false if not (because the
// first argument doesn't point to a command-line flag, or because a
// validator is already registered for this flag).
extern GFLAGS_DLL_DECL bool RegisterFlagValidator(const bool* flag, bool (*validate_fn)(const char*, bool));
extern GFLAGS_DLL_DECL bool RegisterFlagValidator(const int32* flag, bool (*validate_fn)(const char*, int32));
extern GFLAGS_DLL_DECL bool RegisterFlagValidator(const uint32* flag, bool (*validate_fn)(const char*, uint32));
extern GFLAGS_DLL_DECL bool RegisterFlagValidator(const int64* flag, bool (*validate_fn)(const char*, int64));
extern GFLAGS_DLL_DECL bool RegisterFlagValidator(const uint64* flag, bool (*validate_fn)(const char*, uint64));
extern GFLAGS_DLL_DECL bool RegisterFlagValidator(const double* flag, bool (*validate_fn)(const char*, double));
extern GFLAGS_DLL_DECL bool RegisterFlagValidator(const std::string* flag, bool (*validate_fn)(const char*, const std::string&));
// Convenience macro for the registration of a flag validator
#define DEFINE_validator(name, validator) \
static const bool name##_validator_registered = \
GFLAGS_NAMESPACE::RegisterFlagValidator(&FLAGS_##name, validator)
// --------------------------------------------------------------------
// These methods are the best way to get access to info about the
// list of commandline flags. Note that these routines are pretty slow.
// GetAllFlags: mostly-complete info about the list, sorted by file.
// ShowUsageWithFlags: pretty-prints the list to stdout (what --help does)
// ShowUsageWithFlagsRestrict: limit to filenames with restrict as a substr
//
// In addition to accessing flags, you can also access argv[0] (the program
// name) and argv (the entire commandline), which we sock away a copy of.
// These variables are static, so you should only set them once.
//
// No need to export this data only structure from DLL, avoiding VS warning 4251.
struct CommandLineFlagInfo {
std::string name; // the name of the flag
std::string type; // the type of the flag: int32, etc
std::string description; // the "help text" associated with the flag
std::string current_value; // the current value, as a string
std::string default_value; // the default value, as a string
std::string filename; // 'cleaned' version of filename holding the flag
bool has_validator_fn; // true if RegisterFlagValidator called on this flag
bool is_default; // true if the flag has the default value and
// has not been set explicitly from the cmdline
// or via SetCommandLineOption
const void* flag_ptr; // pointer to the flag's current value (i.e. FLAGS_foo)
};
// Using this inside of a validator is a recipe for a deadlock.
// TODO(user) Fix locking when validators are running, to make it safe to
// call validators during ParseAllFlags.
// Also make sure then to uncomment the corresponding unit test in
// gflags_unittest.sh
extern GFLAGS_DLL_DECL void GetAllFlags(std::vector<CommandLineFlagInfo>* OUTPUT);
// These two are actually defined in gflags_reporting.cc.
extern GFLAGS_DLL_DECL void ShowUsageWithFlags(const char *argv0); // what --help does
extern GFLAGS_DLL_DECL void ShowUsageWithFlagsRestrict(const char *argv0, const char *restrict);
// Create a descriptive string for a flag.
// Goes to some trouble to make pretty line breaks.
extern GFLAGS_DLL_DECL std::string DescribeOneFlag(const CommandLineFlagInfo& flag);
// Thread-hostile; meant to be called before any threads are spawned.
extern GFLAGS_DLL_DECL void SetArgv(int argc, const char** argv);
// The following functions are thread-safe as long as SetArgv() is
// only called before any threads start.
extern GFLAGS_DLL_DECL const std::vector<std::string>& GetArgvs();
extern GFLAGS_DLL_DECL const char* GetArgv(); // all of argv as a string
extern GFLAGS_DLL_DECL const char* GetArgv0(); // only argv0
extern GFLAGS_DLL_DECL uint32 GetArgvSum(); // simple checksum of argv
extern GFLAGS_DLL_DECL const char* ProgramInvocationName(); // argv0, or "UNKNOWN" if not set
extern GFLAGS_DLL_DECL const char* ProgramInvocationShortName(); // basename(argv0)
// ProgramUsage() is thread-safe as long as SetUsageMessage() is only
// called before any threads start.
extern GFLAGS_DLL_DECL const char* ProgramUsage(); // string set by SetUsageMessage()
// VersionString() is thread-safe as long as SetVersionString() is only
// called before any threads start.
extern GFLAGS_DLL_DECL const char* VersionString(); // string set by SetVersionString()
// --------------------------------------------------------------------
// Normally you access commandline flags by just saying "if (FLAGS_foo)"
// or whatever, and set them by calling "FLAGS_foo = bar" (or, more
// commonly, via the DEFINE_foo macro). But if you need a bit more
// control, we have programmatic ways to get/set the flags as well.
// These programmatic ways to access flags are thread-safe, but direct
// access is only thread-compatible.
// Return true iff the flagname was found.
// OUTPUT is set to the flag's value, or unchanged if we return false.
extern GFLAGS_DLL_DECL bool GetCommandLineOption(const char* name, std::string* OUTPUT);
// Return true iff the flagname was found. OUTPUT is set to the flag's
// CommandLineFlagInfo or unchanged if we return false.
extern GFLAGS_DLL_DECL bool GetCommandLineFlagInfo(const char* name, CommandLineFlagInfo* OUTPUT);
// Return the CommandLineFlagInfo of the flagname. exit() if name not found.
// Example usage, to check if a flag's value is currently the default value:
// if (GetCommandLineFlagInfoOrDie("foo").is_default) ...
extern GFLAGS_DLL_DECL CommandLineFlagInfo GetCommandLineFlagInfoOrDie(const char* name);
enum GFLAGS_DLL_DECL FlagSettingMode {
// update the flag's value (can call this multiple times).
SET_FLAGS_VALUE,
// update the flag's value, but *only if* it has not yet been updated
// with SET_FLAGS_VALUE, SET_FLAG_IF_DEFAULT, or "FLAGS_xxx = nondef".
SET_FLAG_IF_DEFAULT,
// set the flag's default value to this. If the flag has not yet updated
// yet (via SET_FLAGS_VALUE, SET_FLAG_IF_DEFAULT, or "FLAGS_xxx = nondef")
// change the flag's current value to the new default value as well.
SET_FLAGS_DEFAULT
};
// Set a particular flag ("command line option"). Returns a string
// describing the new value that the option has been set to. The
// return value API is not well-specified, so basically just depend on
// it to be empty if the setting failed for some reason -- the name is
// not a valid flag name, or the value is not a valid value -- and
// non-empty else.
// SetCommandLineOption uses set_mode == SET_FLAGS_VALUE (the common case)
extern GFLAGS_DLL_DECL std::string SetCommandLineOption (const char* name, const char* value);
extern GFLAGS_DLL_DECL std::string SetCommandLineOptionWithMode(const char* name, const char* value, FlagSettingMode set_mode);
// --------------------------------------------------------------------
// Saves the states (value, default value, whether the user has set
// the flag, registered validators, etc) of all flags, and restores
// them when the FlagSaver is destroyed. This is very useful in
// tests, say, when you want to let your tests change the flags, but
// make sure that they get reverted to the original states when your
// test is complete.
//
// Example usage:
// void TestFoo() {
// FlagSaver s1;
// FLAG_foo = false;
// FLAG_bar = "some value";
//
// // test happens here. You can return at any time
// // without worrying about restoring the FLAG values.
// }
//
// Note: This class is marked with GFLAGS_ATTRIBUTE_UNUSED because all
// the work is done in the constructor and destructor, so in the standard
// usage example above, the compiler would complain that it's an
// unused variable.
//
// This class is thread-safe. However, its destructor writes to
// exactly the set of flags that have changed value during its
// lifetime, so concurrent _direct_ access to those flags
// (i.e. FLAGS_foo instead of {Get,Set}CommandLineOption()) is unsafe.
class GFLAGS_DLL_DECL FlagSaver {
public:
FlagSaver();
~FlagSaver();
private:
class FlagSaverImpl* impl_; // we use pimpl here to keep API steady
FlagSaver(const FlagSaver&); // no copying!
void operator=(const FlagSaver&);
};
// --------------------------------------------------------------------
// Some deprecated or hopefully-soon-to-be-deprecated functions.
// This is often used for logging. TODO(csilvers): figure out a better way
extern GFLAGS_DLL_DECL std::string CommandlineFlagsIntoString();
// Usually where this is used, a FlagSaver should be used instead.
extern GFLAGS_DLL_DECL
bool ReadFlagsFromString(const std::string& flagfilecontents,
const char* prog_name,
bool errors_are_fatal); // uses SET_FLAGS_VALUE
// These let you manually implement --flagfile functionality.
// DEPRECATED.
extern GFLAGS_DLL_DECL bool AppendFlagsIntoFile(const std::string& filename, const char* prog_name);
extern GFLAGS_DLL_DECL bool ReadFromFlagsFile(const std::string& filename, const char* prog_name, bool errors_are_fatal); // uses SET_FLAGS_VALUE
// --------------------------------------------------------------------
// Useful routines for initializing flags from the environment.
// In each case, if 'varname' does not exist in the environment
// return defval. If 'varname' does exist but is not valid
// (e.g., not a number for an int32 flag), abort with an error.
// Otherwise, return the value. NOTE: for booleans, for true use
// 't' or 'T' or 'true' or '1', for false 'f' or 'F' or 'false' or '0'.
extern GFLAGS_DLL_DECL bool BoolFromEnv(const char *varname, bool defval);
extern GFLAGS_DLL_DECL int32 Int32FromEnv(const char *varname, int32 defval);
extern GFLAGS_DLL_DECL uint32 Uint32FromEnv(const char *varname, uint32 defval);
extern GFLAGS_DLL_DECL int64 Int64FromEnv(const char *varname, int64 defval);
extern GFLAGS_DLL_DECL uint64 Uint64FromEnv(const char *varname, uint64 defval);
extern GFLAGS_DLL_DECL double DoubleFromEnv(const char *varname, double defval);
extern GFLAGS_DLL_DECL const char *StringFromEnv(const char *varname, const char *defval);
// --------------------------------------------------------------------
// The next two functions parse gflags from main():
// Set the "usage" message for this program. For example:
// string usage("This program does nothing. Sample usage:\n");
// usage += argv[0] + " <uselessarg1> <uselessarg2>";
// SetUsageMessage(usage);
// Do not include commandline flags in the usage: we do that for you!
// Thread-hostile; meant to be called before any threads are spawned.
extern GFLAGS_DLL_DECL void SetUsageMessage(const std::string& usage);
// Sets the version string, which is emitted with --version.
// For instance: SetVersionString("1.3");
// Thread-hostile; meant to be called before any threads are spawned.
extern GFLAGS_DLL_DECL void SetVersionString(const std::string& version);
// Looks for flags in argv and parses them. Rearranges argv to put
// flags first, or removes them entirely if remove_flags is true.
// If a flag is defined more than once in the command line or flag
// file, the last definition is used. Returns the index (into argv)
// of the first non-flag argument.
// See top-of-file for more details on this function.
#ifndef SWIG // In swig, use ParseCommandLineFlagsScript() instead.
extern GFLAGS_DLL_DECL uint32 ParseCommandLineFlags(int *argc, char*** argv, bool remove_flags);
#endif
// Calls to ParseCommandLineNonHelpFlags and then to
// HandleCommandLineHelpFlags can be used instead of a call to
// ParseCommandLineFlags during initialization, in order to allow for
// changing default values for some FLAGS (via
// e.g. SetCommandLineOptionWithMode calls) between the time of
// command line parsing and the time of dumping help information for
// the flags as a result of command line parsing. If a flag is
// defined more than once in the command line or flag file, the last
// definition is used. Returns the index (into argv) of the first
// non-flag argument. (If remove_flags is true, will always return 1.)
extern GFLAGS_DLL_DECL uint32 ParseCommandLineNonHelpFlags(int *argc, char*** argv, bool remove_flags);
// This is actually defined in gflags_reporting.cc.
// This function is misnamed (it also handles --version, etc.), but
// it's too late to change that now. :-(
extern GFLAGS_DLL_DECL void HandleCommandLineHelpFlags(); // in gflags_reporting.cc
// Allow command line reparsing. Disables the error normally
// generated when an unknown flag is found, since it may be found in a
// later parse. Thread-hostile; meant to be called before any threads
// are spawned.
extern GFLAGS_DLL_DECL void AllowCommandLineReparsing();
// Reparse the flags that have not yet been recognized. Only flags
// registered since the last parse will be recognized. Any flag value
// must be provided as part of the argument using "=", not as a
// separate command line argument that follows the flag argument.
// Intended for handling flags from dynamically loaded libraries,
// since their flags are not registered until they are loaded.
extern GFLAGS_DLL_DECL void ReparseCommandLineNonHelpFlags();
// Clean up memory allocated by flags. This is only needed to reduce
// the quantity of "potentially leaked" reports emitted by memory
// debugging tools such as valgrind. It is not required for normal
// operation, or for the google perftools heap-checker. It must only
// be called when the process is about to exit, and all threads that
// might access flags are quiescent. Referencing flags after this is
// called will have unexpected consequences. This is not safe to run
// when multiple threads might be running: the function is
// thread-hostile.
extern GFLAGS_DLL_DECL void ShutDownCommandLineFlags();
// --------------------------------------------------------------------
// Now come the command line flag declaration/definition macros that
// will actually be used. They're kind of hairy. A major reason
// for this is initialization: we want people to be able to access
// variables in global constructors and have that not crash, even if
// their global constructor runs before the global constructor here.
// (Obviously, we can't guarantee the flags will have the correct
// default value in that case, but at least accessing them is safe.)
// The only way to do that is have flags point to a static buffer.
// So we make one, using a union to ensure proper alignment, and
// then use placement-new to actually set up the flag with the
// correct default value. In the same vein, we have to worry about
// flag access in global destructors, so FlagRegisterer has to be
// careful never to destroy the flag-values it constructs.
//
// Note that when we define a flag variable FLAGS_<name>, we also
// preemptively define a junk variable, FLAGS_no<name>. This is to
// cause a link-time error if someone tries to define 2 flags with
// names like "logging" and "nologging". We do this because a bool
// flag FLAG can be set from the command line to true with a "-FLAG"
// argument, and to false with a "-noFLAG" argument, and so this can
// potentially avert confusion.
//
// We also put flags into their own namespace. It is purposefully
// named in an opaque way that people should have trouble typing
// directly. The idea is that DEFINE puts the flag in the weird
// namespace, and DECLARE imports the flag from there into the current
// namespace. The net result is to force people to use DECLARE to get
// access to a flag, rather than saying "extern GFLAGS_DLL_DECL bool FLAGS_whatever;"
// or some such instead. We want this so we can put extra
// functionality (like sanity-checking) in DECLARE if we want, and
// make sure it is picked up everywhere.
//
// We also put the type of the variable in the namespace, so that
// people can't DECLARE_int32 something that they DEFINE_bool'd
// elsewhere.
class GFLAGS_DLL_DECL FlagRegisterer {
public:
// We instantiate this template ctor for all supported types,
// so it is possible to place implementation of the FlagRegisterer ctor in
// .cc file.
// Calling this constructor with unsupported type will produce linker error.
template <typename FlagType>
FlagRegisterer(const char* name,
const char* help, const char* filename,
FlagType* current_storage, FlagType* defvalue_storage);
};
// Force compiler to not generate code for the given template specialization.
#if defined(_MSC_VER) && _MSC_VER < 1800 // Visual Studio 2013 version 12.0
#define GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(type)
#else
#define GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(type) \
extern template GFLAGS_DLL_DECL FlagRegisterer::FlagRegisterer( \
const char* name, const char* help, const char* filename, \
type* current_storage, type* defvalue_storage)
#endif
// Do this for all supported flag types.
GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(bool);
GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(int32);
GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(uint32);
GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(int64);
GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(uint64);
GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(double);
GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(std::string);
#undef GFLAGS_DECLARE_FLAG_REGISTERER_CTOR
// If your application #defines STRIP_FLAG_HELP to a non-zero value
// before #including this file, we remove the help message from the
// binary file. This can reduce the size of the resulting binary
// somewhat, and may also be useful for security reasons.
extern GFLAGS_DLL_DECL const char kStrippedFlagHelp[];
} // namespace GFLAGS_NAMESPACE
#ifndef SWIG // In swig, ignore the main flag declarations
#if defined(STRIP_FLAG_HELP) && STRIP_FLAG_HELP > 0
// Need this construct to avoid the 'defined but not used' warning.
#define MAYBE_STRIPPED_HELP(txt) \
(false ? (txt) : GFLAGS_NAMESPACE::kStrippedFlagHelp)
#else
#define MAYBE_STRIPPED_HELP(txt) txt
#endif
// Each command-line flag has two variables associated with it: one
// with the current value, and one with the default value. However,
// we have a third variable, which is where value is assigned; it's a
// constant. This guarantees that FLAG_##value is initialized at
// static initialization time (e.g. before program-start) rather than
// than global construction time (which is after program-start but
// before main), at least when 'value' is a compile-time constant. We
// use a small trick for the "default value" variable, and call it
// FLAGS_no<name>. This serves the second purpose of assuring a
// compile error if someone tries to define a flag named no<name>
// which is illegal (--foo and --nofoo both affect the "foo" flag).
#define DEFINE_VARIABLE(type, shorttype, name, value, help) \
namespace fL##shorttype { \
static const type FLAGS_nono##name = value; \
/* We always want to export defined variables, dll or no */ \
GFLAGS_DLL_DEFINE_FLAG type FLAGS_##name = FLAGS_nono##name; \
static type FLAGS_no##name = FLAGS_nono##name; \
static GFLAGS_NAMESPACE::FlagRegisterer o_##name( \
#name, MAYBE_STRIPPED_HELP(help), __FILE__, \
&FLAGS_##name, &FLAGS_no##name); \
} \
using fL##shorttype::FLAGS_##name
// For DEFINE_bool, we want to do the extra check that the passed-in
// value is actually a bool, and not a string or something that can be
// coerced to a bool. These declarations (no definition needed!) will
// help us do that, and never evaluate From, which is important.
// We'll use 'sizeof(IsBool(val))' to distinguish. This code requires
// that the compiler have different sizes for bool & double. Since
// this is not guaranteed by the standard, we check it with a
// COMPILE_ASSERT.
namespace fLB {
struct CompileAssert {};
typedef CompileAssert expected_sizeof_double_neq_sizeof_bool[
(sizeof(double) != sizeof(bool)) ? 1 : -1];
template<typename From> double GFLAGS_DLL_DECL IsBoolFlag(const From& from);
GFLAGS_DLL_DECL bool IsBoolFlag(bool from);
} // namespace fLB
// Here are the actual DEFINE_*-macros. The respective DECLARE_*-macros
// are in a separate include, gflags_declare.h, for reducing
// the physical transitive size for DECLARE use.
#define DEFINE_bool(name, val, txt) \
namespace fLB { \
typedef ::fLB::CompileAssert FLAG_##name##_value_is_not_a_bool[ \
(sizeof(::fLB::IsBoolFlag(val)) != sizeof(double))? 1: -1]; \
} \
DEFINE_VARIABLE(bool, B, name, val, txt)
#define DEFINE_int32(name, val, txt) \
DEFINE_VARIABLE(GFLAGS_NAMESPACE::int32, I, \
name, val, txt)
#define DEFINE_uint32(name,val, txt) \
DEFINE_VARIABLE(GFLAGS_NAMESPACE::uint32, U, \
name, val, txt)
#define DEFINE_int64(name, val, txt) \
DEFINE_VARIABLE(GFLAGS_NAMESPACE::int64, I64, \
name, val, txt)
#define DEFINE_uint64(name,val, txt) \
DEFINE_VARIABLE(GFLAGS_NAMESPACE::uint64, U64, \
name, val, txt)
#define DEFINE_double(name, val, txt) \
DEFINE_VARIABLE(double, D, name, val, txt)
// Strings are trickier, because they're not a POD, so we can't
// construct them at static-initialization time (instead they get
// constructed at global-constructor time, which is much later). To
// try to avoid crashes in that case, we use a char buffer to store
// the string, which we can static-initialize, and then placement-new
// into it later. It's not perfect, but the best we can do.
namespace fLS {
inline clstring* dont_pass0toDEFINE_string(char *stringspot,
const char *value) {
return new(stringspot) clstring(value);
}
inline clstring* dont_pass0toDEFINE_string(char *stringspot,
const clstring &value) {
return new(stringspot) clstring(value);
}
inline clstring* dont_pass0toDEFINE_string(char *stringspot,
int value);
// Auxiliary class used to explicitly call destructor of string objects
// allocated using placement new during static program deinitialization.
// The destructor MUST be an inline function such that the explicit
// destruction occurs in the same compilation unit as the placement new.
class StringFlagDestructor {
void *current_storage_;
void *defvalue_storage_;
public:
StringFlagDestructor(void *current, void *defvalue)
: current_storage_(current), defvalue_storage_(defvalue) {}
~StringFlagDestructor() {
reinterpret_cast<clstring*>(current_storage_ )->~clstring();
reinterpret_cast<clstring*>(defvalue_storage_)->~clstring();
}
};
} // namespace fLS
// We need to define a var named FLAGS_no##name so people don't define
// --string and --nostring. And we need a temporary place to put val
// so we don't have to evaluate it twice. Two great needs that go
// great together!
// The weird 'using' + 'extern' inside the fLS namespace is to work around
// an unknown compiler bug/issue with the gcc 4.2.1 on SUSE 10. See
// http://code.google.com/p/google-gflags/issues/detail?id=20
#define DEFINE_string(name, val, txt) \
namespace fLS { \
using ::fLS::clstring; \
using ::fLS::StringFlagDestructor; \
static union { void* align; char s[sizeof(clstring)]; } s_##name[2]; \
clstring* const FLAGS_no##name = ::fLS:: \
dont_pass0toDEFINE_string(s_##name[0].s, \
val); \
static GFLAGS_NAMESPACE::FlagRegisterer o_##name( \
#name, MAYBE_STRIPPED_HELP(txt), __FILE__, \
FLAGS_no##name, new (s_##name[1].s) clstring(*FLAGS_no##name)); \
static StringFlagDestructor d_##name(s_##name[0].s, s_##name[1].s); \
extern GFLAGS_DLL_DEFINE_FLAG clstring& FLAGS_##name; \
using fLS::FLAGS_##name; \
clstring& FLAGS_##name = *FLAGS_no##name; \
} \
using fLS::FLAGS_##name
#endif // SWIG
// Import gflags library symbols into alternative/deprecated namespace(s)
#include "gflags_gflags.h"
#endif // GFLAGS_GFLAGS_H_

View File

@@ -0,0 +1,121 @@
// Copyright (c) 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ---
//
// Implement helpful bash-style command line flag completions
//
// ** Functional API:
// HandleCommandLineCompletions() should be called early during
// program startup, but after command line flag code has been
// initialized, such as the beginning of HandleCommandLineHelpFlags().
// It checks the value of the flag --tab_completion_word. If this
// flag is empty, nothing happens here. If it contains a string,
// however, then HandleCommandLineCompletions() will hijack the
// process, attempting to identify the intention behind this
// completion. Regardless of the outcome of this deduction, the
// process will be terminated, similar to --helpshort flag
// handling.
//
// ** Overview of Bash completions:
// Bash can be told to programatically determine completions for the
// current 'cursor word'. It does this by (in this case) invoking a
// command with some additional arguments identifying the command
// being executed, the word being completed, and the previous word
// (if any). Bash then expects a sequence of output lines to be
// printed to stdout. If these lines all contain a common prefix
// longer than the cursor word, bash will replace the cursor word
// with that common prefix, and display nothing. If there isn't such
// a common prefix, bash will display the lines in pages using 'more'.
//
// ** Strategy taken for command line completions:
// If we can deduce either the exact flag intended, or a common flag
// prefix, we'll output exactly that. Otherwise, if information
// must be displayed to the user, we'll take the opportunity to add
// some helpful information beyond just the flag name (specifically,
// we'll include the default flag value and as much of the flag's
// description as can fit on a single terminal line width, as specified
// by the flag --tab_completion_columns). Furthermore, we'll try to
// make bash order the output such that the most useful or relevent
// flags are the most likely to be shown at the top.
//
// ** Additional features:
// To assist in finding that one really useful flag, substring matching
// was implemented. Before pressing a <TAB> to get completion for the
// current word, you can append one or more '?' to the flag to do
// substring matching. Here's the semantics:
// --foo<TAB> Show me all flags with names prefixed by 'foo'
// --foo?<TAB> Show me all flags with 'foo' somewhere in the name
// --foo??<TAB> Same as prior case, but also search in module
// definition path for 'foo'
// --foo???<TAB> Same as prior case, but also search in flag
// descriptions for 'foo'
// Finally, we'll trim the output to a relatively small number of
// flags to keep bash quiet about the verbosity of output. If one
// really wanted to see all possible matches, appending a '+' to the
// search word will force the exhaustive list of matches to be printed.
//
// ** How to have bash accept completions from a binary:
// Bash requires that it be informed about each command that programmatic
// completion should be enabled for. Example addition to a .bashrc
// file would be (your path to gflags_completions.sh file may differ):
/*
$ complete -o bashdefault -o default -o nospace -C \
'/home/build/eng/bash/bash_completions.sh --tab_completion_columns $COLUMNS' \
time env binary_name another_binary [...]
*/
// This would allow the following to work:
// $ /path/to/binary_name --vmodule<TAB>
// Or:
// $ ./bin/path/another_binary --gfs_u<TAB>
// (etc)
//
// Sadly, it appears that bash gives no easy way to force this behavior for
// all commands. That's where the "time" in the above example comes in.
// If you haven't specifically added a command to the list of completion
// supported commands, you can still get completions by prefixing the
// entire command with "env".
// $ env /some/brand/new/binary --vmod<TAB>
// Assuming that "binary" is a newly compiled binary, this should still
// produce the expected completion output.
#ifndef GFLAGS_COMPLETIONS_H_
#define GFLAGS_COMPLETIONS_H_
namespace google {
extern void HandleCommandLineCompletions(void);
}
#endif // GFLAGS_COMPLETIONS_H_

View File

@@ -0,0 +1,156 @@
// Copyright (c) 1999, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ---
//
// Revamped and reorganized by Craig Silverstein
//
// This is the file that should be included by any file which declares
// command line flag.
#ifndef GFLAGS_DECLARE_H_
#define GFLAGS_DECLARE_H_
// ---------------------------------------------------------------------------
// Namespace of gflags library symbols.
#define GFLAGS_NAMESPACE google
// ---------------------------------------------------------------------------
// Windows DLL import/export.
// Whether gflags library is a DLL.
//
// Set to 1 by default when the shared gflags library was built on Windows.
// Must be overwritten when this header file is used with the optionally also
// built static library instead; set by CMake's INTERFACE_COMPILE_DEFINITIONS.
#ifndef GFLAGS_IS_A_DLL
# define GFLAGS_IS_A_DLL 1
#endif
// We always want to import the symbols of the gflags library.
#ifndef GFLAGS_DLL_DECL
# if GFLAGS_IS_A_DLL && defined(_MSC_VER)
# define GFLAGS_DLL_DECL __declspec(dllimport)
# elif defined(__GNUC__) && __GNUC__ >= 4
# define GFLAGS_DLL_DECL __attribute__((visibility("default")))
# else
# define GFLAGS_DLL_DECL
# endif
#endif
// We always want to import variables declared in user code.
#ifndef GFLAGS_DLL_DECLARE_FLAG
# if GFLAGS_IS_A_DLL && defined(_MSC_VER)
# define GFLAGS_DLL_DECLARE_FLAG __declspec(dllimport)
# elif defined(__GNUC__) && __GNUC__ >= 4
# define GFLAGS_DLL_DECLARE_FLAG __attribute__((visibility("default")))
# else
# define GFLAGS_DLL_DECLARE_FLAG
# endif
#endif
// ---------------------------------------------------------------------------
// Flag types
#include <string>
#if 1
# include <stdint.h> // the normal place uint32_t is defined
#elif 1
# include <sys/types.h> // the normal place u_int32_t is defined
#elif 1
# include <inttypes.h> // a third place for uint32_t or u_int32_t
#endif
namespace GFLAGS_NAMESPACE {
#if 0 // C99
typedef int32_t int32;
typedef uint32_t uint32;
typedef int64_t int64;
typedef uint64_t uint64;
#elif 0 // BSD
typedef int32_t int32;
typedef u_int32_t uint32;
typedef int64_t int64;
typedef u_int64_t uint64;
#elif 1 // Windows
typedef __int32 int32;
typedef unsigned __int32 uint32;
typedef __int64 int64;
typedef unsigned __int64 uint64;
#else
# error Do not know how to define a 32-bit integer quantity on your system
#endif
} // namespace GFLAGS_NAMESPACE
namespace fLS {
// The meaning of "string" might be different between now and when the
// macros below get invoked (e.g., if someone is experimenting with
// other string implementations that get defined after this file is
// included). Save the current meaning now and use it in the macros.
typedef std::string clstring;
} // namespace fLS
#define DECLARE_VARIABLE(type, shorttype, name) \
/* We always want to import declared variables, dll or no */ \
namespace fL##shorttype { extern GFLAGS_DLL_DECLARE_FLAG type FLAGS_##name; } \
using fL##shorttype::FLAGS_##name
#define DECLARE_bool(name) \
DECLARE_VARIABLE(bool, B, name)
#define DECLARE_int32(name) \
DECLARE_VARIABLE(::GFLAGS_NAMESPACE::int32, I, name)
#define DECLARE_uint32(name) \
DECLARE_VARIABLE(::GFLAGS_NAMESPACE::uint32, U, name)
#define DECLARE_int64(name) \
DECLARE_VARIABLE(::GFLAGS_NAMESPACE::int64, I64, name)
#define DECLARE_uint64(name) \
DECLARE_VARIABLE(::GFLAGS_NAMESPACE::uint64, U64, name)
#define DECLARE_double(name) \
DECLARE_VARIABLE(double, D, name)
#define DECLARE_string(name) \
/* We always want to import declared variables, dll or no */ \
namespace fLS { \
extern GFLAGS_DLL_DECLARE_FLAG ::fLS::clstring& FLAGS_##name; \
} \
using fLS::FLAGS_##name
#endif // GFLAGS_DECLARE_H_

View File

@@ -0,0 +1,102 @@
// Copyright (c) 2014, Andreas Schuh
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
// Imports the gflags library symbols into an alternative/deprecated namespace.
#ifndef GFLAGS_GFLAGS_H_
# error The internal header gflags_gflags.h may only be included by gflags.h
#endif
#ifndef GFLAGS_NS_GFLAGS_H_
#define GFLAGS_NS_GFLAGS_H_
namespace gflags {
using GFLAGS_NAMESPACE::int32;
using GFLAGS_NAMESPACE::uint32;
using GFLAGS_NAMESPACE::int64;
using GFLAGS_NAMESPACE::uint64;
using GFLAGS_NAMESPACE::RegisterFlagValidator;
using GFLAGS_NAMESPACE::CommandLineFlagInfo;
using GFLAGS_NAMESPACE::GetAllFlags;
using GFLAGS_NAMESPACE::ShowUsageWithFlags;
using GFLAGS_NAMESPACE::ShowUsageWithFlagsRestrict;
using GFLAGS_NAMESPACE::DescribeOneFlag;
using GFLAGS_NAMESPACE::SetArgv;
using GFLAGS_NAMESPACE::GetArgvs;
using GFLAGS_NAMESPACE::GetArgv;
using GFLAGS_NAMESPACE::GetArgv0;
using GFLAGS_NAMESPACE::GetArgvSum;
using GFLAGS_NAMESPACE::ProgramInvocationName;
using GFLAGS_NAMESPACE::ProgramInvocationShortName;
using GFLAGS_NAMESPACE::ProgramUsage;
using GFLAGS_NAMESPACE::VersionString;
using GFLAGS_NAMESPACE::GetCommandLineOption;
using GFLAGS_NAMESPACE::GetCommandLineFlagInfo;
using GFLAGS_NAMESPACE::GetCommandLineFlagInfoOrDie;
using GFLAGS_NAMESPACE::FlagSettingMode;
using GFLAGS_NAMESPACE::SET_FLAGS_VALUE;
using GFLAGS_NAMESPACE::SET_FLAG_IF_DEFAULT;
using GFLAGS_NAMESPACE::SET_FLAGS_DEFAULT;
using GFLAGS_NAMESPACE::SetCommandLineOption;
using GFLAGS_NAMESPACE::SetCommandLineOptionWithMode;
using GFLAGS_NAMESPACE::FlagSaver;
using GFLAGS_NAMESPACE::CommandlineFlagsIntoString;
using GFLAGS_NAMESPACE::ReadFlagsFromString;
using GFLAGS_NAMESPACE::AppendFlagsIntoFile;
using GFLAGS_NAMESPACE::ReadFromFlagsFile;
using GFLAGS_NAMESPACE::BoolFromEnv;
using GFLAGS_NAMESPACE::Int32FromEnv;
using GFLAGS_NAMESPACE::Uint32FromEnv;
using GFLAGS_NAMESPACE::Int64FromEnv;
using GFLAGS_NAMESPACE::Uint64FromEnv;
using GFLAGS_NAMESPACE::DoubleFromEnv;
using GFLAGS_NAMESPACE::StringFromEnv;
using GFLAGS_NAMESPACE::SetUsageMessage;
using GFLAGS_NAMESPACE::SetVersionString;
using GFLAGS_NAMESPACE::ParseCommandLineNonHelpFlags;
using GFLAGS_NAMESPACE::HandleCommandLineHelpFlags;
using GFLAGS_NAMESPACE::AllowCommandLineReparsing;
using GFLAGS_NAMESPACE::ReparseCommandLineNonHelpFlags;
using GFLAGS_NAMESPACE::ShutDownCommandLineFlags;
using GFLAGS_NAMESPACE::FlagRegisterer;
#ifndef SWIG
using GFLAGS_NAMESPACE::ParseCommandLineFlags;
#endif
} // namespace gflags
#endif // GFLAGS_NS_GFLAGS_H_

View File

@@ -0,0 +1,94 @@
/*
// Copyright (C) 2021-2024 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.
*/
#pragma once
#include <stddef.h>
#include <memory>
#include <vector>
#include <opencv2/core.hpp>
struct Peak {
explicit Peak(const cv::Point2f& keypoint = cv::Point2f(-1, -1), const float score = 0.0f, const float tag = 0.0f)
: keypoint(keypoint),
score(score),
tag(tag) {}
cv::Point2f keypoint;
float score;
float tag;
};
class Pose {
public:
explicit Pose(size_t numJoints) : peaks(numJoints) {}
void add(size_t index, Peak peak) {
peaks[index] = peak;
sum += peak.score;
poseTag = poseTag * static_cast<float>(validPointsNum) + peak.tag;
poseCenter = poseCenter * static_cast<float>(validPointsNum) + peak.keypoint;
validPointsNum += 1;
poseTag = poseTag / static_cast<float>(validPointsNum);
poseCenter = poseCenter / static_cast<float>(validPointsNum);
}
float getPoseTag() const {
return poseTag;
}
float getMeanScore() const {
return sum / static_cast<float>(size());
}
Peak& getPeak(size_t index) {
return peaks[index];
}
cv::Point2f& getPoseCenter() {
return poseCenter;
}
size_t size() const {
return peaks.size();
}
private:
std::vector<Peak> peaks;
cv::Point2f poseCenter = cv::Point2f(0.f, 0.f);
int validPointsNum = 0;
float poseTag = 0;
float sum = 0;
};
void findPeaks(const std::vector<cv::Mat>& nmsHeatMaps,
const std::vector<cv::Mat>& aembdsMaps,
std::vector<std::vector<Peak>>& allPeaks,
size_t jointId,
size_t maxNumPeople,
float detectionThreshold);
std::vector<Pose> matchByTag(std::vector<std::vector<Peak>>& allPeaks,
size_t maxNumPeople,
size_t numJoints,
float tagThreshold);
void adjustAndRefine(std::vector<Pose>& allPoses,
const std::vector<cv::Mat>& heatMaps,
const std::vector<cv::Mat>& aembdsMaps,
int poseId,
float delta);

View File

@@ -0,0 +1,57 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <stddef.h>
#include <memory>
#include <string>
#include <vector>
#include "models/image_model.h"
namespace ov {
class Model;
} // namespace ov
struct InferenceResult;
struct ResultBase;
class ClassificationModel : public ImageModel {
public:
/// Constructor
/// @param modelFileName name of model to load.
/// @param nTop - number of top results.
/// Any detected object with confidence lower than this threshold will be ignored.
/// @param useAutoResize - if true, image will be resized by openvino.
/// Otherwise, image will be preprocessed and resized using OpenCV routines.
/// @param labels - array of labels for every class.
/// @param layout - model input layout
ClassificationModel(const std::string& modelFileName,
size_t nTop,
bool useAutoResize,
const std::vector<std::string>& labels,
const std::string& layout = "");
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
static std::vector<std::string> loadLabels(const std::string& labelFilename);
protected:
size_t nTop;
std::vector<std::string> labels;
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
};

View File

@@ -0,0 +1,52 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <stddef.h>
#include <string>
#include <vector>
#include "models/image_model.h"
class DetectionModel : public ImageModel {
public:
/// Constructor
/// @param modelFileName name of model to load
/// @param confidenceThreshold - threshold to eliminate low-confidence detections.
/// Any detected object with confidence lower than this threshold will be ignored.
/// @param useAutoResize - if true, image will be resized by openvino.
/// Otherwise, image will be preprocessed and resized using OpenCV routines.
/// @param labels - array of labels for every class. If this array is empty or contains less elements
/// than actual classes number, default "Label #N" will be shown for missing items.
/// @param layout - model input layout
DetectionModel();
DetectionModel(const std::string& modelFileName,
float confidenceThreshold,
bool useAutoResize,
const std::vector<std::string>& labels,
const std::string& layout = "");
static std::vector<std::string> loadLabels(const std::string& labelFilename);
protected:
float confidenceThreshold;
std::vector<std::string> labels;
std::string getLabelName(size_t labelID) {
return labelID < labels.size() ? labels[labelID] : std::string("Label #") + std::to_string(labelID);
}
};

View File

@@ -0,0 +1,59 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
#include "models/detection_model.h"
namespace ov {
class InferRequest;
class Model;
} // namespace ov
struct InferenceResult;
struct InputData;
struct InternalModelData;
struct ResultBase;
class ModelCenterNet : public DetectionModel {
public:
struct BBox {
float left;
float top;
float right;
float bottom;
float getWidth() const {
return (right - left) + 1.0f;
}
float getHeight() const {
return (bottom - top) + 1.0f;
}
};
static const int INIT_VECTOR_SIZE = 200;
ModelCenterNet(const std::string& modelFileName,
float confidenceThreshold,
const std::vector<std::string>& labels = std::vector<std::string>(),
const std::string& layout = "");
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
protected:
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
};

View File

@@ -0,0 +1,55 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <stddef.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <utils/nms.hpp>
#include "models/detection_model.h"
namespace ov {
class Model;
} // namespace ov
struct InferenceResult;
struct ResultBase;
class ModelFaceBoxes : public DetectionModel {
public:
static const int INIT_VECTOR_SIZE = 200;
ModelFaceBoxes(const std::string& modelFileName,
float confidenceThreshold,
bool useAutoResize,
float boxIOUThreshold,
const std::string& layout = "");
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
protected:
size_t maxProposalsCount;
const float boxIOUThreshold;
const std::vector<float> variance;
const std::vector<int> steps;
const std::vector<std::vector<int>> minSizes;
std::vector<Anchor> anchors;
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
void priorBoxes(const std::vector<std::pair<size_t, size_t>>& featureMaps);
};

View File

@@ -0,0 +1,74 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <utils/nms.hpp>
#include "models/detection_model.h"
namespace ov {
class Model;
} // namespace ov
struct InferenceResult;
struct ResultBase;
class ModelRetinaFace : public DetectionModel {
public:
static const int LANDMARKS_NUM = 5;
static const int INIT_VECTOR_SIZE = 200;
/// Loads model and performs required initialization
/// @param model_name name of model to load
/// @param confidenceThreshold - threshold to eliminate low-confidence detections.
/// Any detected object with confidence lower than this threshold will be ignored.
/// @param useAutoResize - if true, image will be resized by openvino.
/// @param boxIOUThreshold - threshold for NMS boxes filtering, varies in [0.0, 1.0] range.
/// @param layout - model input layout
ModelRetinaFace(const std::string& model_name,
float confidenceThreshold,
bool useAutoResize,
float boxIOUThreshold,
const std::string& layout = "");
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
protected:
struct AnchorCfgLine {
int stride;
std::vector<int> scales;
int baseSize;
std::vector<int> ratios;
};
bool shouldDetectMasks;
bool shouldDetectLandmarks;
const float boxIOUThreshold;
const float maskThreshold;
float landmarkStd;
enum OutputType { OUT_BOXES, OUT_SCORES, OUT_LANDMARKS, OUT_MASKSCORES, OUT_MAX };
std::vector<std::string> separateOutputsNames[OUT_MAX];
const std::vector<AnchorCfgLine> anchorCfg;
std::map<int, std::vector<Anchor>> anchorsFpn;
std::vector<std::vector<Anchor>> anchors;
void generateAnchorsFpn();
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
};

View File

@@ -0,0 +1,81 @@
/*
// Copyright (C) 2021-2024 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.
*/
#pragma once
#include <stddef.h>
#include <memory>
#include <string>
#include <vector>
#include <opencv2/core/types.hpp>
#include <utils/nms.hpp>
#include "models/detection_model.h"
namespace ov {
class Model;
class Tensor;
} // namespace ov
struct InferenceResult;
struct ResultBase;
class ModelRetinaFacePT : public DetectionModel {
public:
struct Box {
float cX;
float cY;
float width;
float height;
};
/// Loads model and performs required initialization
/// @param model_name name of model to load
/// @param confidenceThreshold - threshold to eliminate low-confidence detections.
/// Any detected object with confidence lower than this threshold will be ignored.
/// @param useAutoResize - if true, image will be resized by openvino.
/// @param boxIOUThreshold - threshold for NMS boxes filtering, varies in [0.0, 1.0] range.
/// @param layout - model input layout
ModelRetinaFacePT(const std::string& modelFileName,
float confidenceThreshold,
bool useAutoResize,
float boxIOUThreshold,
const std::string& layout = "");
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
protected:
size_t landmarksNum;
const float boxIOUThreshold;
float variance[2] = {0.1f, 0.2f};
enum OutputType { OUT_BOXES, OUT_SCORES, OUT_LANDMARKS, OUT_MAX };
std::vector<ModelRetinaFacePT::Box> priors;
std::vector<size_t> filterByScore(const ov::Tensor& scoresTensor, const float confidenceThreshold);
std::vector<float> getFilteredScores(const ov::Tensor& scoresTensor, const std::vector<size_t>& indicies);
std::vector<cv::Point2f> getFilteredLandmarks(const ov::Tensor& landmarksTensor,
const std::vector<size_t>& indicies,
int imgWidth,
int imgHeight);
std::vector<ModelRetinaFacePT::Box> generatePriorData();
std::vector<Anchor> getFilteredProposals(const ov::Tensor& boxesTensor,
const std::vector<size_t>& indicies,
int imgWidth,
int imgHeight);
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
};

View File

@@ -0,0 +1,73 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <stddef.h>
#include <memory>
#include <string>
#include <vector>
#include "models/detection_model.h"
namespace ov {
class InferRequest;
class Model;
} // namespace ov
struct InferenceResult;
struct InputData;
struct InternalModelData;
struct ResultBase;
class ModelSSD : public DetectionModel {
public:
ModelSSD();
/// Constructor
/// @param modelFileName name of model to load
/// @param confidenceThreshold - threshold to eliminate low-confidence detections.
/// Any detected object with confidence lower than this threshold will be ignored.
/// @param useAutoResize - if true, image will be resized by openvino.
/// Otherwise, image will be preprocessed and resized using OpenCV routines.
/// @param labels - array of labels for every class. If this array is empty or contains less elements
/// than actual classes number, default "Label #N" will be shown for missing items.
/// @param layout - model input layout
ModelSSD(const std::string& modelFileName,
float confidenceThreshold,
bool useAutoResize,
const std::vector<std::string>& labels = std::vector<std::string>(),
const std::string& layout = "");
void Initialise(const std::string& modelFileName,
float _confidenceThreshold,
bool useAutoResize,
const std::vector<std::string>& _labels = std::vector<std::string>(),
const std::string& layout = "")
{
ImageModel::Initalise(modelFileName, useAutoResize, layout),
confidenceThreshold = confidenceThreshold;
labels = _labels;
}
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
protected:
std::unique_ptr<ResultBase> postprocessSingleOutput(InferenceResult& infResult);
std::unique_ptr<ResultBase> postprocessMultipleOutputs(InferenceResult& infResult);
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
void prepareSingleOutput(std::shared_ptr<ov::Model>& model);
void prepareMultipleOutputs(std::shared_ptr<ov::Model>& model);
size_t objectSize = 0;
size_t detectionsNumId = 0;
};

View File

@@ -0,0 +1,107 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <openvino/op/region_yolo.hpp>
#include <openvino/openvino.hpp>
#include "models/detection_model.h"
struct DetectedObject;
struct InferenceResult;
struct ResultBase;
class ModelYolo : public DetectionModel {
protected:
class Region {
public:
int num = 0;
size_t classes = 0;
int coords = 0;
std::vector<float> anchors;
size_t outputWidth = 0;
size_t outputHeight = 0;
Region(const std::shared_ptr<ov::op::v0::RegionYolo>& regionYolo);
Region(size_t classes,
int coords,
const std::vector<float>& anchors,
const std::vector<int64_t>& masks,
size_t outputWidth,
size_t outputHeight);
};
public:
enum YoloVersion { YOLO_V1V2, YOLO_V3, YOLO_V4, YOLO_V4_TINY, YOLOF };
/// Constructor.
/// @param modelFileName name of model to load
/// @param confidenceThreshold - threshold to eliminate low-confidence detections.
/// Any detected object with confidence lower than this threshold will be ignored.
/// @param useAutoResize - if true, image will be resized by openvino.
/// Otherwise, image will be preprocessed and resized using OpenCV routines.
/// @param useAdvancedPostprocessing - if true, an advanced algorithm for filtering/postprocessing will be used
/// (with better processing of multiple crossing objects). Otherwise, classic algorithm will be used.
/// @param boxIOUThreshold - threshold to treat separate output regions as one object for filtering
/// during postprocessing (only one of them should stay). The default value is 0.5
/// @param labels - array of labels for every class. If this array is empty or contains less elements
/// than actual classes number, default "Label #N" will be shown for missing items.
/// @param anchors - vector of anchors coordinates. Required for YOLOv4, for other versions it may be omitted.
/// @param masks - vector of masks values. Required for YOLOv4, for other versions it may be omitted.
/// @param layout - model input layout
ModelYolo(const std::string& modelFileName,
float confidenceThreshold,
bool useAutoResize,
bool useAdvancedPostprocessing = true,
float boxIOUThreshold = 0.5,
const std::vector<std::string>& labels = std::vector<std::string>(),
const std::vector<float>& anchors = std::vector<float>(),
const std::vector<int64_t>& masks = std::vector<int64_t>(),
const std::string& layout = "");
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
protected:
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
void parseYOLOOutput(const std::string& output_name,
const ov::Tensor& tensor,
const unsigned long resized_im_h,
const unsigned long resized_im_w,
const unsigned long original_im_h,
const unsigned long original_im_w,
std::vector<DetectedObject>& objects);
static int calculateEntryIndex(int entriesNum, int lcoords, size_t lclasses, int location, int entry);
static double intersectionOverUnion(const DetectedObject& o1, const DetectedObject& o2);
std::map<std::string, Region> regions;
double boxIOUThreshold;
bool useAdvancedPostprocessing;
bool isObjConf = 1;
YoloVersion yoloVersion;
const std::vector<float> presetAnchors;
const std::vector<int64_t> presetMasks;
ov::Layout yoloRegionLayout = "NCHW";
};

View File

@@ -0,0 +1,50 @@
/*
// Copyright (C) 2022-2024 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.
*/
#pragma once
#include <string>
#include <vector>
#include <openvino/openvino.hpp>
#include "models/detection_model.h"
class ModelYoloV3ONNX: public DetectionModel {
public:
/// Constructor.
/// @param modelFileName name of model to load
/// @param confidenceThreshold - threshold to eliminate low-confidence detections.
/// Any detected object with confidence lower than this threshold will be ignored.
/// @param labels - array of labels for every class. If this array is empty or contains less elements
/// than actual classes number, default "Label #N" will be shown for missing items.
/// @param layout - model input layout
ModelYoloV3ONNX(const std::string& modelFileName,
float confidenceThreshold,
const std::vector<std::string>& labels = std::vector<std::string>(),
const std::string& layout = "");
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
protected:
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
std::string boxesOutputName;
std::string scoresOutputName;
std::string indicesOutputName;
static const int numberOfClasses = 80;
};

View File

@@ -0,0 +1,54 @@
/*
// Copyright (C) 2022-2024 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.
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <openvino/openvino.hpp>
#include "models/detection_model.h"
class ModelYoloX: public DetectionModel {
public:
/// Constructor.
/// @param modelFileName name of model to load
/// @param confidenceThreshold - threshold to eliminate low-confidence detections.
/// Any detected object with confidence lower than this threshold will be ignored.
/// @param boxIOUThreshold - threshold to treat separate output regions as one object for filtering
/// during postprocessing (only one of them should stay). The default value is 0.5
/// @param labels - array of labels for every class. If this array is empty or contains less elements
/// than actual classes number, default "Label #N" will be shown for missing items.
/// @param layout - model input layout
ModelYoloX(const std::string& modelFileName,
float confidenceThreshold,
float boxIOUThreshold = 0.5,
const std::vector<std::string>& labels = std::vector<std::string>(),
const std::string& layout = "");
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
protected:
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
void setStridesGrids();
float boxIOUThreshold;
std::vector<std::pair<size_t, size_t>> grids;
std::vector<size_t> expandedStrides;
static const size_t numberOfClasses = 80;
};

View File

@@ -0,0 +1,89 @@
/*
// Copyright (C) 2021-2024 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.
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <opencv2/core.hpp>
#include <utils/image_utils.h>
#include "models/image_model.h"
namespace ov {
class InferRequest;
class Model;
class Shape;
} // namespace ov
struct HumanPose;
struct InferenceResult;
struct InputData;
struct InternalModelData;
struct ResultBase;
class HpeAssociativeEmbedding : public ImageModel {
public:
/// Constructor
/// @param modelFileName name of model to load
/// @param aspectRatio - the ratio of input width to its height.
/// @param targetSize - the length of a short image side used for model reshaping.
/// @param confidenceThreshold - threshold to eliminate low-confidence poses.
/// Any pose with confidence lower than this threshold will be ignored.
/// @param layout - model input layout
HpeAssociativeEmbedding(const std::string& modelFileName,
double aspectRatio,
int targetSize,
float confidenceThreshold,
const std::string& layout = "",
float delta = 0.0,
RESIZE_MODE resizeMode = RESIZE_KEEP_ASPECT);
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
protected:
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
cv::Size inputLayerSize;
double aspectRatio;
int targetSize;
float confidenceThreshold;
float delta;
std::string embeddingsTensorName;
std::string heatmapsTensorName;
std::string nmsHeatmapsTensorName;
static const int numJoints = 17;
static const int stride = 32;
static const int maxNumPeople = 30;
static const cv::Vec3f meanPixel;
static const float detectionThreshold;
static const float tagThreshold;
void changeInputSize(std::shared_ptr<ov::Model>& model);
std::string findTensorByName(const std::string& tensorName, const std::vector<std::string>& outputsNames);
std::vector<cv::Mat> split(float* data, const ov::Shape& shape);
std::vector<HumanPose> extractPoses(std::vector<cv::Mat>& heatMaps,
const std::vector<cv::Mat>& aembdsMaps,
const std::vector<cv::Mat>& nmsHeatMaps) const;
};

View File

@@ -0,0 +1,78 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <stddef.h>
#include <memory>
#include <string>
#include <vector>
#include <opencv2/core.hpp>
#include "models/image_model.h"
namespace ov {
class InferRequest;
class Model;
} // namespace ov
struct HumanPose;
struct InferenceResult;
struct InputData;
struct InternalModelData;
struct ResultBase;
class HPEOpenPose : public ImageModel {
public:
/// Constructor
/// @param modelFileName name of model to load
/// @param aspectRatio - the ratio of input width to its height.
/// @param targetSize - the height used for model reshaping.
/// @param confidenceThreshold - threshold to eliminate low-confidence keypoints.
/// @param layout - model input layout
HPEOpenPose(const std::string& modelFileName,
double aspectRatio,
int targetSize,
float confidenceThreshold,
const std::string& layout = "");
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
static const size_t keypointsNumber = 18;
protected:
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
static const int minJointsNumber = 3;
static const int stride = 8;
static const int upsampleRatio = 4;
static const cv::Vec3f meanPixel;
static const float minPeaksDistance;
static const float midPointsScoreThreshold;
static const float foundMidPointsRatioThreshold;
static const float minSubsetScore;
cv::Size inputLayerSize;
double aspectRatio;
int targetSize;
float confidenceThreshold;
std::vector<HumanPose> extractPoses(const std::vector<cv::Mat>& heatMaps, const std::vector<cv::Mat>& pafs) const;
void resizeFeatureMaps(std::vector<cv::Mat>& featureMaps) const;
void UpdateAspectRatio(double aRatio) { aspectRatio = aRatio; }
void changeInputSize(std::shared_ptr<ov::Model>& model);
};

View File

@@ -0,0 +1,58 @@
/*
// Copyright (C) 2021-2024 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.
*/
#pragma once
#include <stddef.h>
#include <memory>
#include <string>
#include "models/model_base.h"
#include "utils/image_utils.h"
namespace ov {
class InferRequest;
} // namespace ov
struct InputData;
struct InternalModelData;
class ImageModel : public ModelBase {
public:
/// Constructor
/// @param modelFileName name of model to load
/// @param useAutoResize - if true, image is resized by openvino
/// @param layout - model input layout
ImageModel();
ImageModel(const std::string& modelFileName, bool useAutoResize, const std::string& layout = "");
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
void updateImageSize(const cv::Size& inputImgSize) override {
netInputHeight = inputImgSize.height;
netInputWidth = inputImgSize.width;
}
void Initalise(const std::string& _modelFileName, bool _useAutoResize, const std::string& _layout = "") {
useAutoResize = _useAutoResize;
ModelBase::Initilise(modelFileName, _layout);
}
protected:
bool useAutoResize;
size_t netInputHeight = 0;
size_t netInputWidth = 0;
cv::InterpolationFlags interpolationMode = cv::INTER_LINEAR;
RESIZE_MODE resizeMode = RESIZE_FILL;
};

View File

@@ -0,0 +1,41 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <opencv2/opencv.hpp>
struct InputData {
virtual ~InputData() {}
template <class T>
T& asRef() {
return dynamic_cast<T&>(*this);
}
template <class T>
const T& asRef() const {
return dynamic_cast<const T&>(*this);
}
};
struct ImageInputData : public InputData {
cv::Mat inputImage;
ImageInputData() {}
ImageInputData(const cv::Mat& img) {
inputImage = img;
}
};

View File

@@ -0,0 +1,48 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
struct InternalModelData {
virtual ~InternalModelData() {}
template <class T>
T& asRef() {
return dynamic_cast<T&>(*this);
}
template <class T>
const T& asRef() const {
return dynamic_cast<const T&>(*this);
}
};
struct InternalImageModelData : public InternalModelData {
InternalImageModelData(int width, int height) : inputImgWidth(width), inputImgHeight(height) {}
int inputImgWidth;
int inputImgHeight;
};
struct InternalScaleData : public InternalImageModelData {
InternalScaleData(int width, int height, float scaleX, float scaleY)
: InternalImageModelData(width, height),
scaleX(scaleX),
scaleY(scaleY) {}
float scaleX;
float scaleY;
};

View File

@@ -0,0 +1,55 @@
/*
// Copyright (C) 2021-2024 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 writingb 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.
*/
#pragma once
#include <memory>
#include <string>
#include <opencv2/core/types.hpp>
#include "models/image_model.h"
namespace ov {
class InferRequest;
class Model;
} // namespace ov
struct InferenceResult;
struct InputData;
struct InternalModelData;
struct ResultBase;
class JPEGRestorationModel : public ImageModel {
public:
/// Constructor
/// @param modelFileName name of model to load
/// @param inputImgSize size of image to set model input shape
/// @param jpegCompression flag allows to perform compression before the inference
/// @param layout - model input layout
JPEGRestorationModel(const std::string& modelFileName,
const cv::Size& inputImgSize,
bool jpegCompression,
const std::string& layout = "");
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
protected:
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
void changeInputSize(std::shared_ptr<ov::Model>& model);
static const size_t stride = 8;
bool jpegCompression = false;
};

View File

@@ -0,0 +1,87 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <openvino/openvino.hpp>
#include <utils/args_helper.hpp>
#include <utils/config_factory.h>
#include <utils/ocv_common.hpp>
struct InferenceResult;
struct InputData;
struct InternalModelData;
struct ResultBase;
class ModelBase {
public:
ModelBase() {};
ModelBase(const std::string& modelFileName, const std::string& layout = "")
: modelFileName(modelFileName),
inputsLayouts(parseLayoutString(layout)) {}
virtual ~ModelBase() {}
virtual std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) = 0;
// Virtual method to be overridden in derived classes
virtual void updateImageSize(const cv::Size& inputImgSize) {
// Optionally leave empty or add any base logic here
}
virtual ov::CompiledModel compileModel(const ModelConfig& config, ov::Core& core);
virtual void onLoadCompleted(const std::vector<ov::InferRequest>& requests) {}
virtual std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) = 0;
void Initilise(const std::string& _modelFileName, const std::string& _layout = "") {
modelFileName = _modelFileName;
inputsLayouts = parseLayoutString(_layout);
}
const std::vector<std::string>& getOutputsNames() const {
return outputsNames;
}
const std::vector<std::string>& getInputsNames() const {
return inputsNames;
}
std::string getModelFileName() {
return modelFileName;
}
void setInputsPreprocessing(bool reverseInputChannels,
const std::string& meanValues,
const std::string& scaleValues) {
this->inputTransform = InputTransform(reverseInputChannels, meanValues, scaleValues);
}
protected:
virtual void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) = 0;
virtual void setBatch(std::shared_ptr<ov::Model>& model);
std::shared_ptr<ov::Model> prepareModel(ov::Core& core);
InputTransform inputTransform = InputTransform();
std::vector<std::string> inputsNames;
std::vector<std::string> outputsNames;
ov::CompiledModel compiledModel;
std::string modelFileName;
ModelConfig config = {};
std::map<std::string, ov::Layout> inputsLayouts;
ov::Layout getInputLayout(const ov::Output<ov::Node>& input);
};

View File

@@ -0,0 +1,62 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <stddef.h>
#include <vector>
#include <opencv2/core.hpp>
struct HumanPose;
struct Peak {
Peak(const int id = -1, const cv::Point2f& pos = cv::Point2f(), const float score = 0.0f);
int id;
cv::Point2f pos;
float score;
};
struct HumanPoseByPeaksIndices {
explicit HumanPoseByPeaksIndices(const int keypointsNumber);
std::vector<int> peaksIndices;
int nJoints;
float score;
};
struct TwoJointsConnection {
TwoJointsConnection(const int firstJointIdx, const int secondJointIdx, const float score);
int firstJointIdx;
int secondJointIdx;
float score;
};
void findPeaks(const std::vector<cv::Mat>& heatMaps,
const float minPeaksDistance,
std::vector<std::vector<Peak>>& allPeaks,
int heatMapId,
float confidenceThreshold);
std::vector<HumanPose> groupPeaksToPoses(const std::vector<std::vector<Peak>>& allPeaks,
const std::vector<cv::Mat>& pafs,
const size_t keypointsNumber,
const float midPointsScoreThreshold,
const float foundMidPointsRatioThreshold,
const int minJointsNumber,
const float minSubsetScore);

View File

@@ -0,0 +1,122 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <opencv2/core.hpp>
#include <openvino/openvino.hpp>
#include "internal_model_data.h"
struct MetaData;
struct ResultBase {
ResultBase(int64_t frameId = -1, const std::shared_ptr<MetaData>& metaData = nullptr)
: frameId(frameId),
metaData(metaData) {}
virtual ~ResultBase() {}
int64_t frameId;
std::shared_ptr<MetaData> metaData;
bool IsEmpty() {
return frameId < 0;
}
template <class T>
T& asRef() {
return dynamic_cast<T&>(*this);
}
template <class T>
const T& asRef() const {
return dynamic_cast<const T&>(*this);
}
};
struct InferenceResult : public ResultBase {
std::shared_ptr<InternalModelData> internalModelData;
std::map<std::string, ov::Tensor> outputsData;
/// Returns the first output tensor
/// This function is a useful addition to direct access to outputs list as many models have only one output
/// @returns first output tensor
ov::Tensor getFirstOutputTensor() {
if (outputsData.empty()) {
throw std::out_of_range("Outputs map is empty.");
}
return outputsData.begin()->second;
}
/// Returns true if object contains no valid data
/// @returns true if object contains no valid data
bool IsEmpty() {
return outputsData.empty();
}
};
struct ClassificationResult : public ResultBase {
ClassificationResult(int64_t frameId = -1, const std::shared_ptr<MetaData>& metaData = nullptr)
: ResultBase(frameId, metaData) {}
struct Classification {
unsigned int id;
std::string label;
float score;
Classification(unsigned int id, const std::string& label, float score) : id(id), label(label), score(score) {}
};
std::vector<Classification> topLabels;
};
struct DetectedObject : public cv::Rect2f {
size_t labelID;
std::string label;
float confidence;
};
struct DetectionResult : public ResultBase {
DetectionResult(int64_t frameId = -1, const std::shared_ptr<MetaData>& metaData = nullptr)
: ResultBase(frameId, metaData) {}
std::vector<DetectedObject> objects;
};
struct RetinaFaceDetectionResult : public DetectionResult {
RetinaFaceDetectionResult(int64_t frameId = -1, const std::shared_ptr<MetaData>& metaData = nullptr)
: DetectionResult(frameId, metaData) {}
std::vector<cv::Point2f> landmarks;
};
struct ImageResult : public ResultBase {
ImageResult(int64_t frameId = -1, const std::shared_ptr<MetaData>& metaData = nullptr)
: ResultBase(frameId, metaData) {}
cv::Mat resultImage;
};
struct HumanPose {
std::vector<cv::Point2f> keypoints;
float score;
};
struct HumanPoseResult : public ResultBase {
HumanPoseResult(int64_t frameId = -1, const std::shared_ptr<MetaData>& metaData = nullptr)
: ResultBase(frameId, metaData) {}
std::vector<HumanPose> poses;
};

View File

@@ -0,0 +1,50 @@
/*
// Copyright (C) 2020-2024 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 writingb 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.
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
#include "models/image_model.h"
namespace ov {
class Model;
} // namespace ov
struct InferenceResult;
struct ResultBase;
#pragma once
class SegmentationModel : public ImageModel {
public:
/// Constructor
/// @param modelFileName name of model to load
/// @param useAutoResize - if true, image will be resized by openvino.
/// Otherwise, image will be preprocessed and resized using OpenCV routines.
/// @param layout - model input layout
SegmentationModel(const std::string& modelFileName, bool useAutoResize, const std::string& layout = "");
static std::vector<std::string> loadLabels(const std::string& labelFilename);
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
protected:
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
int outHeight = 0;
int outWidth = 0;
int outChannels = 0;
};

View File

@@ -0,0 +1,43 @@
/*
// Copyright (C) 2021-2024 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 writingb 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.
*/
#pragma once
#include <memory>
#include <string>
#include "models/image_model.h"
namespace ov {
class InferRequest;
class Model;
} // namespace ov
struct InferenceResult;
struct InputData;
struct InternalModelData;
struct ResultBase;
class StyleTransferModel : public ImageModel {
public:
/// Constructor
/// @param modelFileName name of model to load
/// @param layout - model input layout
StyleTransferModel(const std::string& modelFileName, const std::string& layout = "");
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
protected:
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
};

View File

@@ -0,0 +1,59 @@
/*
// Copyright (C) 2021-2024 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 writingb 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.
*/
#pragma once
#include <memory>
#include <string>
#include <opencv2/core/types.hpp>
#include "models/image_model.h"
namespace ov {
class InferRequest;
class Model;
} // namespace ov
struct InferenceResult;
struct InputData;
struct InternalModelData;
struct ResultBase;
class SuperResolutionModel : public ImageModel {
public:
/// Constructor
/// @param modelFileName name of model to load
/// @param layout - model input layout
SuperResolutionModel(const std::string& modelFileName,
const cv::Size& inputImgSize,
const std::string& layout = "");
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
protected:
void changeInputSize(std::shared_ptr<ov::Model>& model, int coeff);
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
};
class SuperResolutionChannelJoint : public SuperResolutionModel {
public:
using SuperResolutionModel::SuperResolutionModel;
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
void setBatch(std::shared_ptr<ov::Model>& model) override;
};

View File

@@ -0,0 +1,28 @@
// Copyright (C) 2019-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <deque>
#include <memory>
#include <vector>
class CpuMonitor {
public:
CpuMonitor();
~CpuMonitor();
void setHistorySize(std::size_t size);
std::size_t getHistorySize() const;
void collectData();
std::deque<std::vector<double>> getLastHistory() const;
std::vector<double> getMeanCpuLoad() const;
private:
unsigned samplesNumber;
unsigned historySize;
std::vector<double> cpuLoadSum;
std::deque<std::vector<double>> cpuLoadHistory;
class PerformanceCounter;
std::unique_ptr<PerformanceCounter> performanceCounter;
};

View File

@@ -0,0 +1,34 @@
// Copyright (C) 2019-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <deque>
#include <memory>
class MemoryMonitor {
public:
MemoryMonitor();
~MemoryMonitor();
void setHistorySize(std::size_t size);
std::size_t getHistorySize() const;
void collectData();
std::deque<std::pair<double, double>> getLastHistory() const;
double getMeanMem() const; // in GiB
double getMeanSwap() const;
double getMaxMem() const;
double getMaxSwap() const;
double getMemTotal() const;
double getMaxMemTotal() const; // a system may have hotpluggable memory
private:
unsigned samplesNumber;
std::size_t historySize;
double memSum, swapSum;
double maxMem, maxSwap;
double memTotal;
double maxMemTotal;
std::deque<std::pair<double, double>> memSwapUsageHistory;
class PerformanceCounter;
std::unique_ptr<PerformanceCounter> performanceCounter;
};

View File

@@ -0,0 +1,44 @@
// Copyright (C) 2019-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <chrono>
#include <map>
#include <ostream>
#include <set>
#include <opencv2/imgproc.hpp>
#include "cpu_monitor.h"
#include "memory_monitor.h"
enum class MonitorType{CpuAverage, DistributionCpu, Memory};
class Presenter {
public:
explicit Presenter(std::set<MonitorType> enabledMonitors = {},
int yPos = 20,
cv::Size graphSize = {150, 60},
std::size_t historySize = 20);
explicit Presenter(const std::string& keys,
int yPos = 20,
cv::Size graphSize = {150, 60},
std::size_t historySize = 20);
void addRemoveMonitor(MonitorType monitor);
void handleKey(int key); // handles C, D, M, H keys
void drawGraphs(cv::Mat& frame);
std::vector<std::string> reportMeans() const;
const int yPos;
const cv::Size graphSize;
const int graphPadding;
private:
std::chrono::steady_clock::time_point prevTimeStamp;
std::size_t historySize;
CpuMonitor cpuMonitor;
bool distributionCpuEnabled;
MemoryMonitor memoryMonitor;
std::ostringstream strStream;
};

View File

@@ -0,0 +1,123 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <stdint.h>
#include <condition_variable>
#include <exception>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <openvino/openvino.hpp>
#include <models/results.h>
#include <utils/performance_metrics.hpp>
#include "pipelines/requests_pool.h"
#include "models/model_base.h"
class ModelBase;
struct InputData;
struct MetaData;
struct ModelConfig;
/// This is base class for asynchronous pipeline
/// Derived classes should add functions for data submission and output processing
class AsyncPipeline {
public:
/// Loads model and performs required initialization
/// @param modelInstance pointer to model object. Object it points to should not be destroyed manually after passing
/// pointer to this function.
/// @param config - fine tuning configuration for model
/// @param core - reference to ov::Core instance to use.
/// If it is omitted, new instance of ov::Core will be created inside.
AsyncPipeline(std::unique_ptr<ModelBase>&& modelInstance, const ModelConfig& config, ov::Core& core);
virtual ~AsyncPipeline();
/// Waits until either output data becomes available or pipeline allows to submit more input data.
/// @param shouldKeepOrder if true, function will treat results as ready only if next sequential result (frame) is
/// ready (so results can be extracted in the same order as they were submitted). Otherwise, function will return if
/// any result is ready.
void waitForData(bool shouldKeepOrder = true);
/// @returns true if there's available infer requests in the pool
/// and next frame can be submitted for processing, false otherwise.
bool isReadyToProcess() {
return requestsPool->isIdleRequestAvailable();
}
/// Waits for all currently submitted requests to be completed.
///
void waitForTotalCompletion() {
if (requestsPool)
requestsPool->waitForTotalCompletion();
}
void updateModelImageSize(const cv::Size& inputImgSize) {
model->updateImageSize(inputImgSize);
}
/// Submits data to the model for inference
/// @param inputData - input data to be submitted
/// @param metaData - shared pointer to metadata container.
/// Might be null. This pointer will be passed through pipeline and put to the final result structure.
/// @returns -1 if image cannot be scheduled for processing (there's no free InferRequest available).
/// Otherwise returns unique sequential frame ID for this particular request. Same frame ID will be written in the
/// result structure.
virtual int64_t submitData(const InputData& inputData, const std::shared_ptr<MetaData>& metaData);
/// Gets available data from the queue
/// @param shouldKeepOrder if true, function will treat results as ready only if next sequential result (frame) is
/// ready (so results can be extracted in the same order as they were submitted). Otherwise, function will return if
/// any result is ready.
virtual std::unique_ptr<ResultBase> getResult(bool shouldKeepOrder = true);
PerformanceMetrics getInferenceMetircs() {
return inferenceMetrics;
}
PerformanceMetrics getPreprocessMetrics() {
return preprocessMetrics;
}
PerformanceMetrics getPostprocessMetrics() {
return postprocessMetrics;
}
protected:
/// Returns processed result, if available
/// @param shouldKeepOrder if true, function will return processed data sequentially,
/// keeping original frames order (as they were submitted). Otherwise, function will return processed data in random
/// order.
/// @returns InferenceResult with processed information or empty InferenceResult (with negative frameID) if there's
/// no any results yet.
virtual InferenceResult getInferenceResult(bool shouldKeepOrder);
std::unique_ptr<RequestsPool> requestsPool;
std::unordered_map<int64_t, InferenceResult> completedInferenceResults;
ov::CompiledModel compiledModel;
std::mutex mtx;
std::condition_variable condVar;
int64_t inputFrameId = 0;
int64_t outputFrameId = 0;
std::exception_ptr callbackException = nullptr;
std::unique_ptr<ModelBase> model;
PerformanceMetrics inferenceMetrics;
PerformanceMetrics preprocessMetrics;
PerformanceMetrics postprocessMetrics;
};

View File

@@ -0,0 +1,51 @@
/*
// Copyright (C) 2018-2024 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.
*/
#pragma once
#include <utils/ocv_common.hpp>
struct MetaData {
virtual ~MetaData() {}
template <class T>
T& asRef() {
return dynamic_cast<T&>(*this);
}
template <class T>
const T& asRef() const {
return dynamic_cast<const T&>(*this);
}
};
struct ImageMetaData : public MetaData {
cv::Mat img;
std::chrono::steady_clock::time_point timeStamp;
ImageMetaData() {}
ImageMetaData(cv::Mat img, std::chrono::steady_clock::time_point timeStamp) : img(img), timeStamp(timeStamp) {}
};
struct ClassificationImageMetaData : public ImageMetaData {
unsigned int groundTruthId;
ClassificationImageMetaData(cv::Mat img,
std::chrono::steady_clock::time_point timeStamp,
unsigned int groundTruthId)
: ImageMetaData(img, timeStamp),
groundTruthId(groundTruthId) {}
};

View File

@@ -0,0 +1,65 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <stddef.h>
#include <mutex>
#include <utility>
#include <vector>
#include <openvino/openvino.hpp>
/// This is class storing requests pool for asynchronous pipeline
///
class RequestsPool {
public:
RequestsPool(ov::CompiledModel& compiledModel, unsigned int size);
~RequestsPool();
/// Returns idle request from the pool. Returned request is automatically marked as In Use (this status will be
/// reset after request processing completion) This function is thread safe as long as request is used only until
/// setRequestIdle call
/// @returns pointer to request with idle state or nullptr if all requests are in use.
ov::InferRequest getIdleRequest();
/// Sets particular request to Idle state
/// This function is thread safe as long as request provided is not used after call to this function
/// @param request - request to be returned to idle state
void setRequestIdle(const ov::InferRequest& request);
/// Returns number of requests in use. This function is thread safe.
/// @returns number of requests in use
size_t getInUseRequestsCount();
/// Returns number of requests in use. This function is thread safe.
/// @returns number of requests in use
bool isIdleRequestAvailable();
/// Waits for completion of every non-idle requests in pool.
/// getIdleRequest should not be called together with this function or after it to avoid race condition or invalid
/// state
/// @returns number of requests in use
void waitForTotalCompletion();
/// Returns list of all infer requests in the pool.
/// @returns list of all infer requests in the pool.
std::vector<ov::InferRequest> getInferRequestsList();
private:
std::vector<std::pair<ov::InferRequest, bool>> requests;
size_t numRequestsInUse;
std::mutex mtx;
};

View File

@@ -0,0 +1,300 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <memory>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#include "utils/kuhn_munkres.hpp"
#include "cnn.hpp"
struct TrackedObject {
cv::Rect rect;
float confidence;
int object_id;
int label; // either id of a label, or UNKNOWN_LABEL_IDX
static const int UNKNOWN_LABEL_IDX; // the value (-1) for unknown label
size_t frame_idx; ///< Frame index where object was detected (-1 if N/A).
TrackedObject(const cv::Rect& rect = cv::Rect(), float conf = -1.0f,
int label = -1, int object_id = -1) :
rect(rect), confidence(conf), object_id(object_id), label(label), frame_idx(-1) {}
};
using TrackedObjects = std::vector<TrackedObject>;
///
/// \brief The Params struct stores parameters of Tracker.
///
struct TrackerParams {
size_t min_track_duration; ///< Min track duration in frames
size_t forget_delay; ///< Forget about track if the last bounding box in
/// track was detected more than specified number of
/// frames ago.
float affinity_thr; ///< Affinity threshold which is used to determine if
/// tracklet and detection should be combined.
float shape_affinity_w; ///< Shape affinity weight.
float motion_affinity_w; ///< Motion affinity weight.
float min_det_conf; ///< Min confidence of detection.
cv::Vec2f bbox_aspect_ratios_range; ///< Bounding box aspect ratios range.
cv::Vec2f bbox_heights_range; ///< Bounding box heights range.
bool drop_forgotten_tracks; ///< Drop forgotten tracks. If it's enabled it
/// disables an ability to get detection log.
int max_num_objects_in_track; ///< The number of objects in track is
/// restricted by this parameter. If it is negative or zero, the max number of
/// objects in track is not restricted.
int averaging_window_size_for_rects; ///< The number of objects in track for averaging rects of predictions.
int averaging_window_size_for_labels; ///< The number of objects in track for averaging labels of predictions.
std::string objects_type; ///< The type of boxes which will be grabbed from
/// detector. Boxes with other types are ignored.
///
/// Default constructor.
///
TrackerParams();
};
///
/// \brief The Track struct describes tracks.
///
struct Track {
///
/// \brief Track constructor.
/// \param objs Detected objects sequence.
///
explicit Track(const TrackedObjects& objs) : objects(objs), lost(0), length(1) {
CV_Assert(!objs.empty());
first_object = objs[0];
}
///
/// \brief empty returns if track does not contain objects.
/// \return true if track does not contain objects.
///
bool empty() const { return objects.empty(); }
///
/// \brief size returns number of detected objects in a track.
/// \return number of detected objects in a track.
///
size_t size() const { return objects.size(); }
///
/// \brief operator [] return const reference to detected object with
/// specified index.
/// \param i Index of object.
/// \return const reference to detected object with specified index.
///
const TrackedObject& operator[](size_t i) const { return objects[i]; }
///
/// \brief operator [] return non-const reference to detected object with
/// specified index.
/// \param i Index of object.
/// \return non-const reference to detected object with specified index.
///
TrackedObject& operator[](size_t i) { return objects[i]; }
///
/// \brief back returns const reference to last object in track.
/// \return const reference to last object in track.
///
const TrackedObject& back() const {
CV_Assert(!empty());
return objects.back();
}
///
/// \brief back returns non-const reference to last object in track.
/// \return non-const reference to last object in track.
///
TrackedObject& back() {
CV_Assert(!empty());
return objects.back();
}
TrackedObjects objects; ///< Detected objects;
size_t lost; ///< How many frames ago track has been lost.
TrackedObject first_object; ///< First object in track.
size_t length; ///< Length of a track including number of objects that were
/// removed from track in order to avoid memory usage growth.
};
///
/// \brief Simple Hungarian algorithm-based tracker.
///
class Tracker {
public:
///
/// \brief Constructor that creates an instance of Tracker with
/// parameters.
/// \param[in] params Tracker parameters.
///
explicit Tracker(const TrackerParams& params = TrackerParams()) :
m_params(params), m_tracks_counter(0), m_frame_size() {}
///
/// \brief Process given frame.
/// \param[in] frame Colored image (CV_8UC3).
/// \param[in] detections Detected objects on the frame.
/// \param[in] timestamp Timestamp must be positive and measured in
/// milliseconds
///
void Process(const cv::Mat& frame, const TrackedObjects& detections, int frame_idx);
///
/// \brief Pipeline parameters getter.
/// \return Parameters of pipeline.
///
const TrackerParams& params() const;
///
/// \brief Pipeline parameters setter.
/// \param[in] params Parameters of pipeline.
///
void set_params(const TrackerParams& params);
///
/// \brief Reset the pipeline.
///
void Reset();
///
/// \brief Returns recently detected objects.
/// \return recently detected objects.
///
const TrackedObjects& detections() const;
///
/// \brief Get active tracks to draw
/// \return Active tracks.
///
std::unordered_map<size_t, std::vector<cv::Point>> GetActiveTracks() const;
///
/// \brief Get tracked detections.
/// \return Tracked detections.
///
TrackedObjects TrackedDetections() const;
///
/// \brief Get tracked detections with labels.
/// \return Tracked detections.
///
TrackedObjects TrackedDetectionsWithLabels() const;
///
/// \brief IsTrackForgotten returns true if track is forgotten.
/// \param id Track ID.
/// \return true if track is forgotten.
///
bool IsTrackForgotten(size_t id) const;
///
/// \brief tracks Returns all tracks including forgotten (lost too many frames
/// ago).
/// \return Set of tracks {id, track}.
///
const std::unordered_map<size_t, Track>& tracks() const;
///
/// \brief tracks Returns all tracks including forgotten (lost too many frames
/// ago).
/// \return Vector of tracks
///
std::vector<Track> vector_tracks() const;
///
/// \brief IsTrackValid Checks whether track is valid (duration > threshold).
/// \param id Index of checked track.
/// \return True if track duration exceeds some predefined value.
///
bool IsTrackValid(size_t id) const;
///
/// \brief DropForgottenTracks Removes tracks from memory that were lost too
/// many frames ago.
///
void DropForgottenTracks();
private:
const std::set<size_t>& active_track_ids() const { return m_active_track_ids; }
float ShapeAffinity(const cv::Rect& trk, const cv::Rect& det);
float MotionAffinity(const cv::Rect& trk, const cv::Rect& det);
void SolveAssignmentProblem(
const std::set<size_t>& track_ids, const TrackedObjects& detections,
std::set<size_t>* unmatched_tracks,
std::set<size_t>* unmatched_detections,
std::set<std::tuple<size_t, size_t, float>>* matches);
void FilterDetectionsAndStore(const TrackedObjects& detected_objects);
void ComputeDissimilarityMatrix(const std::set<size_t>& active_track_ids,
const TrackedObjects& detections,
cv::Mat* dissimilarity_matrix);
std::vector<std::pair<size_t, size_t>> GetTrackToDetectionIds(
const std::set<std::tuple<size_t, size_t, float>>& matches);
float Distance(const TrackedObject& obj1, const TrackedObject& obj2);
void AddNewTrack(const TrackedObject& detection);
void AddNewTracks(const TrackedObjects& detections);
void AddNewTracks(const TrackedObjects& detections, const std::set<size_t>& ids);
void AppendToTrack(size_t track_id, const TrackedObject& detection);
bool EraseTrackIfBBoxIsOutOfFrame(size_t track_id);
bool EraseTrackIfItWasLostTooManyFramesAgo(size_t track_id);
bool UptateLostTrackAndEraseIfItsNeeded(size_t track_id);
void UpdateLostTracks(const std::set<size_t>& track_ids);
std::unordered_map<size_t, std::vector<cv::Point>> GetActiveTracks();
// Parameters of the pipeline.
TrackerParams m_params;
// Indexes of active tracks.
std::set<size_t> m_active_track_ids;
// All tracks.
std::unordered_map<size_t, Track> m_tracks;
// Recent detections.
TrackedObjects m_detections;
// Number of all current tracks.
size_t m_tracks_counter;
cv::Size m_frame_size;
};
int LabelWithMaxFrequencyInTrack(const Track& track, int window_size);
std::vector<Track> UpdateTrackLabelsToBestAndFilterOutUnknowns(const std::vector<Track>& tracks);

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief a header file with common samples functionality
* @file args_helper.hpp
*/
#pragma once
#include <map>
#include <set>
#include <string>
#include <vector>
#include <opencv2/core/types.hpp>
#include <openvino/openvino.hpp>
/**
* @brief This function checks input args and existence of specified files in a given folder
* @param arg path to a file to be checked for existence
* @return files updated vector of verified input files
*/
void readInputFilesArguments(std::vector<std::string>& files, const std::string& arg);
/**
* @brief This function finds -i/--i key in input args
* It's necessary to process multiple values for single key
* @return files updated vector of verified input files
*/
void parseInputFilesArguments(std::vector<std::string>& files);
std::vector<std::string> split(const std::string& s, char delim);
void split(const std::string& s, char delim, std::vector<float> &out);
std::string merge(std::initializer_list<std::string> list, const char *delim);
std::string merge(const std::vector<std::string> &list, const char *delim);
std::vector<std::string> parseDevices(const std::string& device_string);
std::map<std::string, int32_t> parseValuePerDevice(const std::set<std::string>& devices,
const std::string& values_string);
cv::Size stringToSize(const std::string& str);
std::map<std::string, ov::Layout> parseLayoutString(const std::string& layout_string);

View File

@@ -0,0 +1,155 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <algorithm>
#include <queue>
#include <set>
#include <string>
#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <monitors/presenter.h>
#include <utils/ocv_common.hpp>
#include "utils/performance_metrics.hpp"
enum class PredictionResult { Correct, Incorrect, Unknown };
class ClassificationGridMat {
public:
cv::Mat outImg;
explicit ClassificationGridMat(Presenter& presenter,
const cv::Size maxDisp = cv::Size{1920, 1080},
const cv::Size aspectRatio = cv::Size{16, 9},
double targetFPS = 60)
: currSourceId{0} {
cv::Size size(static_cast<int>(std::round(sqrt(1. * targetFPS * aspectRatio.width / aspectRatio.height))),
static_cast<int>(std::round(sqrt(1. * targetFPS * aspectRatio.height / aspectRatio.width))));
if (size.width == 0 || size.height == 0) {
size = {1, 1}; // set minimum possible grid size
}
int minCellSize = std::min(maxDisp.width / size.width, maxDisp.height / size.height);
cellSize = cv::Size(minCellSize, minCellSize);
for (int i = 0; i < size.height; i++) {
for (int j = 0; j < size.width; j++) {
points.emplace_back(cellSize.width * j, presenter.graphSize.height + cellSize.height * i);
}
}
outImg.create((cellSize.height * size.height) + presenter.graphSize.height,
cellSize.width * size.width,
CV_8UC3);
outImg.setTo(0);
textSize = cv::getTextSize("", fontType, fontScale, thickness, &baseline);
accuracyMessageSize = cv::getTextSize("Accuracy (top 0): 0.000", fontType, fontScale, thickness, &baseline);
testMessageSize = cv::getTextSize(ClassificationGridMat::testMessage, fontType, fontScale, thickness, &baseline);
}
void textUpdate(PerformanceMetrics& metrics,
PerformanceMetrics::TimePoint lastRequestStartTime,
double accuracy,
unsigned int nTop,
bool isFpsTest,
bool showAccuracy,
Presenter& presenter) {
rectangle(outImg, {0, 0}, {outImg.cols, presenter.graphSize.height}, cv::Scalar(0, 0, 0), cv::FILLED);
presenter.drawGraphs(outImg);
metrics.update(lastRequestStartTime,
outImg,
cv::Point(textPadding, textSize.height + textPadding),
fontType,
fontScale,
cv::Scalar(255, 100, 100),
thickness);
if (showAccuracy) {
cv::putText(outImg,
cv::format("Accuracy (top %d): %.3f", nTop, accuracy),
cv::Point(outImg.cols - accuracyMessageSize.width - textPadding, textSize.height + textPadding),
fontType,
fontScale,
cv::Scalar(255, 255, 255),
thickness);
}
if (isFpsTest) {
cv::putText(
outImg,
ClassificationGridMat::testMessage,
cv::Point(outImg.cols - testMessageSize.width - textPadding, (textSize.height + textPadding) * 2),
fontType,
fontScale,
cv::Scalar(50, 50, 255),
thickness);
}
}
void updateMat(const cv::Mat& mat, const std::string& label, PredictionResult predictionResul) {
if (!prevImg.empty()) {
size_t prevSourceId = currSourceId - 1;
prevSourceId = std::min(prevSourceId, points.size() - 1);
prevImg.copyTo(outImg(cv::Rect(points[prevSourceId], cellSize)));
}
cv::Scalar textColor;
switch (predictionResul) {
case PredictionResult::Correct:
textColor = cv::Scalar(75, 255, 75); // green
break;
case PredictionResult::Incorrect:
textColor = cv::Scalar(50, 50, 255); // red
break;
case PredictionResult::Unknown:
textColor = cv::Scalar(200, 10, 10); // blue
break;
default:
throw std::runtime_error("Undefined type of prediction result");
}
int labelThickness = cellSize.width / 20;
cv::Size labelTextSize = cv::getTextSize(label, fontType, 1, 2, &baseline);
double labelFontScale = static_cast<double>(cellSize.width - 2 * labelThickness) / labelTextSize.width;
cv::resize(mat, prevImg, cellSize);
putHighlightedText(prevImg,
label,
cv::Point(labelThickness, cellSize.height - labelThickness - labelTextSize.height),
fontType,
labelFontScale,
textColor,
2);
cv::Mat cell = outImg(cv::Rect(points[currSourceId], cellSize));
prevImg.copyTo(cell);
cv::rectangle(cell, {0, 0}, {cell.cols, cell.rows}, {255, 50, 50}, labelThickness); // draw a border
if (currSourceId == points.size() - 1) {
currSourceId = 0;
} else {
currSourceId++;
}
}
private:
cv::Mat prevImg;
cv::Size cellSize;
size_t currSourceId;
std::vector<cv::Point> points;
static const int fontType = cv::FONT_HERSHEY_PLAIN;
static constexpr double fontScale = 1.5;
static const int thickness = 2;
static const int textPadding = 10;
static constexpr const char testMessage[] = "Testing, please wait...";
int baseline;
cv::Size textSize;
cv::Size accuracyMessageSize;
cv::Size testMessageSize;
};
constexpr const char ClassificationGridMat::testMessage[];

View File

@@ -0,0 +1,193 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief a header file with common samples functionality
* @file common.hpp
*/
#pragma once
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <openvino/openvino.hpp>
#include "utils/slog.hpp"
#include "utils/args_helper.hpp"
#ifndef UNUSED
#ifdef _WIN32
#define UNUSED
#else
#define UNUSED __attribute__((unused))
#endif
#endif
template <typename T, std::size_t N>
constexpr std::size_t arraySize(const T(&)[N]) noexcept {
return N;
}
static inline void catcher() noexcept {
if (std::current_exception()) {
try {
std::rethrow_exception(std::current_exception());
} catch (const std::exception& error) {
slog::err << error.what() << slog::endl;
} catch (...) {
slog::err << "Non-exception object thrown" << slog::endl;
}
std::exit(1);
}
std::abort();
}
template <typename T>
T clamp(T value, T low, T high) {
return value < low ? low : (value > high ? high : value);
}
inline slog::LogStream& operator<<(slog::LogStream& os, const ov::Version& version) {
return os << "OpenVINO" << slog::endl
<< "\tversion: " << OPENVINO_VERSION_MAJOR << "." << OPENVINO_VERSION_MINOR << "." << OPENVINO_VERSION_PATCH << slog::endl
<< "\tbuild: " << version.buildNumber;
}
/**
* @class Color
* @brief A Color class stores channels of a given color
*/
class Color {
private:
unsigned char _r;
unsigned char _g;
unsigned char _b;
public:
/**
* A default constructor.
* @param r - value for red channel
* @param g - value for green channel
* @param b - value for blue channel
*/
Color(unsigned char r,
unsigned char g,
unsigned char b) : _r(r), _g(g), _b(b) {}
inline unsigned char red() const {
return _r;
}
inline unsigned char blue() const {
return _b;
}
inline unsigned char green() const {
return _g;
}
};
// Known colors for training classes from the Cityscapes dataset
static UNUSED const Color CITYSCAPES_COLORS[] = {
{ 128, 64, 128 },
{ 232, 35, 244 },
{ 70, 70, 70 },
{ 156, 102, 102 },
{ 153, 153, 190 },
{ 153, 153, 153 },
{ 30, 170, 250 },
{ 0, 220, 220 },
{ 35, 142, 107 },
{ 152, 251, 152 },
{ 180, 130, 70 },
{ 60, 20, 220 },
{ 0, 0, 255 },
{ 142, 0, 0 },
{ 70, 0, 0 },
{ 100, 60, 0 },
{ 90, 0, 0 },
{ 230, 0, 0 },
{ 32, 11, 119 },
{ 0, 74, 111 },
{ 81, 0, 81 }
};
inline void showAvailableDevices() {
ov::Core core;
std::vector<std::string> devices = core.get_available_devices();
std::cout << "Available devices:";
for (const auto& device : devices) {
std::cout << ' ' << device;
}
std::cout << std::endl;
}
inline std::string fileNameNoExt(const std::string& filepath) {
auto pos = filepath.rfind('.');
if (pos == std::string::npos) return filepath;
return filepath.substr(0, pos);
}
inline void logCompiledModelInfo(
const ov::CompiledModel& compiledModel,
const std::string& modelName,
const std::string& deviceName,
const std::string& modelType = "") {
slog::info << "The " << modelType << (modelType.empty() ? "" : " ") << "model " << modelName << " is loaded to " << deviceName << slog::endl;
std::set<std::string> devices;
for (const std::string& device : parseDevices(deviceName)) {
devices.insert(device);
}
if (devices.find("AUTO") == devices.end()) { // do not print info for AUTO device
for (const auto& device : devices) {
try {
slog::info << "\tDevice: " << device << slog::endl;
int32_t nstreams = compiledModel.get_property(ov::streams::num);
slog::info << "\t\tNumber of streams: " << nstreams << slog::endl;
if (device == "CPU") {
int32_t nthreads = compiledModel.get_property(ov::inference_num_threads);
slog::info << "\t\tNumber of threads: " << (nthreads == 0 ? "AUTO" : std::to_string(nthreads)) << slog::endl;
}
}
catch (const ov::Exception&) {}
}
}
}
inline void logBasicModelInfo(const std::shared_ptr<ov::Model>& model) {
slog::info << "Model name: " << model->get_friendly_name() << slog::endl;
// Dump information about model inputs/outputs
ov::OutputVector inputs = model->inputs();
ov::OutputVector outputs = model->outputs();
slog::info << "\tInputs: " << slog::endl;
for (const ov::Output<ov::Node>& input : inputs) {
const std::string name = input.get_any_name();
const ov::element::Type type = input.get_element_type();
const ov::PartialShape shape = input.get_partial_shape();
const ov::Layout layout = ov::layout::get_layout(input);
slog::info << "\t\t" << name << ", " << type << ", " << shape << ", " << layout.to_string() << slog::endl;
}
slog::info << "\tOutputs: " << slog::endl;
for (const ov::Output<ov::Node>& output : outputs) {
const std::string name = output.get_any_name();
const ov::element::Type type = output.get_element_type();
const ov::PartialShape shape = output.get_partial_shape();
const ov::Layout layout = ov::layout::get_layout(output);
slog::info << "\t\t" << name << ", " << type << ", " << shape << ", " << layout.to_string() << slog::endl;
}
return;
}
std::vector<unsigned> loadClassIndices(const std::string &groundtruth_filepath,
const std::vector<std::string> &imageNames);

View File

@@ -0,0 +1,50 @@
/*
// Copyright (C) 2020-2024 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.
*/
#pragma once
#include <stdint.h>
#include <map>
#include <set>
#include <string>
#include <openvino/openvino.hpp>
struct ModelConfig {
std::string deviceName;
std::string cpuExtensionsPath;
std::string clKernelsConfigPath;
unsigned int maxAsyncRequests;
ov::AnyMap compiledModelConfig;
std::set<std::string> getDevices();
std::map<std::string, std::string> getLegacyConfig() const;
protected:
std::set<std::string> devices;
};
class ConfigFactory {
public:
static ModelConfig getUserConfig(const std::string& flags_d,
uint32_t flags_nireq,
const std::string& flags_nstreams,
uint32_t flags_nthreads);
static ModelConfig getMinLatencyConfig(const std::string& flags_d, uint32_t flags_nireq);
protected:
static ModelConfig getCommonConfig(const std::string& flags_d, uint32_t flags_nireq);
};

View File

@@ -0,0 +1,21 @@
// Copyright (C) 2020-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <gflags/gflags.h>
#define DEFINE_INPUT_FLAGS \
DEFINE_string(i, "", input_message); \
DEFINE_bool(loop, false, loop_message);
#define DEFINE_OUTPUT_FLAGS \
DEFINE_string(o, "", output_message); \
DEFINE_uint32(limit, 1000, limit_message);
static const char input_message[] = "Required. An input to process. The input must be a single image, a folder of "
"images, video file or camera id.";
static const char loop_message[] = "Optional. Enable reading the input in a loop.";
static const char output_message[] = "Optional. Name of the output file(s) to save. Frames of odd width or height can be truncated. See https://github.com/opencv/opencv/pull/24086";
static const char limit_message[] = "Optional. Number of frames to store in output. If 0 is set, all frames are stored.";

View File

@@ -0,0 +1,127 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <opencv2/core/core.hpp>
class GridMat {
public:
cv::Mat outimg;
explicit GridMat(const std::vector<cv::Size>& sizes, const cv::Size maxDisp = cv::Size{1920, 1080}) {
size_t maxWidth = 0;
size_t maxHeight = 0;
for (size_t i = 0; i < sizes.size(); i++) {
maxWidth = std::max(maxWidth, static_cast<size_t>(sizes[i].width));
maxHeight = std::max(maxHeight, static_cast<size_t>(sizes[i].height));
}
if (0 == maxWidth || 0 == maxHeight) {
throw std::invalid_argument("Input resolution must not be zero.");
}
size_t nGridCols = static_cast<size_t>(ceil(sqrt(static_cast<float>(sizes.size()))));
size_t nGridRows = (sizes.size() - 1) / nGridCols + 1;
size_t gridMaxWidth = static_cast<size_t>(maxDisp.width/nGridCols);
size_t gridMaxHeight = static_cast<size_t>(maxDisp.height/nGridRows);
float scaleWidth = static_cast<float>(gridMaxWidth) / maxWidth;
float scaleHeight = static_cast<float>(gridMaxHeight) / maxHeight;
float scaleFactor = std::min(1.f, std::min(scaleWidth, scaleHeight));
cellSize.width = static_cast<int>(maxWidth * scaleFactor);
cellSize.height = static_cast<int>(maxHeight * scaleFactor);
for (size_t i = 0; i < sizes.size(); i++) {
cv::Point p;
p.x = cellSize.width * (i % nGridCols);
p.y = cellSize.height * (i / nGridCols);
points.push_back(p);
}
outimg.create(cellSize.height * nGridRows, cellSize.width * nGridCols, CV_8UC3);
outimg.setTo(0);
clear();
}
cv::Size getCellSize() {
return cellSize;
}
void fill(std::vector<cv::Mat>& frames) {
if (frames.size() > points.size()) {
throw std::logic_error("Cannot display " + std::to_string(frames.size()) + " channels in a grid with " + std::to_string(points.size()) + " cells");
}
for (size_t i = 0; i < frames.size(); i++) {
cv::Mat cell = outimg(cv::Rect(points[i].x, points[i].y, cellSize.width, cellSize.height));
if ((cellSize.width == frames[i].cols) && (cellSize.height == frames[i].rows)) {
frames[i].copyTo(cell);
} else if ((cellSize.width > frames[i].cols) && (cellSize.height > frames[i].rows)) {
frames[i].copyTo(cell(cv::Rect(0, 0, frames[i].cols, frames[i].rows)));
} else {
cv::resize(frames[i], cell, cellSize);
}
}
unupdatedSourceIDs.clear();
}
void update(const cv::Mat& frame, const size_t sourceID) {
const cv::Mat& cell = outimg(cv::Rect(points[sourceID], cellSize));
if ((cellSize.width == frame.cols) && (cellSize.height == frame.rows)) {
frame.copyTo(cell);
} else if ((cellSize.width > frame.cols) && (cellSize.height > frame.rows)) {
frame.copyTo(cell(cv::Rect(0, 0, frame.cols, frame.rows)));
} else {
cv::resize(frame, cell, cellSize);
}
unupdatedSourceIDs.erase(unupdatedSourceIDs.find(sourceID));
}
bool isFilled() const noexcept {
return unupdatedSourceIDs.empty();
}
void clear() {
size_t counter = 0;
std::generate_n(std::inserter(unupdatedSourceIDs, unupdatedSourceIDs.end()), points.size(), [&counter]{return counter++;});
}
std::set<size_t> getUnupdatedSourceIDs() const noexcept {
return unupdatedSourceIDs;
}
cv::Mat getMat() const noexcept {
return outimg;
}
private:
cv::Size cellSize;
std::set<size_t> unupdatedSourceIDs;
std::vector<cv::Point> points;
};
//void fillROIColor(cv::Mat& displayImage, cv::Rect roi, cv::Scalar color, double opacity) {
// if (opacity > 0) {
// roi = roi & cv::Rect(0, 0, displayImage.cols, displayImage.rows);
// cv::Mat textROI = displayImage(roi);
// cv::addWeighted(color, opacity, textROI, 1.0 - opacity , 0.0, textROI);
// }
//}
//
//void putTextOnImage(cv::Mat& displayImage, std::string str, cv::Point p,
// cv::HersheyFonts font, double fontScale, cv::Scalar color,
// int thickness = 1, cv::Scalar bgcolor = cv::Scalar(),
// double opacity = 0) {
// int baseline = 0;
// cv::Size textSize = cv::getTextSize(str, font, 0.5, 1, &baseline);
// fillROIColor(displayImage, cv::Rect(cv::Point(p.x, p.y + baseline),
// cv::Point(p.x + textSize.width, p.y - textSize.height)),
// bgcolor, opacity);
// cv::putText(displayImage, str, p, font, fontScale, color, thickness);
//}

View File

@@ -0,0 +1,29 @@
/*
// Copyright (C) 2021-2024 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.
*/
#pragma once
#include <opencv2/opencv.hpp>
enum RESIZE_MODE {
RESIZE_FILL,
RESIZE_KEEP_ASPECT,
RESIZE_KEEP_ASPECT_LETTERBOX
};
cv::Mat resizeImageExt(const cv::Mat& mat, int width, int height, RESIZE_MODE resizeMode = RESIZE_FILL,
cv::InterpolationFlags interpolationMode = cv::INTER_LINEAR, cv::Rect* roi = nullptr,
cv::Scalar BorderConstant = cv::Scalar(0, 0, 0));

View File

@@ -0,0 +1,52 @@
// Copyright (C) 2020-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <stddef.h>
#include <limits>
#include <memory>
#include <string>
#include <opencv2/core.hpp>
#include "utils/performance_metrics.hpp"
enum class read_type { efficient, safe };
class ImagesCapture {
public:
const bool loop;
ImagesCapture(bool loop) : loop{loop} {}
virtual double fps() const = 0;
virtual cv::Mat read() = 0;
virtual std::string getType() const = 0;
const PerformanceMetrics& getMetrics() {
return readerMetrics;
}
virtual ~ImagesCapture() = default;
protected:
PerformanceMetrics readerMetrics;
};
// An advanced version of
// try {
// return cv::VideoCapture(std::stoi(input));
// } catch (const std::invalid_argument&) {
// return cv::VideoCapture(input);
// } catch (const std::out_of_range&) {
// return cv::VideoCapture(input);
// }
// Some VideoCapture backends continue owning the video buffer under cv::Mat. safe_copy forses to return a copy from
// read()
// https://github.com/opencv/opencv/blob/46e1560678dba83d25d309d8fbce01c40f21b7be/modules/gapi/include/opencv2/gapi/streaming/cap.hpp#L72-L76
std::unique_ptr<ImagesCapture> openImagesCapture(
const std::string& input,
bool loop,
read_type type = read_type::efficient,
size_t initialImageId = 0,
size_t readLengthLimit = std::numeric_limits<size_t>::max(), // General option
cv::Size cameraResolution = {1280, 720});

View File

@@ -0,0 +1,149 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <list>
#include <memory>
#include <set>
#include <thread>
#include <vector>
#include <queue>
#include <opencv2/opencv.hpp>
class InputChannel;
class IInputSource {
public:
virtual bool read(cv::Mat& mat, const std::shared_ptr<InputChannel>& caller) = 0;
virtual void addSubscriber(const std::weak_ptr<InputChannel>& inputChannel) = 0;
virtual cv::Size getSize() = 0;
virtual void lock() {
sourceLock.lock();
}
virtual void unlock() {
sourceLock.unlock();
}
virtual ~IInputSource() = default;
private:
std::mutex sourceLock;
};
class InputChannel: public std::enable_shared_from_this<InputChannel> { // note: public inheritance
public:
InputChannel(const InputChannel&) = delete;
InputChannel& operator=(const InputChannel&) = delete;
static std::shared_ptr<InputChannel> create(const std::shared_ptr<IInputSource>& source) {
auto tmp = std::shared_ptr<InputChannel>(new InputChannel(source));
source->addSubscriber(tmp);
return tmp;
}
bool read(cv::Mat& mat) {
readQueueMutex.lock();
if (readQueue.empty()) {
readQueueMutex.unlock();
source->lock();
readQueueMutex.lock();
if (readQueue.empty()) {
bool res = source->read(mat, shared_from_this());
readQueueMutex.unlock();
source->unlock();
return res;
} else {
source->unlock();
}
}
mat = readQueue.front().clone();
readQueue.pop();
readQueueMutex.unlock();
return true;
}
void push(const cv::Mat& mat) {
readQueueMutex.lock();
readQueue.push(mat);
readQueueMutex.unlock();
}
cv::Size getSize() {
return source->getSize();
}
private:
explicit InputChannel(const std::shared_ptr<IInputSource>& source): source{source} {}
std::shared_ptr<IInputSource> source;
std::queue<cv::Mat, std::list<cv::Mat>> readQueue;
std::mutex readQueueMutex;
};
class VideoCaptureSource: public IInputSource {
public:
VideoCaptureSource(const cv::VideoCapture& videoCapture, bool loop): videoCapture{videoCapture}, loop{loop},
imSize{static_cast<int>(videoCapture.get(cv::CAP_PROP_FRAME_WIDTH)), static_cast<int>(videoCapture.get(cv::CAP_PROP_FRAME_HEIGHT))} {}
bool read(cv::Mat& mat, const std::shared_ptr<InputChannel>& caller) override {
if (!videoCapture.read(mat)) {
if (loop) {
videoCapture.set(cv::CAP_PROP_POS_FRAMES, 0);
videoCapture.read(mat);
} else {
return false;
}
}
if (1 != subscribedInputChannels.size()) {
cv::Mat shared = mat.clone();
for (const std::weak_ptr<InputChannel>& weakInputChannel : subscribedInputChannels) {
try {
std::shared_ptr<InputChannel> sharedInputChannel = std::shared_ptr<InputChannel>(weakInputChannel);
if (caller != sharedInputChannel) {
sharedInputChannel->push(shared);
}
} catch (const std::bad_weak_ptr&) {}
}
}
return true;
}
void addSubscriber(const std::weak_ptr<InputChannel>& inputChannel) override {
subscribedInputChannels.push_back(inputChannel);
}
cv::Size getSize() override {
return imSize;
}
private:
std::vector<std::weak_ptr<InputChannel>> subscribedInputChannels;
cv::VideoCapture videoCapture;
bool loop;
cv::Size imSize;
};
class ImageSource: public IInputSource {
public:
ImageSource(const cv::Mat& im, bool loop): im{im.clone()}, loop{loop} {} // clone to avoid image changing
bool read(cv::Mat& mat, const std::shared_ptr<InputChannel>& caller) override {
if (!loop) {
auto subscribedInputChannelsIt = subscribedInputChannels.find(caller);
if (subscribedInputChannels.end() == subscribedInputChannelsIt) {
return false;
} else {
subscribedInputChannels.erase(subscribedInputChannelsIt);
mat = im;
return true;
}
} else {
mat = im;
return true;
}
}
void addSubscriber(const std::weak_ptr<InputChannel>& inputChannel) override {
if (false == subscribedInputChannels.insert(inputChannel).second)
throw std::invalid_argument("The insertion did not take place");
}
cv::Size getSize() override {
return im.size();
}
private:
std::set<std::weak_ptr<InputChannel>, std::owner_less<std::weak_ptr<InputChannel>>> subscribedInputChannels;
cv::Mat im;
bool loop;
};

View File

@@ -0,0 +1,57 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "opencv2/core.hpp"
#include <memory>
#include <vector>
///
/// \brief The KuhnMunkres class
///
/// Solves the assignment problem.
///
class KuhnMunkres {
public:
///
/// \brief Initializes the class for assignment problem solving.
/// \param[in] greedy If a faster greedy matching algorithm should be used.
explicit KuhnMunkres(bool greedy = false);
///
/// \brief Solves the assignment problem for given dissimilarity matrix.
/// It returns a vector that where each element is a column index for
/// corresponding row (e.g. result[0] stores optimal column index for very
/// first row in the dissimilarity matrix).
/// \param dissimilarity_matrix CV_32F dissimilarity matrix.
/// \return Optimal column index for each row. -1 means that there is no
/// column for row.
///
std::vector<size_t> Solve(const cv::Mat &dissimilarity_matrix);
private:
static constexpr int kStar = 1;
static constexpr int kPrime = 2;
cv::Mat dm_;
cv::Mat marked_;
std::vector<cv::Point> points_;
std::vector<int> is_row_visited_;
std::vector<int> is_col_visited_;
int n_;
bool greedy_;
void TrySimpleCase();
bool CheckIfOptimumIsFound();
cv::Point FindUncoveredMinValPos();
void UpdateDissimilarityMatrix(float val);
int FindInRow(int row, int what);
int FindInCol(int col, int what);
void Run();
};

View File

@@ -0,0 +1,81 @@
/*
// Copyright (C) 2021-2024 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.
*/
#pragma once
#include "opencv2/core.hpp"
#include <numeric>
#include <vector>
struct Anchor {
float left;
float top;
float right;
float bottom;
float getWidth() const {
return (right - left) + 1.0f;
}
float getHeight() const {
return (bottom - top) + 1.0f;
}
float getXCenter() const {
return left + (getWidth() - 1.0f) / 2.0f;
}
float getYCenter() const {
return top + (getHeight() - 1.0f) / 2.0f;
}
};
template <typename Anchor>
std::vector<int> nms(const std::vector<Anchor>& boxes, const std::vector<float>& scores,
const float thresh, bool includeBoundaries=false) {
std::vector<float> areas(boxes.size());
for (size_t i = 0; i < boxes.size(); ++i) {
areas[i] = (boxes[i].right - boxes[i].left + includeBoundaries) * (boxes[i].bottom - boxes[i].top + includeBoundaries);
}
std::vector<int> order(scores.size());
std::iota(order.begin(), order.end(), 0);
std::sort(order.begin(), order.end(), [&scores](int o1, int o2) { return scores[o1] > scores[o2]; });
size_t ordersNum = 0;
for (; ordersNum < order.size() && scores[order[ordersNum]] >= 0; ordersNum++);
std::vector<int> keep;
bool shouldContinue = true;
for (size_t i = 0; shouldContinue && i < ordersNum; ++i) {
auto idx1 = order[i];
if (idx1 >= 0) {
keep.push_back(idx1);
shouldContinue = false;
for (size_t j = i + 1; j < ordersNum; ++j) {
auto idx2 = order[j];
if (idx2 >= 0) {
shouldContinue = true;
auto overlappingWidth = std::fminf(boxes[idx1].right, boxes[idx2].right) - std::fmaxf(boxes[idx1].left, boxes[idx2].left);
auto overlappingHeight = std::fminf(boxes[idx1].bottom, boxes[idx2].bottom) - std::fmaxf(boxes[idx1].top, boxes[idx2].top);
auto intersection = overlappingWidth > 0 && overlappingHeight > 0 ? overlappingWidth * overlappingHeight : 0;
auto overlap = intersection / (areas[idx1] + areas[idx2] - intersection);
if (overlap >= thresh) {
order[j] = -1;
}
}
}
}
}
return keep;
}

View File

@@ -0,0 +1,347 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief a header file with common samples functionality using OpenCV
* @file ocv_common.hpp
*/
#pragma once
#include <opencv2/opencv.hpp>
#include <openvino/openvino.hpp>
#include "utils/common.hpp"
#include "utils/shared_tensor_allocator.hpp"
/**
* @brief Get cv::Mat value in the correct format.
*/
template <typename T>
const T getMatValue(const cv::Mat& mat, size_t h, size_t w, size_t c) {
switch (mat.type()) {
case CV_8UC1: return (T)mat.at<uchar>((int)h, (int)w);
case CV_8UC3: return (T)mat.at<cv::Vec3b>((int)h, (int)w)[c];
case CV_32FC1: return (T)mat.at<float>((int)h, (int)w);
case CV_32FC3: return (T)mat.at<cv::Vec3f>((int)h, (int)w)[c];
}
throw std::runtime_error("cv::Mat type is not recognized");
};
/**
* @brief Resize and copy image data from cv::Mat object to a given Tensor object.
* @param mat - given cv::Mat object with an image data.
* @param tensor - Tensor object which to be filled by an image data.
* @param batchIndex - batch index of an image inside of the blob.
*/
static UNUSED void matToTensor(const cv::Mat& mat, ov::Tensor& tensor, int batchIndex = 0) {
ov::Shape tensorShape = tensor.get_shape();
static const ov::Layout layout("NCHW");
const size_t width = tensorShape[ov::layout::width_idx(layout)];
const size_t height = tensorShape[ov::layout::height_idx(layout)];
const size_t channels = tensorShape[ov::layout::channels_idx(layout)];
if (static_cast<size_t>(mat.channels()) != channels) {
throw std::runtime_error("The number of channels for model input and image must match");
}
if (channels != 1 && channels != 3) {
throw std::runtime_error("Unsupported number of channels");
}
int batchOffset = batchIndex * width * height * channels;
cv::Mat resizedMat;
if (static_cast<int>(width) != mat.size().width || static_cast<int>(height) != mat.size().height) {
cv::resize(mat, resizedMat, cv::Size(width, height));
} else {
resizedMat = mat;
}
if (tensor.get_element_type() == ov::element::f32) {
float_t* tensorData = tensor.data<float_t>();
for (size_t c = 0; c < channels; c++)
for (size_t h = 0; h < height; h++)
for (size_t w = 0; w < width; w++)
tensorData[batchOffset + c * width * height + h * width + w] =
getMatValue<float_t>(resizedMat, h, w, c);
} else {
uint8_t* tensorData = tensor.data<uint8_t>();
if (resizedMat.depth() == CV_32F) {
throw std::runtime_error("Conversion of cv::Mat from float_t to uint8_t is forbidden");
}
for (size_t c = 0; c < channels; c++)
for (size_t h = 0; h < height; h++)
for (size_t w = 0; w < width; w++)
tensorData[batchOffset + c * width * height + h * width + w] =
getMatValue<uint8_t>(resizedMat, h, w, c);
}
}
static UNUSED ov::Tensor wrapMat2Tensor(const cv::Mat& mat) {
auto matType = mat.type() & CV_MAT_DEPTH_MASK;
if (matType != CV_8U && matType != CV_32F) {
throw std::runtime_error("Unsupported mat type for wrapping");
}
bool isMatFloat = matType == CV_32F;
const size_t channels = mat.channels();
const size_t height = mat.rows;
const size_t width = mat.cols;
const size_t strideH = mat.step.buf[0];
const size_t strideW = mat.step.buf[1];
const bool isDense = !isMatFloat ? (strideW == channels && strideH == channels * width) :
(strideW == channels * sizeof(float) && strideH == channels * width * sizeof(float));
if (!isDense) {
throw std::runtime_error("Doesn't support conversion from not dense cv::Mat");
}
auto precision = isMatFloat ? ov::element::f32 : ov::element::u8;
return ov::Tensor(precision, ov::Shape{ 1, height, width, channels }, SharedMatAllocator{mat});
}
static inline void resize2tensor(const cv::Mat& mat, ov::Tensor& tensor) {
static const ov::Layout layout{"NHWC"};
const ov::Shape& shape = tensor.get_shape();
cv::Size size{int(shape[ov::layout::width_idx(layout)]), int(shape[ov::layout::height_idx(layout)])};
assert(tensor.get_element_type() == ov::element::u8);
assert(shape.size() == 4);
assert(shape[ov::layout::batch_idx(layout)] == 1);
assert(shape[ov::layout::channels_idx(layout)] == 3);
cv::resize(mat, cv::Mat{size, CV_8UC3, tensor.data<uint8_t>()}, size);
}
struct IntervalCondition {
using DimType = size_t;
using IndexType = size_t;
using ConditionChecker = std::function<bool(IndexType, const ov::PartialShape&)>;
template<class Cond>
constexpr IntervalCondition(IndexType i1, IndexType i2, Cond c) :
impl([=](IndexType i0, const ov::PartialShape& shape) {
return c(shape[i0].get_max_length(), shape[i1].get_max_length()) && c(shape[i0].get_max_length(), shape[i2].get_max_length());})
{}
bool operator() (IndexType i0, const ov::PartialShape& shape) const { return impl(i0, shape); }
private:
ConditionChecker impl;
};
template <template<class> class Cond, class ...Args>
IntervalCondition makeCond(Args&&...args) {
return IntervalCondition(std::forward<Args>(args)..., Cond<IntervalCondition::DimType>{});
}
using LayoutCondition = std::tuple<size_t/*dim index*/, IntervalCondition, std::string>;
static inline std::tuple<bool, ov::Layout> makeGuesLayoutFrom4DShape(const ov::PartialShape& shape) {
// at the moment we make assumption about NCHW & NHCW only
// if hypothetical C value is less than hypothetical H and W - then
// out assumption is correct and we pick a corresponding layout
static const std::array<LayoutCondition, 2> hypothesisMatrix {{
{1, makeCond<std::less_equal>(2, 3), "NCHW"},
{3, makeCond<std::less_equal>(1, 2), "NHWC"}
}};
for (const auto &h : hypothesisMatrix) {
auto channel_index = std::get<0>(h);
const auto &cond = std::get<1>(h);
if (cond(channel_index, shape)) {
return std::make_tuple(true, ov::Layout{std::get<2>(h)});
}
}
return {false, ov::Layout{}};
}
static inline ov::Layout getLayoutFromShape(const ov::PartialShape& shape) {
if (shape.size() == 2) {
return "NC";
}
if (shape.size() == 3) {
if (shape[0] == 1) {
return "NHW";
}
if (shape[2] == 1) {
return "HWN";
}
throw std::runtime_error("Can't guess layout for " + shape.to_string());
}
if (shape.size() == 4) {
if (ov::Interval{1, 4}.contains(shape[1].get_interval())) {
return "NCHW";
}
if (ov::Interval{1, 4}.contains(shape[3].get_interval())) {
return "NHWC";
}
if (shape[1] == shape[2]) {
return "NHWC";
}
if (shape[2] == shape[3]) {
return "NCHW";
}
bool guesResult = false;
ov::Layout guessedLayout;
std::tie(guesResult, guessedLayout) = makeGuesLayoutFrom4DShape(shape);
if (guesResult) {
return guessedLayout;
}
}
throw std::runtime_error("Usupported " + std::to_string(shape.size()) + "D shape");
}
/**
* @brief Puts text message on the frame, highlights the text with a white border to make it distinguishable from
* the background.
* @param frame - frame to put the text on.
* @param message - text of the message.
* @param position - bottom-left corner of the text string in the image.
* @param fontFace - font type.
* @param fontScale - font scale factor that is multiplied by the font-specific base size.
* @param color - text color.
* @param thickness - thickness of the lines used to draw a text.
*/
inline void putHighlightedText(const cv::Mat& frame,
const std::string& message,
cv::Point position,
int fontFace,
double fontScale,
cv::Scalar color,
int thickness) {
cv::putText(frame, message, position, fontFace, fontScale, cv::Scalar(255, 255, 255), thickness + 1);
cv::putText(frame, message, position, fontFace, fontScale, color, thickness);
}
// TODO: replace with Size::empty() after OpenCV3 is dropped
static inline bool isSizeEmpty(const cv::Size& size) {
return size.width <= 0 || size.height <= 0;
}
// TODO: replace with Rect::empty() after OpenCV3 is dropped
static inline bool isRectEmpty(const cv::Rect& rect) {
return rect.width <= 0 || rect.height <= 0;
}
class OutputTransform {
public:
OutputTransform() : doResize(false), scaleFactor(1) {}
OutputTransform(cv::Size inputSize, cv::Size outputResolution) :
doResize(true), scaleFactor(1), inputSize(inputSize), outputResolution(outputResolution) {}
cv::Size computeResolution() {
float inputWidth = static_cast<float>(inputSize.width);
float inputHeight = static_cast<float>(inputSize.height);
scaleFactor = MIN(outputResolution.height / inputHeight, outputResolution.width / inputWidth);
newResolution = cv::Size{static_cast<int>(inputWidth * scaleFactor), static_cast<int>(inputHeight * scaleFactor)};
return newResolution;
}
void resize(cv::Mat& image) {
if (!doResize) { return; }
cv::Size currSize = image.size();
if (currSize != inputSize) {
inputSize = currSize;
computeResolution();
}
if (scaleFactor == 1) { return; }
cv::resize(image, image, newResolution);
}
template<typename T>
void scaleCoord(T& coord) {
if (!doResize || scaleFactor == 1) { return; }
coord.x = std::floor(coord.x * scaleFactor);
coord.y = std::floor(coord.y * scaleFactor);
}
template<typename T>
void scaleRect(T& rect) {
if (!doResize || scaleFactor == 1) { return; }
scaleCoord(rect);
rect.width = std::floor(rect.width * scaleFactor);
rect.height = std::floor(rect.height * scaleFactor);
}
bool doResize;
private:
float scaleFactor;
cv::Size inputSize;
cv::Size outputResolution;
cv::Size newResolution;
};
class InputTransform {
public:
InputTransform() : reverseInputChannels(false), isTrivial(true) {}
InputTransform(bool reverseInputChannels, const std::string& meanValues, const std::string& scaleValues) :
reverseInputChannels(reverseInputChannels),
isTrivial(!reverseInputChannels && meanValues.empty() && scaleValues.empty()),
means(meanValues.empty() ? cv::Scalar(0.0, 0.0, 0.0) : string2Vec(meanValues)),
stdScales(scaleValues.empty() ? cv::Scalar(1.0, 1.0, 1.0) : string2Vec(scaleValues)) {
}
cv::Scalar string2Vec(const std::string& string) {
const auto& strValues = split(string, ' ');
std::vector<float> values;
try {
for (auto& str : strValues)
values.push_back(std::stof(str));
}
catch (const std::invalid_argument&) {
throw std::runtime_error("Invalid parameter --mean_values or --scale_values is provided.");
}
if (values.size() != 3) {
throw std::runtime_error("InputTransform expects 3 values per channel, but get \"" + string + "\".");
}
return cv::Scalar(values[0], values[1], values[2]);
}
void setPrecision(ov::preprocess::PrePostProcessor& ppp, const std::string& tensorName) {
const auto precision = isTrivial ? ov::element::u8 : ov::element::f32;
ppp.input(tensorName).tensor().
set_element_type(precision);
}
cv::Mat operator()(const cv::Mat& inputs) {
if (isTrivial) { return inputs; }
cv::Mat result;
inputs.convertTo(result, CV_32F);
if (reverseInputChannels) {
cv::cvtColor(result, result, cv::COLOR_BGR2RGB);
}
// TODO: merge the two following lines after OpenCV3 is droppped
result -= means;
result /= cv::Mat{stdScales};
return result;
}
private:
bool reverseInputChannels;
bool isTrivial;
cv::Scalar means;
cv::Scalar stdScales;
};
class LazyVideoWriter {
cv::VideoWriter writer;
unsigned nwritten;
public:
const std::string filenames;
const double fps;
const unsigned lim;
LazyVideoWriter(const std::string& filenames, double fps, unsigned lim) :
nwritten{1}, filenames{filenames}, fps{fps}, lim{lim} {}
void write(const cv::Mat& im) {
if (writer.isOpened() && (nwritten < lim || 0 == lim)) {
writer.write(im);
++nwritten;
return;
}
if (!writer.isOpened() && !filenames.empty()) {
if (!writer.open(filenames, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, im.size())) {
throw std::runtime_error("Can't open video writer");
}
writer.write(im);
}
}
};

View File

@@ -0,0 +1,92 @@
// Copyright (C) 2020-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief a header file for performance metrics calculation class
* @file performance_metrics.hpp
*/
#pragma once
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
#include "utils/ocv_common.hpp"
class PerformanceMetrics {
public:
using Clock = std::chrono::steady_clock;
using TimePoint = std::chrono::time_point<Clock>;
using Duration = Clock::duration;
using Ms = std::chrono::duration<double, std::ratio<1, 1000>>;
using Sec = std::chrono::duration<double, std::ratio<1, 1>>;
struct Metrics {
double latency;
double fps;
};
enum MetricTypes {
ALL,
FPS,
LATENCY
};
PerformanceMetrics(Duration timeWindow = std::chrono::seconds(1));
void update(TimePoint lastRequestStartTime,
const cv::Mat& frame,
cv::Point position = {15, 30},
int fontFace = cv::FONT_HERSHEY_COMPLEX,
double fontScale = 0.75,
cv::Scalar color = {200, 10, 10},
int thickness = 2, MetricTypes metricType = ALL);
void update(TimePoint lastRequestStartTime);
/// Paints metrics over provided mat
/// @param frame frame to paint over
/// @param position left top corner of text block
/// @param fontScale font scale
/// @param color font color
/// @param thickness font thickness
void paintMetrics(const cv::Mat& frame,
cv::Point position = { 15, 30 },
int fontFace = cv::FONT_HERSHEY_COMPLEX,
double fontScale = 0.75,
cv::Scalar color = { 200, 10, 10 },
int thickness = 2, MetricTypes metricType = ALL) const;
Metrics getLast() const;
Metrics getTotal() const;
void logTotal() const;
private:
struct Statistic {
Duration latency;
Duration period;
int frameCount;
Statistic() {
latency = Duration::zero();
period = Duration::zero();
frameCount = 0;
}
void combine(const Statistic& other) {
latency += other.latency;
period += other.period;
frameCount += other.frameCount;
}
};
Duration timeWindowSize;
Statistic lastMovingStatistic;
Statistic currentMovingStatistic;
Statistic totalStatistic;
TimePoint lastUpdateTime;
bool firstFrameProcessed;
};
void logLatencyPerStage(double readLat, double preprocLat, double inferLat, double postprocLat, double renderLat);

View File

@@ -0,0 +1,26 @@
/*
// Copyright (C) 2021-2024 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.
*/
#pragma once
#include <opencv2/core.hpp>
struct SharedMatAllocator {
const cv::Mat mat;
void* allocate(size_t bytes, size_t) {return bytes <= mat.rows * mat.step[0] ? mat.data : nullptr;}
void deallocate(void*, size_t, size_t) noexcept {}
bool is_equal(const SharedMatAllocator& other) const noexcept {return this == &other;}
};

View File

@@ -0,0 +1,99 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief a header file with logging facility for common samples
* @file log.hpp
*/
#pragma once
#include <iostream>
#include <string>
namespace slog {
/**
* @class LogStreamEndLine
* @brief The LogStreamEndLine class implements an end line marker for a log stream
*/
class LogStreamEndLine { };
static constexpr LogStreamEndLine endl;
/**
* @class LogStreamBoolAlpha
* @brief The LogStreamBoolAlpha class implements bool printing for a log stream
*/
class LogStreamBoolAlpha { };
static constexpr LogStreamBoolAlpha boolalpha;
/**
* @class LogStream
* @brief The LogStream class implements a stream for sample logging
*/
class LogStream {
std::string _prefix;
std::ostream* _log_stream;
bool _new_line;
public:
/**
* @brief A constructor. Creates a LogStream object
* @param prefix The prefix to print
*/
LogStream(const std::string &prefix, std::ostream& log_stream)
: _prefix(prefix), _new_line(true) {
_log_stream = &log_stream;
}
/**
* @brief A stream output operator to be used within the logger
* @param arg Object for serialization in the logger message
*/
template<class T>
LogStream &operator<<(const T &arg) {
if (_new_line) {
(*_log_stream) << "[ " << _prefix << " ] ";
_new_line = false;
}
(*_log_stream) << arg;
return *this;
}
// Specializing for LogStreamEndLine to support slog::endl
LogStream& operator<< (const LogStreamEndLine &/*arg*/) {
_new_line = true;
(*_log_stream) << std::endl;
return *this;
}
// Specializing for LogStreamBoolAlpha to support slog::boolalpha
LogStream& operator<< (const LogStreamBoolAlpha &/*arg*/) {
(*_log_stream) << std::boolalpha;
return *this;
}
// Specializing for std::vector and std::list
template<template<class, class> class Container, class T>
LogStream& operator<< (const Container<T, std::allocator<T>>& container) {
for (const auto& el : container) {
*this << el << slog::endl;
}
return *this;
}
};
static LogStream info("INFO", std::cout);
static LogStream debug("DEBUG", std::cout);
static LogStream warn("WARNING", std::cout);
static LogStream err("ERROR", std::cerr);
} // namespace slog

View File

@@ -0,0 +1,160 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <algorithm>
#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <utility>
#include <set>
#include <string>
#include <thread>
#include <vector>
#include <opencv2/core/core.hpp>
#include "utils/performance_metrics.hpp"
// VideoFrame can represent not a single image but the whole grid
class VideoFrame {
public:
typedef std::shared_ptr<VideoFrame> Ptr;
VideoFrame(unsigned sourceID, int64_t frameId, const cv::Mat& frame = cv::Mat()) :
sourceID{sourceID}, frameId{frameId}, frame{frame} {}
virtual ~VideoFrame() = default; // A user has to define how it is reconstructed
const unsigned sourceID;
const int64_t frameId;
cv::Mat frame;
PerformanceMetrics::TimePoint timestamp;
};
class Worker;
class Task {
public:
explicit Task(VideoFrame::Ptr sharedVideoFrame, float priority = 0):
sharedVideoFrame{sharedVideoFrame}, priority{priority} {}
virtual bool isReady() = 0;
virtual void process() = 0;
virtual ~Task() = default;
std::string name;
VideoFrame::Ptr sharedVideoFrame; // it is possible that two tasks try to draw on the same cvMat
const float priority;
};
struct HigherPriority {
bool operator()(const std::shared_ptr<Task>& lhs, const std::shared_ptr<Task>& rhs) const {
return lhs->priority > rhs->priority
|| (lhs->priority == rhs->priority && lhs->sharedVideoFrame->frameId < rhs->sharedVideoFrame->frameId)
|| (lhs->priority == rhs->priority && lhs->sharedVideoFrame->frameId == rhs->sharedVideoFrame->frameId && lhs < rhs);
}
};
class Worker {
public:
explicit Worker(unsigned threadNum):
threadPool(threadNum), running{false} {}
~Worker() {
stop();
}
void runThreads() {
running = true;
for (std::thread& t : threadPool) {
t = std::thread(&Worker::threadFunc, this);
}
}
void push(std::shared_ptr<Task> task) {
tasksMutex.lock();
tasks.insert(task);
tasksMutex.unlock();
tasksCondVar.notify_one();
}
void threadFunc() {
while (running) {
std::unique_lock<std::mutex> lk(tasksMutex);
while (running && tasks.empty()) {
tasksCondVar.wait(lk);
}
try {
auto it = std::find_if(tasks.begin(), tasks.end(), [](const std::shared_ptr<Task>& task){return task->isReady();});
if (tasks.end() != it) {
const std::shared_ptr<Task> task = std::move(*it);
tasks.erase(it);
lk.unlock();
task->process();
}
} catch (...) {
std::lock_guard<std::mutex> lock{exceptionMutex};
if (nullptr == currentException) {
currentException = std::current_exception();
stop();
}
}
}
}
void stop() {
running = false;
tasksCondVar.notify_all();
}
void join() {
for (auto& t : threadPool) {
t.join();
}
if (nullptr != currentException) {
std::rethrow_exception(currentException);
}
}
private:
std::condition_variable tasksCondVar;
std::set<std::shared_ptr<Task>, HigherPriority> tasks;
std::mutex tasksMutex;
std::vector<std::thread> threadPool;
std::atomic<bool> running;
std::exception_ptr currentException;
std::mutex exceptionMutex;
};
template <class C> class ConcurrentContainer {
public:
C container;
mutable std::mutex mutex;
bool lockedEmpty() const noexcept {
std::lock_guard<std::mutex> lock{mutex};
return container.empty();
}
typename C::size_type lockedSize() const noexcept {
std::lock_guard<std::mutex> lock{mutex};
return container.size();
}
void lockedPushBack(const typename C::value_type& value) {
std::lock_guard<std::mutex> lock{mutex};
container.push_back(value);
}
bool lockedTryPop(typename C::value_type& value) {
mutex.lock();
if (!container.empty()) {
value = container.back();
container.pop_back();
mutex.unlock();
return true;
} else {
mutex.unlock();
return false;
}
}
operator C() const {
std::lock_guard<std::mutex> lock{mutex};
return container;
}
};

View File

@@ -0,0 +1,66 @@
/*
// Copyright (C) 2021-2024 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.
*/
#include <string>
#include <utility>
#include <opencv2/core.hpp>
struct ImageResult;
class Visualizer {
private:
// names of window and trackbar
std::string winName = "Image Processing Demo (press A for help)";
std::string trackbarName = "Orig/Diff | Res";
// images info
cv::Size resolution = cv::Size(1000, 600);
bool isResolutionSet = false;
cv::Mat inputImg = cv::Mat(resolution, CV_32FC3, 0.);
cv::Mat resultImg = cv::Mat(resolution, CV_32FC3, 0.);
cv::Mat displayImg = cv::Mat(resolution, CV_32FC3, 0.);
// trackbar info
std::string mode = "result";
bool isTrackbarShown = false;
int slider = 1;
// help message
bool isHelpShown = false;
std::string helpMessage[4] = {"Use R to display the result",
"Use O to display the orig with result",
"Use V to display the diff with result",
"Esc or Q to quit"};
void addTrackbar();
void disableTrackbar();
void setResolution(cv::Size& newResolution);
void markImage(cv::Mat& image, const std::pair<std::string, std::string>& marks, float alpha);
void drawSweepLine(cv::Mat& image);
void changeDisplayImg();
public:
Visualizer(const std::string& type = "");
cv::Size getSize();
// change display image for new input and result images
cv::Mat renderResultData(ImageResult result, cv::Size& newResolution);
// show display image or specified value
void show(cv::Mat img = cv::Mat());
void handleKey(int key);
};