Skip to content

Instantly share code, notes, and snippets.

@RomanSteinberg
Last active May 17, 2022 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RomanSteinberg/e7a2d6a99f198670be48bede0b9f1cb3 to your computer and use it in GitHub Desktop.
Save RomanSteinberg/e7a2d6a99f198670be48bede0b9f1cb3 to your computer and use it in GitHub Desktop.
C++ calls Python routines (pybind11)
# actually src/api.py, but gist demands no subdirectirories
from typing import Tuple, List
import numpy as np
def refine_frame(a: np.ndarray) -> Tuple[List[int], List[List[int]]]:
print(a.shape)
return [100, 100, 200, 200], [[[120, 120], [130, 120]]]
#include <opencv2/opencv.hpp>
#include "main.hpp" // everything needed for embedding
using namespace std;
using namespace cv;
FrameInfo* send_frame(int height, int width, const uint8_t* data) {
py::scoped_interpreter guard{}; // start the interpreter and keep it alive
py::exec(R"(
import sys
sys.path.append('src')
)");
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);
}
void test_foo() {
Mat frame;
VideoCapture cap(0);
if (!cap.isOpened()) {
cout << "cannot open camera";
}
while (true) {
cap >> frame;
imshow("Display window", frame);
FrameInfo* fi = send_frame(frame.rows, frame.cols, frame.data);
if ((char) waitKey(1) == (char) 27)
break;
}
}
int main() {
test_foo();
return EXIT_SUCCESS;
}
#ifndef TEST_APP_CPP_LIB_H
#define TEST_APP_CPP_LIB_H
#include <vector>
struct FrameInfo {
using first_type = std::vector<int>;
using second_type = std::vector<std::vector<std::vector<int>>>;
private:
first_type result1;
first_type result2;
public:
FrameInfo(first_type& a, first_type& b): result1(a), result2(b) {};
};
FrameInfo* send_frame(int height, int width, const uint8_t* data);
#endif //TEST_APP_CPP_LIB_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment