diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:19:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:20:02 +0000 |
commit | 58daab21cd043e1dc37024a7f99b396788372918 (patch) | |
tree | 96771e43bb69f7c1c2b0b4f7374cb74d7866d0cb /ml/dlib/dlib/python/pybind_utils.h | |
parent | Releasing debian version 1.43.2-1. (diff) | |
download | netdata-58daab21cd043e1dc37024a7f99b396788372918.tar.xz netdata-58daab21cd043e1dc37024a7f99b396788372918.zip |
Merging upstream version 1.44.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ml/dlib/dlib/python/pybind_utils.h')
-rw-r--r-- | ml/dlib/dlib/python/pybind_utils.h | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/ml/dlib/dlib/python/pybind_utils.h b/ml/dlib/dlib/python/pybind_utils.h new file mode 100644 index 000000000..7f94cf32d --- /dev/null +++ b/ml/dlib/dlib/python/pybind_utils.h @@ -0,0 +1,82 @@ +// Copyright (C) 2013 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_PYBIND_UtILS_Hh_ +#define DLIB_PYBIND_UtILS_Hh_ + +#include <pybind11/pybind11.h> +#include <vector> +#include <string> +#include <dlib/serialize.h> + +namespace py = pybind11; + +template <typename T> +std::vector<T> python_list_to_vector ( + const py::list& obj +) +/*! + ensures + - converts a python object into a std::vector<T> and returns it. +!*/ +{ + std::vector<T> vect(len(obj)); + for (unsigned long i = 0; i < vect.size(); ++i) + { + vect[i] = obj[i].cast<T>(); + } + return vect; +} + +template <typename T> +py::list vector_to_python_list ( + const std::vector<T>& vect +) +/*! + ensures + - converts a std::vector<T> into a python list object. +!*/ +{ + py::list obj; + for (unsigned long i = 0; i < vect.size(); ++i) + obj.append(vect[i]); + return obj; +} + +template <typename T> +void extend_vector_with_python_list ( + std::vector<T> &v, + const py::list &l +) +/*! + ensures + - appends items from a python list to the end of std::vector<T>. +!*/ +{ + for (const auto &item : l) + v.push_back(item.cast<T>()); +} + +// ---------------------------------------------------------------------------------------- + +template <typename T> +std::shared_ptr<T> load_object_from_file ( + const std::string& filename +) +/*! + ensures + - deserializes an object of type T from the given file and returns it. +!*/ +{ + std::ifstream fin(filename.c_str(), std::ios::binary); + if (!fin) + throw dlib::error("Unable to open " + filename); + auto obj = std::make_shared<T>(); + deserialize(*obj, fin); + return obj; +} + +// ---------------------------------------------------------------------------------------- + + +#endif // DLIB_PYBIND_UtILS_Hh_ + |