1

I have a deep learning python code which gets an image and returns an array of detection positions in that image.

I have developed all other parts of my project in C++ and I want to pass an opencv Mat image (cv::Mat) to python script and it would return an array to C++.

Is there a way to pass these data types from/to C++/Python? what funtions should I use?

4
  • if it is not performance critical, the easiest is to store it in a file and read it again Commented Mar 5, 2019 at 11:42
  • @user463035818 Performance is exactly my problem. Commented Mar 5, 2019 at 11:49
  • 2
    Is this what you are looking for? Just pass a pointer to the data to your python script and unpack it as struct. Then pack it again to pass it back to your C++ application. Or compile your python script down to C with Cython and try to integrate that into your code. Another way to do it would be to entirely do it in C++ or Python and not mix it, if performance is really critical. Tensorflow has a C++ API. Commented Mar 5, 2019 at 12:23
  • "Is there a way to pass these data types from/to C++/Python?" -- Of course, have a look at how the OpenCV Python bindings work. There are multiple posts on SO about how to handle mapping Mat to ndarray. There's also Boost.Numpy. Commented Mar 5, 2019 at 13:10

1 Answer 1

1

So this can be achieved in 2-ways.

1.) you can develop a backend server for python and then expose those api's , and then you can use your api's to send the data and get the results back.

2.) also you can make your python function called by a c++ program ,for that you can use lib like Python.h to call and receive data from the python script.

Example lets say you have a python file named aiDosomething.py which has a function detect which do the detection task. then in c++ you can do something like

#include <Python.h>

int main()
{

pModule = PyImport_ImportModule("aiDosomething");

//chose the function in file Sample

pFunc = PyObject_GetAttrString(pModule,"detect");

pDict = PyModule_GetDict(pModule);   // Create a dictionary for the contents of the module.

//make the args to be passes in the function

pArgs = PyTuple_New(1);     // Create a Python tuple to hold the arguments to the method.
PyTuple_SetItem(pArgs, 0, insertArray); // insertArray can be your cv::mat array

//run the code and get the result
PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
return 0;

}

for keeping things simple you can put your python code and c++ under same directory.

Sign up to request clarification or add additional context in comments.

6 Comments

This is the error returned when I want to use cv::Mat as insertArray: "no suitable conversion from "cv::Mat" to "PyObject *" exists"
try converting cv::mat array to normal array and send it
how is that possible? Just type casting?
you can think of the image as an 2D array,you first initialize the 2d array having shape equal to cv::Mat, then you can simply iterate through every value in the cv::mat and then setting the value to the normal c++ array. Also if you dont do much in c++ you can directly pass your imagePath to python and load the image in the function itself.
I did it with no error by PyTuple_SetItem(pArgs, 0, (PyObject*)(char*)img.data); but nothing is shown in python script. Even print(img.shape()) does nothing!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.