Created
May 17, 2022 11:07
-
-
Save Holt59/422c279f92ab607fffba33a0dc5e2b87 to your computer and use it in GitHub Desktop.
Working version of https://gist.github.com/RomanSteinberg/e7a2d6a99f198670be48bede0b9f1cb3
This file contains 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
#include <vector> | |
#include <iostream> | |
#include <pybind11/pybind11.h> | |
#include <pybind11/embed.h> | |
#include <pybind11/stl.h> | |
#include <pybind11/numpy.h> | |
struct FrameInfo | |
{ | |
using first_type = std::vector<int>; | |
using second_type = std::vector<std::vector<std::vector<int>>>; | |
private: | |
first_type result1; | |
second_type result2; | |
public: | |
FrameInfo(first_type const &a, second_type const &b) : result1(a), result2(b){}; | |
}; | |
namespace py = pybind11; | |
FrameInfo *send_frame(int height, int width, const uint8_t *data) | |
{ | |
try { | |
py::module_ api_module = py::module_::import("api"); | |
pybind11::dtype dt("uint8"); | |
const int shape[3] = {height, width, 3}; | |
py::array py_frame(dt, shape, data); | |
py::tuple result = api_module.attr("refine_frame")(py_frame); | |
auto b = result[0].cast<std::vector<int>>(); | |
auto c = result[1].cast<std::vector<std::vector<std::vector<int>>>>(); | |
return new FrameInfo(b, c); | |
} | |
catch (py::error_already_set const& ex) { | |
std::cout << "error: " << ex.what() << "\n"; | |
return nullptr; | |
} | |
} | |
void test_foo() | |
{ | |
int height = 4, width = 5, depth = 3; | |
std::vector<uint8_t> data(height * width * depth); | |
for (int i = 0; i < 10; ++i) | |
{ std::cout << "loop " << i << "\n"; | |
FrameInfo *fi = send_frame(height, width, data.data()); | |
if (!fi) { | |
break; | |
} | |
} | |
} | |
int main() | |
{ | |
py::scoped_interpreter guard{}; // start the interpreter and keep it alive | |
py::exec(R"( | |
import site | |
site.addsitedir("lib") | |
print("site.addsitedir() done") | |
)"); | |
test_foo(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment