python 通过pybind11向C++ dll 传递数组 图像

  时间:2019-01-11
本文章向大家介绍python 通过pybind11向C++ dll 传递数组 图像,主要包括python 通过pybind11向C++ dll 传递数组 图像使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

传递python中的List

pybind11 很贴心地帮你把 vector<T> 跟 python 的 list 做好了转换,你只需要 #include <pybind11/stl.h> 即可 [1]

C++端代码


#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
using std::vector;

namespace py = pybind11;



vector<float> ListMul(vector<float>& in_list, float coef) {
	vector<float> ret_vec;
	ret_vec.clear();
	ret_vec.reserve(in_list.size()); // Requests that the vector capacity be at least enough to contain n elements.
	for (size_t i = 0; i < in_list.size(); ++i) {
		int v = in_list[i];
		ret_vec.push_back(coef * v);
	}
	return ret_vec;
}


PYBIND11_MODULE(example, m) {
	m.doc() = "pass and return a list";
	m.def("ListMul", &ListMul, "example function");
}

python端调用

import example

print(example.ListMul([1.0, 2, 3, 4, 5, 6], 5))

类似地还有map<Tk, Tv>在 python 里对应的就是 Dict[Tk, Tv] [1]。

传递Numpy 数组

两个数组相加的案例 [2]

C++端代码

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>


namespace py = pybind11;



py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) {
	// read input arrays buffer_info
	py::buffer_info buf1 = input1.request(), buf2 = input2.request();

	if (buf1.size != buf2.size)
		throw std::runtime_error("Input shapes must match");

	// allocate the output buffer
	py::array_t<double> result = py::array_t<double>(buf1.size);

	py::buffer_info buf3 = result.request(); // acquire buffer info

	double *ptr1 = (double *)buf1.ptr, *ptr2 = (double *)buf2.ptr, *ptr3 = (double *)buf3.ptr;
	size_t high = buf1.shape[0];
	size_t width = buf1.shape[1];

	// Add both arrays
	for (size_t idy = 0; idy < high; idy++)
	{
		for (size_t idx = 0; idx < width; idx++)
		{
			int curIdx = idy*width + idx;
			ptr3[curIdx] = ptr1[curIdx] + ptr2[curIdx];

		}
	}

	/* Reshape result to have same shape as input */
	result.resize({ high, width });

	return result;
}

PYBIND11_MODULE(example, m) {
	m.doc() = "Add two vectors using pybind11"; // optional module docstring
	m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
}

python端调用

import numpy as np
import example

a = np.ones((10,3))
b = np.ones((10,3)) * 3
c = example.add_arrays(a, b)
print(c)

These numpy array arguments can either be generic py:array or typed py:array_t<double>. The properties of the numpy array can be obtained by calling its request method. This returns a struct of the following form [3]:

struct buffer_info {
    void *ptr;
    size_t itemsize;
    std::string format;
    int ndim;
    std::vector<size_t> shape;
    std::vector<size_t> strides;
};

参考

[1] pybind11 入門玩樂筆記(二) – Dboy Liao – Medium

[2] http://christianjaques.ch/pybind-is-great/

[3] http://people.duke.edu/~ccc14/cspy/18G_C++_Python_pybind11.html

这里面提供了很多丰富的案例

https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html?highlight=numpy#arrays

https://docs.python.org/3/c-api/buffer.html

 

 

 

上一页 下一页

原文地址:http://www.manongjc.com/article/42928.html