pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

C++ generates data in real time, how to receive data in real time through pybind11 in python?

Open GalSang17 opened this issue 2 years ago • 1 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [X] Consider asking first in the Gitter chat room or in a Discussion.

What version (or hash if on master) of pybind11 are you using?

2.11.0dev1

Problem description

I am now using the deep learning framework to generate some target object detection box boxes and other information, and then I want to use pybind11 to encapsulate this information for my python function to use. It should be noted that the information of these box boxes is updated and changed in real time Yes, how should I get the data passed through pybind11 in the python function, and these data also change together. At present, the way I try to get all the empty values, I am a novice in C++, all now It's hard to keep going, hope you can help, thanks

my envs: ubuntu18.04 python3.8.10 gcc 7.5.0

Reproducible example code

my codes:
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/stl.h>
#include <nlohmann/json.hpp>
#include <mutex>

namespace py = pybind11;

void initializeInterpreter() {
    py::initialize_interpreter();
}

static std::once_flag initFlag;
static py::dict py_json;

py::dict get_data() {
    return py_json;
}

void convert_json_cxx_2_py(const nlohmann::json& json_data) {
    std::call_once(initFlag, initializeInterpreter);
    py::gil_scoped_acquire acquire;
    py_json = py::module::import("json").attr("loads")(json_data.dump());
    get_data();
    py::gil_scoped_release release;
}

PYBIND11_MODULE(cxx_json_2_py, m) {
    m.def("convert_json_cxx_2_py", &convert_json_cxx_2_py, "Convert nlohmann::json to Python JSON");
    m.def("get_data", &get_data, "Get data converted from nlohmann::json to Python JSON", py::return_value_policy::reference);
    m.attr("py_json") = py::cast(&py_json);
}

function call location:
nlohmann::json jsondata = helmet_alarm(img, res, rtsp, 10, "no-helmet");

if (!jsondata.empty()) {
    convert_json_cxx_2_py(jsondata);
}
And the function input parameters nlohmann::json jsondat here are changed in real time

the python scipt:
import cxx_json_2_py

r = cxx_json_2_py.py_json
# r = cxx_json_2_py.get_data() or
print('res', r)

the python function return:
res {}
core dumped

Is this a regression? Put the last known working version here if it is.

No response

GalSang17 avatar Jul 12 '23 09:07 GalSang17