Skip to content

Commit b54f769

Browse files
committed
anomaly
1 parent 5ce76c3 commit b54f769

File tree

4 files changed

+104
-27
lines changed

4 files changed

+104
-27
lines changed

src/cpp/py_bindings/py_anomaly.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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/anomaly_model.h"
13+
#include "models/results.h"
14+
#include "py_utils.hpp"
15+
16+
namespace pyutils = vision::nanobind::utils;
17+
18+
void init_anomaly_detection(nb::module_& m) {
19+
nb::class_<AnomalyModel, ImageModel>(m, "AnomalyDetection")
20+
.def_static(
21+
"create_model",
22+
[](const std::string& model_path,
23+
const std::map<std::string, nb::object>& configuration,
24+
bool preload,
25+
const std::string& device) {
26+
auto ov_any_config = ov::AnyMap();
27+
for (const auto& item : configuration) {
28+
ov_any_config[item.first] = pyutils::py_object_to_any(item.second, item.first);
29+
}
30+
31+
return AnomalyModel::create_model(model_path, ov_any_config, preload, device);
32+
},
33+
nb::arg("model_path"),
34+
nb::arg("configuration") = ov::AnyMap({}),
35+
nb::arg("preload") = true,
36+
nb::arg("device") = "AUTO")
37+
38+
.def("__call__",
39+
[](AnomalyModel& self, const nb::ndarray<>& input) {
40+
return self.infer(pyutils::wrap_np_mat(input));
41+
})
42+
.def("infer_batch",
43+
[](AnomalyModel& self, const std::vector<nb::ndarray<>> inputs) {
44+
std::vector<ImageInputData> input_mats;
45+
input_mats.reserve(inputs.size());
46+
47+
for (const auto& input : inputs) {
48+
input_mats.push_back(pyutils::wrap_np_mat(input));
49+
}
50+
51+
return self.inferBatch(input_mats);
52+
})
53+
.def_prop_ro_static("__model__", [](nb::object) {
54+
return AnomalyModel::ModelType;
55+
});
56+
57+
nb::class_<AnomalyResult, ResultBase>(m, "AnomalyResult")
58+
.def(nb::init<int64_t, std::shared_ptr<MetaData>>(), nb::arg("frameId") = -1, nb::arg("metaData") = nullptr)
59+
.def_prop_ro(
60+
"anomaly_map",
61+
[](AnomalyResult& r) {
62+
return nb::ndarray<uint8_t, nb::numpy, nb::c_contig>(r.anomaly_map.data,
63+
{static_cast<size_t>(r.anomaly_map.rows),
64+
static_cast<size_t>(r.anomaly_map.cols),
65+
static_cast<size_t>(r.anomaly_map.channels())});
66+
},
67+
nb::rv_policy::reference_internal)
68+
.def_ro("pred_boxes", &AnomalyResult::pred_boxes)
69+
.def_ro("pred_label", &AnomalyResult::pred_label)
70+
.def_prop_ro(
71+
"pred_mask",
72+
[](AnomalyResult& r) {
73+
return nb::ndarray<uint8_t, nb::numpy, nb::c_contig>(r.pred_mask.data,
74+
{static_cast<size_t>(r.pred_mask.rows),
75+
static_cast<size_t>(r.pred_mask.cols),
76+
static_cast<size_t>(r.pred_mask.channels())});
77+
},
78+
nb::rv_policy::reference_internal)
79+
.def_ro("pred_score", &AnomalyResult::pred_score);
80+
}

src/cpp/py_bindings/py_keypoint_detection.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,26 @@ void init_keypoint_detection(nb::module_& m) {
5656

5757
nb::class_<KeypointDetectionResult, ResultBase>(m, "KeypointDetectionResult")
5858
.def(nb::init<int64_t, std::shared_ptr<MetaData>>(), nb::arg("frameId") = -1, nb::arg("metaData") = nullptr)
59-
.def_prop_ro("keypoints", [](const KeypointDetectionResult& result) {
60-
if (!result.poses.empty()) {
61-
std::vector<size_t> shape = {result.poses[0].keypoints.size(), 2};
62-
return nb::ndarray<float, nb::numpy, nb::c_contig>(
63-
const_cast<void*>(static_cast<const void*>(result.poses[0].keypoints.data())),
64-
shape.size(),
65-
shape.data());
66-
}
67-
return nb::ndarray<float, nb::numpy, nb::c_contig>();
68-
},
69-
nb::rv_policy::reference_internal)
70-
.def_prop_ro("scores", [](const KeypointDetectionResult& result) {
71-
if (!result.poses.empty()) {
72-
std::vector<size_t> shape = {result.poses[0].scores.size()};
73-
return nb::ndarray<float, nb::numpy, nb::c_contig>(
74-
const_cast<void*>(static_cast<const void*>(result.poses[0].scores.data())),
75-
shape.size(),
76-
shape.data());
77-
}
78-
return nb::ndarray<float, nb::numpy, nb::c_contig>();
79-
},
80-
nb::rv_policy::reference_internal
81-
);
59+
.def_prop_ro(
60+
"keypoints",
61+
[](const KeypointDetectionResult& result) {
62+
if (!result.poses.empty()) {
63+
return nb::ndarray<float, nb::numpy, nb::c_contig>(
64+
const_cast<void*>(static_cast<const void*>(result.poses[0].keypoints.data())),
65+
{static_cast<size_t>(result.poses[0].keypoints.size()), 2});
66+
}
67+
return nb::ndarray<float, nb::numpy, nb::c_contig>();
68+
},
69+
nb::rv_policy::reference_internal)
70+
.def_prop_ro(
71+
"scores",
72+
[](const KeypointDetectionResult& result) {
73+
if (!result.poses.empty()) {
74+
return nb::ndarray<float, nb::numpy, nb::c_contig>(
75+
const_cast<void*>(static_cast<const void*>(result.poses[0].scores.data())),
76+
{static_cast<size_t>(result.poses[0].scores.size())});
77+
}
78+
return nb::ndarray<float, nb::numpy, nb::c_contig>();
79+
},
80+
nb::rv_policy::reference_internal);
8281
}

src/cpp/py_bindings/py_segmentation.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ void init_segmentation(nb::module_& m) {
5151

5252
return self.inferBatch(input_mats);
5353
})
54-
.def("postprocess",
55-
[](SegmentationModel& self, InferenceResult& infResult) {
56-
return self.postprocess(infResult);
57-
})
5854
.def_prop_ro_static("__model__", [](nb::object) {
5955
return SegmentationModel::ModelType;
6056
});

src/cpp/py_bindings/py_vision_api.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ void init_classification(nb::module_& m);
1212
void init_segmentation(nb::module_& m);
1313
void init_instance_segmentation(nb::module_& m);
1414
void init_keypoint_detection(nb::module_& m);
15+
void init_anomaly_detection(nb::module_& m);
1516

1617
NB_MODULE(py_model_api, m) {
1718
m.doc() = "Nanobind binding for OpenVINO Vision API library";
@@ -20,4 +21,5 @@ NB_MODULE(py_model_api, m) {
2021
init_keypoint_detection(m);
2122
init_segmentation(m);
2223
init_instance_segmentation(m);
24+
init_anomaly_detection(m);
2325
}

0 commit comments

Comments
 (0)