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+ }
0 commit comments