Skip to content

Commit

Permalink
Preview of DetectionResult refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sovrasov committed Jan 16, 2025
1 parent ae3241a commit 2394fff
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
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
9 changes: 7 additions & 2 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 @@ -41,6 +40,12 @@ int main(int argc, char* argv[]) try {
<< 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";
}

// could be improved with an iterator
for (size_t i = 0; i < result->size(); ++i) {
std::cout << result->getDetection(i) << "\n";
}

} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
Expand Down
26 changes: 24 additions & 2 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,8 +164,27 @@ 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

// memory to forward as np.array
cv::Mat_<float> scores;
cv::Mat_<float> bboxes;
cv::Mat_<int> labels;
std::vector<std::string> label_names;

std::vector<DetectedObject> objects; // to remove
ov::Tensor saliency_map, feature_vector; // Contain "saliency_map" and "feature_vector" model outputs if such exist

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

DetectedObject getDetection(size_t idx) {
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) {
Expand Down
24 changes: 24 additions & 0 deletions src/cpp/models/src/detection_model_ssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ std::unique_ptr<ResultBase> ModelSSD::postprocessSingleOutput(InferenceResult& i
}
}

result->label_names.reserve(numAndStep.detectionsNum);
for (size_t i = 0; i < numAndStep.detectionsNum; i++) {
float image_id = detections[i * numAndStep.objectSize + 0];
if (image_id < 0) {
Expand Down Expand Up @@ -165,6 +166,12 @@ std::unique_ptr<ResultBase> ModelSSD::postprocessSingleOutput(InferenceResult& i
floatInputImgHeight) -
desc.y;
result->objects.push_back(desc);

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

Expand Down Expand Up @@ -200,6 +207,18 @@ 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];

Expand Down Expand Up @@ -229,6 +248,11 @@ std::unique_ptr<ResultBase> ModelSSD::postprocessMultipleOutputs(InferenceResult
if (desc.width * desc.height >= box_area_threshold) {
result->objects.push_back(desc);
}
auto label_idx = static_cast<int>(labels[i]);
result->labels.at<int>(i) = label_idx;
result->scores.at<float>(i) = confidence;
result->bboxes.at<cv::Rect2f>(i) = cv::Rect2f(desc.x, desc.y, desc.width, desc.height);
result->label_names.push_back(getLabelName(label_idx));
}
}

Expand Down

0 comments on commit 2394fff

Please sign in to comment.