Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
cpp:
- changed-files:
- any-glob-to-any-file:
- model_api/cpp/**
- src/cpp/**

python:
- changed-files:
- any-glob-to-any-file:
- model_api/python/**
- src/python/**

tests:
- changed-files:
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ endmacro()

find_package(OpenCV REQUIRED COMPONENTS imgcodecs)

set (ENABLE_PY_BINDINGS OFF)
set(ENABLE_PY_BINDINGS OFF)
add_subdirectory(../../src/cpp ${Samples_BINARY_DIR}/src/cpp)

add_example(NAME asynchronous_api SOURCES ./asynchronous_api/main.cpp DEPENDENCIES model_api)
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/asynchronous_api/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <iomanip>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <openvino/openvino.hpp>
#include <stdexcept>
#include <string>
Expand Down
5 changes: 2 additions & 3 deletions examples/cpp/synchronous_api/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
#include <iomanip>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <openvino/openvino.hpp>
#include <stdexcept>
#include <string>
Expand All @@ -36,7 +35,7 @@ int main(int argc, char* argv[]) try {
auto result = model->infer(image);

// Process detections
for (auto& obj : result->objects) {
for (auto obj : *result) {
std::cout << " " << std::left << std::setw(9) << obj.label << " | " << std::setw(10) << obj.confidence << " | "
<< std::setw(4) << int(obj.x) << " | " << std::setw(4) << int(obj.y) << " | " << std::setw(4)
<< int(obj.x + obj.width) << " | " << std::setw(4) << int(obj.y + obj.height) << "\n";
Expand Down
53 changes: 50 additions & 3 deletions src/cpp/models/include/models/results.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ struct ClassificationResult : public ResultBase {
};

struct DetectedObject : public cv::Rect2f {
DetectedObject() {}
DetectedObject(const cv::Rect2f& rect) : cv::Rect2f(rect) {}

size_t labelID;
std::string label;
float confidence;
Expand All @@ -161,11 +164,55 @@ struct DetectedObject : public cv::Rect2f {
struct DetectionResult : public ResultBase {
DetectionResult(int64_t frameId = -1, const std::shared_ptr<MetaData>& metaData = nullptr)
: ResultBase(frameId, metaData) {}
std::vector<DetectedObject> objects;
ov::Tensor saliency_map, feature_vector; // Contan "saliency_map" and "feature_vector" model outputs if such exist

cv::Mat_<float> scores;
cv::Mat_<float> bboxes;
cv::Mat_<int> labels;
std::vector<std::string> label_names;
ov::Tensor saliency_map, feature_vector; // Contain "saliency_map" and "feature_vector" model outputs if such exist

struct DetectionIterator {
const DetectionResult& result;
size_t index;

DetectionIterator(const DetectionResult& result, size_t index) : result(result), index(index) {}

bool operator!=(const DetectionIterator& other) const {
return index != other.index;
}

DetectionIterator& operator++() {
++index;
return *this;
}

DetectedObject operator*() const {
return result.getObject(index);
}
};

DetectionIterator begin() const {
return DetectionIterator(*this, 0);
}

DetectionIterator end() const {
return DetectionIterator(*this, size());
}

size_t size() const {
return label_names.size();
}

DetectedObject getObject(size_t idx) const {
DetectedObject result(bboxes.at<cv::Rect2f>(idx));
result.labelID = labels.at<size_t>(idx);
result.label = label_names[idx];
result.confidence = scores.at<float>(idx);
return result;
}

friend std::ostream& operator<<(std::ostream& os, const DetectionResult& prediction) {
for (const DetectedObject& obj : prediction.objects) {
for (const DetectedObject obj : prediction) {
os << obj << "; ";
}
try {
Expand Down
50 changes: 40 additions & 10 deletions src/cpp/models/src/detection_model_ssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ std::unique_ptr<ResultBase> ModelSSD::postprocessSingleOutput(InferenceResult& i
}
}

size_t detections_num = 0;
for (size_t i = 0; i < numAndStep.detectionsNum; i++) {
float confidence = detections[i * numAndStep.objectSize + 2];
if (confidence > confidence_threshold) {
++detections_num;
}
}
result->label_names.reserve(detections_num);
result->labels = cv::Mat(1, detections_num, CV_32S);
result->scores = cv::Mat(1, detections_num, CV_32F);
result->bboxes = cv::Mat(1, detections_num, CV_32FC4);

for (size_t i = 0; i < numAndStep.detectionsNum; i++) {
float image_id = detections[i * numAndStep.objectSize + 0];
if (image_id < 0) {
Expand All @@ -141,11 +153,7 @@ std::unique_ptr<ResultBase> ModelSSD::postprocessSingleOutput(InferenceResult& i

/** Filtering out objects with confidence < confidence_threshold probability **/
if (confidence > confidence_threshold) {
DetectedObject desc;

desc.confidence = confidence;
desc.labelID = static_cast<size_t>(detections[i * numAndStep.objectSize + 1]);
desc.label = getLabelName(desc.labelID);
cv::Rect2f desc;
desc.x =
clamp(round((detections[i * numAndStep.objectSize + 3] * netInputWidth - padLeft) * invertedScaleX),
0.f,
Expand All @@ -164,7 +172,13 @@ std::unique_ptr<ResultBase> ModelSSD::postprocessSingleOutput(InferenceResult& i
0.f,
floatInputImgHeight) -
desc.y;
result->objects.push_back(desc);

size_t det_idx = result->label_names.size();
auto label_idx = static_cast<int>(detections[i * numAndStep.objectSize + 1]);
result->labels.at<int>(det_idx) = label_idx;
result->scores.at<float>(det_idx) = confidence;
result->bboxes.at<cv::Rect2f>(det_idx) = desc;
result->label_names.push_back(getLabelName(label_idx));
}
}

Expand Down Expand Up @@ -200,16 +214,25 @@ std::unique_ptr<ResultBase> ModelSSD::postprocessMultipleOutputs(InferenceResult
float widthScale = scores ? netInputWidth : 1.0f;
float heightScale = scores ? netInputHeight : 1.0f;

size_t detections_num = 0;
for (size_t i = 0; i < numAndStep.detectionsNum; i++) {
float confidence = scores ? scores[i] : boxes[i * numAndStep.objectSize + 4];
if (confidence > confidence_threshold) {
++detections_num;
}
}
result->label_names.reserve(detections_num);
result->labels = cv::Mat(1, detections_num, CV_32S);
result->scores = cv::Mat(1, detections_num, CV_32F);
result->bboxes = cv::Mat(1, detections_num, CV_32FC4);

for (size_t i = 0; i < numAndStep.detectionsNum; i++) {
float confidence = scores ? scores[i] : boxes[i * numAndStep.objectSize + 4];

/** Filtering out objects with confidence < confidence_threshold probability **/
if (confidence > confidence_threshold) {
DetectedObject desc;
cv::Rect2f desc;

desc.confidence = confidence;
desc.labelID = labels[i];
desc.label = getLabelName(desc.labelID);
desc.x = clamp_and_round((boxes[i * numAndStep.objectSize] * widthScale - padLeft) * invertedScaleX,
0.f,
floatInputImgWidth);
Expand All @@ -229,6 +252,13 @@ std::unique_ptr<ResultBase> ModelSSD::postprocessMultipleOutputs(InferenceResult
if (desc.width * desc.height >= box_area_threshold) {
result->objects.push_back(desc);
}

size_t det_idx = result->label_names.size();
auto label_idx = static_cast<int>(labels[i]);
result->labels.at<int>(det_idx) = label_idx;
result->scores.at<float>(det_idx) = confidence;
result->bboxes.at<cv::Rect2f>(det_idx) = desc;
result->label_names.push_back(getLabelName(label_idx));
}
}

Expand Down
Loading