Skip to content

Commit 605ab88

Browse files
committed
detection
1 parent 752647b commit 605ab88

File tree

4 files changed

+139
-4
lines changed

4 files changed

+139
-4
lines changed

src/cpp/py_bindings/py_anomaly.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ void init_anomaly_detection(nb::module_& m) {
6868
.def_prop_ro(
6969
"pred_boxes",
7070
[](AnomalyResult& r) {
71-
return nb::ndarray<int, nb::numpy, nb::c_contig>(r.pred_boxes.data(),
72-
{static_cast<size_t>(r.pred_boxes.size()), 4});
71+
return nb::ndarray<int, nb::numpy, nb::c_contig>(r.pred_boxes.data(), {r.pred_boxes.size(), 4});
7372
},
7473
nb::rv_policy::reference_internal)
7574
.def_ro("pred_label", &AnomalyResult::pred_label)
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include <nanobind/ndarray.h>
6+
#include <nanobind/operators.h>
7+
#include <nanobind/stl/map.h>
8+
#include <nanobind/stl/string.h>
9+
#include <nanobind/stl/unique_ptr.h>
10+
#include <nanobind/stl/vector.h>
11+
12+
#include "models/detection_model.h"
13+
#include "models/results.h"
14+
#include "py_utils.hpp"
15+
16+
namespace pyutils = vision::nanobind::utils;
17+
18+
void init_detection(nb::module_& m) {
19+
nb::class_<DetectionModel, ImageModel>(m, "DetectionModel")
20+
.def_static(
21+
"create_model",
22+
[](const std::string& model_path,
23+
const std::map<std::string, nb::object>& configuration,
24+
std::string model_type,
25+
bool preload,
26+
const std::string& device) {
27+
auto ov_any_config = ov::AnyMap();
28+
for (const auto& item : configuration) {
29+
ov_any_config[item.first] = pyutils::py_object_to_any(item.second, item.first);
30+
}
31+
32+
return DetectionModel::create_model(model_path, ov_any_config, model_type, preload, device);
33+
},
34+
nb::arg("model_path"),
35+
nb::arg("configuration") = ov::AnyMap({}),
36+
nb::arg("model_type") = "",
37+
nb::arg("preload") = true,
38+
nb::arg("device") = "AUTO")
39+
40+
.def("__call__",
41+
[](DetectionModel& self, const nb::ndarray<>& input) {
42+
return self.infer(pyutils::wrap_np_mat(input));
43+
})
44+
.def("infer_batch", [](DetectionModel& self, const std::vector<nb::ndarray<>> inputs) {
45+
std::vector<ImageInputData> input_mats;
46+
input_mats.reserve(inputs.size());
47+
48+
for (const auto& input : inputs) {
49+
input_mats.push_back(pyutils::wrap_np_mat(input));
50+
}
51+
52+
return self.inferBatch(input_mats);
53+
});
54+
55+
nb::class_<DetectionResult, ResultBase>(m, "DetectionResult")
56+
.def(nb::init<>())
57+
.def_prop_ro(
58+
"saliency_map",
59+
[](DetectionResult& r) {
60+
if (!r.saliency_map) {
61+
return nb::ndarray<float, nb::numpy, nb::c_contig>();
62+
}
63+
64+
return nb::ndarray<float, nb::numpy, nb::c_contig>(r.saliency_map.data(),
65+
r.saliency_map.get_shape().size(),
66+
r.saliency_map.get_shape().data());
67+
},
68+
nb::rv_policy::reference_internal)
69+
.def_prop_ro(
70+
"feature_vector",
71+
[](DetectionResult& r) {
72+
if (!r.feature_vector) {
73+
return nb::ndarray<float, nb::numpy, nb::c_contig>();
74+
}
75+
76+
return nb::ndarray<float, nb::numpy, nb::c_contig>(r.feature_vector.data(),
77+
r.feature_vector.get_shape().size(),
78+
r.feature_vector.get_shape().data());
79+
},
80+
nb::rv_policy::reference_internal)
81+
.def_prop_ro(
82+
"label_names",
83+
[](DetectionResult& r) {
84+
std::vector<std::string> labels;
85+
std::transform(r.objects.begin(),
86+
r.objects.end(),
87+
std::back_inserter(labels),
88+
[](const DetectedObject& obj) {
89+
return obj.label;
90+
});
91+
92+
return labels;
93+
},
94+
nb::rv_policy::reference_internal)
95+
.def_prop_ro(
96+
"scores",
97+
[](DetectionResult& r) {
98+
std::vector<float> scores;
99+
std::transform(r.objects.begin(),
100+
r.objects.end(),
101+
std::back_inserter(scores),
102+
[](const DetectedObject& obj) {
103+
return obj.confidence;
104+
});
105+
return nb::ndarray<float, nb::numpy, nb::c_contig>(scores.data(), {scores.size()}).cast();
106+
},
107+
nb::rv_policy::move)
108+
.def_prop_ro(
109+
"labels",
110+
[](DetectionResult& r) {
111+
std::vector<size_t> labels;
112+
std::transform(r.objects.begin(),
113+
r.objects.end(),
114+
std::back_inserter(labels),
115+
[](const DetectedObject& obj) {
116+
return obj.labelID;
117+
});
118+
return nb::ndarray<float, nb::numpy, nb::c_contig>(labels.data(), {labels.size()}).cast();
119+
},
120+
nb::rv_policy::move)
121+
.def_prop_ro(
122+
"bboxes",
123+
[](DetectionResult& r) {
124+
std::vector<cv::Rect2f> bboxes;
125+
std::transform(r.objects.begin(),
126+
r.objects.end(),
127+
std::back_inserter(bboxes),
128+
[](const DetectedObject& obj) {
129+
return cv::Rect2f(obj.x, obj.y, obj.width, obj.height);
130+
});
131+
return nb::ndarray<float, nb::numpy, nb::c_contig>(bboxes.data(), {bboxes.size(), 4}).cast();
132+
},
133+
nb::rv_policy::move);
134+
}

src/cpp/py_bindings/py_keypoint_detection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void init_keypoint_detection(nb::module_& m) {
6262
if (!result.poses.empty()) {
6363
return nb::ndarray<float, nb::numpy, nb::c_contig>(
6464
const_cast<void*>(static_cast<const void*>(result.poses[0].keypoints.data())),
65-
{static_cast<size_t>(result.poses[0].keypoints.size()), 2});
65+
{result.poses[0].keypoints.size(), 2});
6666
}
6767
return nb::ndarray<float, nb::numpy, nb::c_contig>();
6868
},
@@ -73,7 +73,7 @@ void init_keypoint_detection(nb::module_& m) {
7373
if (!result.poses.empty()) {
7474
return nb::ndarray<float, nb::numpy, nb::c_contig>(
7575
const_cast<void*>(static_cast<const void*>(result.poses[0].scores.data())),
76-
{static_cast<size_t>(result.poses[0].scores.size())});
76+
{result.poses[0].scores.size()});
7777
}
7878
return nb::ndarray<float, nb::numpy, nb::c_contig>();
7979
},

src/cpp/py_bindings/py_vision_api.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void init_segmentation(nb::module_& m);
1313
void init_instance_segmentation(nb::module_& m);
1414
void init_keypoint_detection(nb::module_& m);
1515
void init_anomaly_detection(nb::module_& m);
16+
void init_detection(nb::module_& m);
1617

1718
NB_MODULE(py_model_api, m) {
1819
m.doc() = "Nanobind binding for OpenVINO Vision API library";
@@ -22,4 +23,5 @@ NB_MODULE(py_model_api, m) {
2223
init_segmentation(m);
2324
init_instance_segmentation(m);
2425
init_anomaly_detection(m);
26+
init_detection(m);
2527
}

0 commit comments

Comments
 (0)