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