-
-
Save RomanSteinberg/e7a2d6a99f198670be48bede0b9f1cb3 to your computer and use it in GitHub Desktop.
C++ calls Python routines (pybind11)
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
# 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]]] |
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 <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; | |
} |
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
#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