-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
57 lines (46 loc) · 1.67 KB
/
Copy pathmain.cpp
File metadata and controls
57 lines (46 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
#include <vector>
#include "pybind11/include/pybind11/pybind11.h"
#include "pybind11/include/pybind11/embed.h"
#include "pybind11/include/pybind11/stl.h"
namespace py = pybind11;
const bool USE_EMBEDDED_PYTHON = true;
int main()
{
std::cout << "C++: program entry point" << std::endl;
try
{
if (USE_EMBEDDED_PYTHON)
{
Py_SetPath(L"python_embedded\\;python_embedded\\python310.zip");
std::cout << "C++: using EMBEDDED Python" << std::endl;
}
else
{
std::cout << "C++: using DEFAULT Python" << std::endl;
}
std::cout << "C++: acquiring Python interpreter" << std::endl;
py::scoped_interpreter guard{};
// Printing Python search paths for debug
std::cout << "-----\nPython search paths:" << std::endl;
PyRun_SimpleString("import sys\nprint(sys.path)");
std::cout << "-----" << std::endl;
std::cout << "C++: requesting function from Python" << std::endl;
py::function py_calc_avg =
py::reinterpret_borrow<py::function>(
py::module::import("numpy").attr("mean")
);
// Making test array
std::vector<uint8_t> array = { 1, 2, 5, 10, 17 };
// Caclulating average value by calling Python
std::cout << "C++: calling Python function" << std::endl;
py::object py_result = py_calc_avg(array);
std::cout << "C++: received value: " << py_result.cast<float>() << std::endl;
}
catch (std::exception& e)
{
std::cout << e.what() << '\n';
}
std::cout << "C++: program finished successfully" << std::endl;
}