|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Intel Corporation |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include <nanobind/ndarray.h> |
| 7 | +#include <nanobind/operators.h> |
| 8 | +#include <nanobind/stl/map.h> |
| 9 | +#include <nanobind/stl/string.h> |
| 10 | +#include <nanobind/stl/unique_ptr.h> |
| 11 | +#include <nanobind/stl/vector.h> |
| 12 | + |
| 13 | +#include "models/classification_model.h" |
| 14 | +#include "models/results.h" |
| 15 | +#include "py_utils.hpp" |
| 16 | + |
| 17 | +namespace pyutils = vision::nanobind::utils; |
| 18 | + |
| 19 | +void init_classification(nb::module_& m) { |
| 20 | + nb::class_<ClassificationResult::Classification>(m, "Classification") |
| 21 | + .def(nb::init<unsigned int, const std::string, float>()) |
| 22 | + .def_rw("id", &ClassificationResult::Classification::id) |
| 23 | + .def_rw("label", &ClassificationResult::Classification::label) |
| 24 | + .def_rw("score", &ClassificationResult::Classification::score); |
| 25 | + |
| 26 | + nb::class_<ClassificationResult, ResultBase>(m, "ClassificationResult") |
| 27 | + .def(nb::init<>()) |
| 28 | + .def_ro("topLabels", &ClassificationResult::topLabels) |
| 29 | + .def("__repr__", &ClassificationResult::operator std::string) |
| 30 | + .def_prop_ro( |
| 31 | + "feature_vector", |
| 32 | + [](ClassificationResult& r) { |
| 33 | + if (!r.feature_vector) { |
| 34 | + return nb::ndarray<float, nb::numpy, nb::c_contig>(); |
| 35 | + } |
| 36 | + |
| 37 | + return nb::ndarray<float, nb::numpy, nb::c_contig>(r.feature_vector.data(), |
| 38 | + r.feature_vector.get_shape().size(), |
| 39 | + r.feature_vector.get_shape().data()); |
| 40 | + }, |
| 41 | + nb::rv_policy::reference_internal) |
| 42 | + .def_prop_ro( |
| 43 | + "saliency_map", |
| 44 | + [](ClassificationResult& r) { |
| 45 | + if (!r.saliency_map) { |
| 46 | + return nb::ndarray<float, nb::numpy, nb::c_contig>(); |
| 47 | + } |
| 48 | + |
| 49 | + return nb::ndarray<float, nb::numpy, nb::c_contig>(r.saliency_map.data(), |
| 50 | + r.saliency_map.get_shape().size(), |
| 51 | + r.saliency_map.get_shape().data()); |
| 52 | + }, |
| 53 | + nb::rv_policy::reference_internal); |
| 54 | + |
| 55 | + nb::class_<ClassificationModel, ImageModel>(m, "ClassificationModel") |
| 56 | + .def_static( |
| 57 | + "create_model", |
| 58 | + [](const std::string& model_path, |
| 59 | + const std::map<std::string, nb::object>& configuration, |
| 60 | + bool preload, |
| 61 | + const std::string& device) { |
| 62 | + auto ov_any_config = ov::AnyMap(); |
| 63 | + for (const auto& item : configuration) { |
| 64 | + ov_any_config[item.first] = pyutils::py_object_to_any(item.second, item.first); |
| 65 | + } |
| 66 | + |
| 67 | + return ClassificationModel::create_model(model_path, ov_any_config, preload, device); |
| 68 | + }, |
| 69 | + nb::arg("model_path"), |
| 70 | + nb::arg("configuration") = ov::AnyMap({}), |
| 71 | + nb::arg("preload") = true, |
| 72 | + nb::arg("device") = "AUTO") |
| 73 | + |
| 74 | + .def("__call__", |
| 75 | + [](ClassificationModel& self, const nb::ndarray<>& input) { |
| 76 | + return self.infer(pyutils::wrap_np_mat(input)); |
| 77 | + }) |
| 78 | + .def("infer_batch", [](ClassificationModel& self, const std::vector<nb::ndarray<>> inputs) { |
| 79 | + std::vector<ImageInputData> input_mats; |
| 80 | + input_mats.reserve(inputs.size()); |
| 81 | + |
| 82 | + for (const auto& input : inputs) { |
| 83 | + input_mats.push_back(pyutils::wrap_np_mat(input)); |
| 84 | + } |
| 85 | + |
| 86 | + return self.inferBatch(input_mats); |
| 87 | + }); |
| 88 | +} |
0 commit comments