-
Notifications
You must be signed in to change notification settings - Fork 19
CVS-160560 nanobindings for keypoint detection and segmentation #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
18e2551
model loading
mgumowsk 6e8dca8
renamed
mgumowsk 9ec7c68
changes
mgumowsk 6cbfc27
change
mgumowsk 6c78d65
license header
mgumowsk 745fdcf
clang-format
mgumowsk d706107
EOL fix for precommits
mgumowsk 5ce76c3
match API to python version, classification, keypoint detection
mgumowsk b54f769
anomaly
mgumowsk 00f8593
no postprocess in instance seg
mgumowsk bafaa5f
end of line
mgumowsk ecef0fa
add pred_boxes
mgumowsk 752647b
segmentation properties
mgumowsk 605ab88
detection
mgumowsk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright (C) 2025 Intel Corporation | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <nanobind/ndarray.h> | ||
| #include <nanobind/operators.h> | ||
| #include <nanobind/stl/map.h> | ||
| #include <nanobind/stl/string.h> | ||
| #include <nanobind/stl/unique_ptr.h> | ||
| #include <nanobind/stl/vector.h> | ||
|
|
||
| #include "models/instance_segmentation.h" | ||
| #include "models/results.h" | ||
| #include "py_utils.hpp" | ||
|
|
||
| namespace pyutils = vision::nanobind::utils; | ||
|
|
||
| void init_instance_segmentation(nb::module_& m) { | ||
| nb::class_<MaskRCNNModel, ImageModel>(m, "MaskRCNNModel") | ||
| .def_static( | ||
| "create_model", | ||
| [](const std::string& model_path, | ||
| const std::map<std::string, nb::object>& configuration, | ||
| bool preload, | ||
| const std::string& device) { | ||
| auto ov_any_config = ov::AnyMap(); | ||
| for (const auto& item : configuration) { | ||
| ov_any_config[item.first] = pyutils::py_object_to_any(item.second, item.first); | ||
| } | ||
|
|
||
| return MaskRCNNModel::create_model(model_path, ov_any_config, preload, device); | ||
| }, | ||
| nb::arg("model_path"), | ||
| nb::arg("configuration") = ov::AnyMap({}), | ||
| nb::arg("preload") = true, | ||
| nb::arg("device") = "AUTO") | ||
|
|
||
| .def("__call__", | ||
| [](MaskRCNNModel& self, const nb::ndarray<>& input) { | ||
| return self.infer(pyutils::wrap_np_mat(input)); | ||
| }) | ||
| .def("infer_batch", | ||
| [](MaskRCNNModel& self, const std::vector<nb::ndarray<>> inputs) { | ||
| std::vector<ImageInputData> input_mats; | ||
| input_mats.reserve(inputs.size()); | ||
|
|
||
| for (const auto& input : inputs) { | ||
| input_mats.push_back(pyutils::wrap_np_mat(input)); | ||
| } | ||
|
|
||
| return self.inferBatch(input_mats); | ||
| }) | ||
| .def("postprocess", | ||
| [](MaskRCNNModel& self, InferenceResult& infResult) { | ||
| return self.postprocess(infResult); | ||
| }) | ||
| .def_prop_ro_static("__model__", [](nb::object) { | ||
| return MaskRCNNModel::ModelType; | ||
| }); | ||
|
|
||
| nb::class_<InstanceSegmentationResult, ResultBase>(m, "InstanceSegmentationResult") | ||
| .def(nb::init<int64_t, std::shared_ptr<MetaData>>(), nb::arg("frameId") = -1, nb::arg("metaData") = nullptr) | ||
| .def_prop_ro( | ||
| "feature_vector", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Iseg result also contains a saliency map as a vector of |
||
| [](InstanceSegmentationResult& r) { | ||
| if (!r.feature_vector) { | ||
| return nb::ndarray<float, nb::numpy, nb::c_contig>(); | ||
| } | ||
|
|
||
| return nb::ndarray<float, nb::numpy, nb::c_contig>(r.feature_vector.data(), | ||
| r.feature_vector.get_shape().size(), | ||
| r.feature_vector.get_shape().data()); | ||
| }, | ||
| nb::rv_policy::reference_internal); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright (C) 2025 Intel Corporation | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| #include <nanobind/ndarray.h> | ||
| #include <nanobind/operators.h> | ||
| #include <nanobind/stl/map.h> | ||
| #include <nanobind/stl/string.h> | ||
| #include <nanobind/stl/unique_ptr.h> | ||
| #include <nanobind/stl/vector.h> | ||
|
|
||
| #include "models/keypoint_detection.h" | ||
| #include "models/results.h" | ||
| #include "py_utils.hpp" | ||
|
|
||
| namespace pyutils = vision::nanobind::utils; | ||
|
|
||
| void init_keypoint_detection(nb::module_& m) { | ||
| nb::class_<KeypointDetectionModel, ImageModel>(m, "KeypointDetectionModel") | ||
| .def_static( | ||
| "create_model", | ||
| [](const std::string& model_path, | ||
| const std::map<std::string, nb::object>& configuration, | ||
| bool preload, | ||
| const std::string& device) { | ||
| auto ov_any_config = ov::AnyMap(); | ||
| for (const auto& item : configuration) { | ||
| ov_any_config[item.first] = pyutils::py_object_to_any(item.second, item.first); | ||
| } | ||
|
|
||
| return KeypointDetectionModel::create_model(model_path, ov_any_config, preload, device); | ||
| }, | ||
| nb::arg("model_path"), | ||
| nb::arg("configuration") = ov::AnyMap({}), | ||
| nb::arg("preload") = true, | ||
| nb::arg("device") = "AUTO") | ||
|
|
||
| .def("__call__", | ||
| [](KeypointDetectionModel& self, const nb::ndarray<>& input) { | ||
| return self.infer(pyutils::wrap_np_mat(input)); | ||
| }) | ||
| .def("infer_batch", | ||
| [](KeypointDetectionModel& self, const std::vector<nb::ndarray<>> inputs) { | ||
| std::vector<ImageInputData> input_mats; | ||
| input_mats.reserve(inputs.size()); | ||
|
|
||
| for (const auto& input : inputs) { | ||
| input_mats.push_back(pyutils::wrap_np_mat(input)); | ||
| } | ||
|
|
||
| return self.inferBatch(input_mats); | ||
| }) | ||
| .def("postprocess", | ||
| [](KeypointDetectionModel& self, InferenceResult& infResult) { | ||
| return self.postprocess(infResult); | ||
| }) | ||
| .def_prop_ro_static("__model__", [](nb::object) { | ||
| return KeypointDetectionModel::ModelType; | ||
| }); | ||
|
|
||
| nb::class_<KeypointDetectionResult, ResultBase>(m, "KeypointDetectionResult") | ||
| .def(nb::init<int64_t, std::shared_ptr<MetaData>>(), nb::arg("frameId") = -1, nb::arg("metaData") = nullptr) | ||
| .def_ro("poses", &KeypointDetectionResult::poses); | ||
|
|
||
| nb::class_<DetectedKeypoints>(m, "DetectedKeypoints") | ||
| .def(nb::init<>()) | ||
| .def_ro("keypoints", &DetectedKeypoints::keypoints) | ||
| .def_ro("scores", &DetectedKeypoints::scores); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright (C) 2025 Intel Corporation | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <nanobind/ndarray.h> | ||
| #include <nanobind/operators.h> | ||
| #include <nanobind/stl/map.h> | ||
| #include <nanobind/stl/string.h> | ||
| #include <nanobind/stl/unique_ptr.h> | ||
| #include <nanobind/stl/vector.h> | ||
|
|
||
| #include "models/results.h" | ||
| #include "models/segmentation_model.h" | ||
| #include "py_utils.hpp" | ||
|
|
||
| namespace pyutils = vision::nanobind::utils; | ||
|
|
||
| void init_segmentation(nb::module_& m) { | ||
| nb::class_<SegmentationModel, ImageModel>(m, "SegmentationModel") | ||
| .def_static( | ||
| "create_model", | ||
| [](const std::string& model_path, | ||
| const std::map<std::string, nb::object>& configuration, | ||
| bool preload, | ||
| const std::string& device) { | ||
| auto ov_any_config = ov::AnyMap(); | ||
| for (const auto& item : configuration) { | ||
| ov_any_config[item.first] = pyutils::py_object_to_any(item.second, item.first); | ||
| } | ||
|
|
||
| return SegmentationModel::create_model(model_path, ov_any_config, preload, device); | ||
| }, | ||
| nb::arg("model_path"), | ||
| nb::arg("configuration") = ov::AnyMap({}), | ||
| nb::arg("preload") = true, | ||
| nb::arg("device") = "AUTO") | ||
|
|
||
| .def("__call__", | ||
| [](SegmentationModel& self, const nb::ndarray<>& input) { | ||
| return self.infer(pyutils::wrap_np_mat(input)); | ||
| }) | ||
| .def("infer_batch", | ||
| [](SegmentationModel& self, const std::vector<nb::ndarray<>> inputs) { | ||
| std::vector<ImageInputData> input_mats; | ||
| input_mats.reserve(inputs.size()); | ||
|
|
||
| for (const auto& input : inputs) { | ||
| input_mats.push_back(pyutils::wrap_np_mat(input)); | ||
| } | ||
|
|
||
| return self.inferBatch(input_mats); | ||
| }) | ||
| .def("postprocess", | ||
| [](SegmentationModel& self, InferenceResult& infResult) { | ||
| return self.postprocess(infResult); | ||
| }) | ||
| .def_prop_ro_static("__model__", [](nb::object) { | ||
| return SegmentationModel::ModelType; | ||
| }); | ||
|
|
||
| nb::class_<ImageResult, ResultBase>(m, "ImageResult") | ||
| .def(nb::init<int64_t, std::shared_ptr<MetaData>>(), nb::arg("frameId") = -1, nb::arg("metaData") = nullptr) | ||
| .def_prop_ro( | ||
| "resultImage", | ||
| [](ImageResult& r) { | ||
| return nb::ndarray<uint8_t, nb::numpy, nb::c_contig>(r.resultImage.data, | ||
| {static_cast<size_t>(r.resultImage.rows), | ||
| static_cast<size_t>(r.resultImage.cols), | ||
| static_cast<size_t>(r.resultImage.channels())}); | ||
| }, | ||
| nb::rv_policy::reference_internal); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.