Skip to content

Instantly share code, notes, and snippets.

@Holt59
Created May 17, 2022 11:07
Show Gist options
  • Save Holt59/422c279f92ab607fffba33a0dc5e2b87 to your computer and use it in GitHub Desktop.
Save Holt59/422c279f92ab607fffba33a0dc5e2b87 to your computer and use it in GitHub Desktop.
#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