python cv2 calls from c++

asked 2020-08-05 03:35:36 -0600

2ros0 gravatar image

updated 2020-08-05 11:54:47 -0600

Hi,

I have embedded python in my c++ code. All other modules work fine, except for the cv2 module. Any call to cv2 functions hangs. For instance cv2.imread

I'm using python 2.7.12

Here is the code

int main()
{
    // initialize python
    Py_Initialize();

    // import our test module
    PyObject * opencv_test_module = PyImport_ImportModule("opencv_test");

    // retrieve read_image() from our module
    PyObject * read_image = PyObject_GetAttrString(numpy_test_module, "read_image");

    // print the array by calling 'print_matrix'
    PyObject* return_value = PyObject_CallObject(read_image, NULL); // program hangs here (!)

    // ... further processing ...
    return 0;
 }

and here is my module opencv_test.py

import cv2
def read_image(): 
    print cv2.__version__ # 3.3.1-dev
    print "[read_image]" # program reaches here 
    i = cv2.imread("/path/to/file", 0) # program hangs here -- also hangs with other cv2 calls 
    print "[read_image] end"
edit retag flag offensive close merge delete

Comments

Why???????

sjhalayka gravatar imagesjhalayka ( 2020-08-05 14:45:31 -0600 )edit
1

Why embed python opencv in a c++ program? Because it's not just cv2, I'm using a lot of other code using python modules like numpy which already are in a good state.

2ros0 gravatar image2ros0 ( 2020-08-05 19:13:37 -0600 )edit

I am using python 3.9 and opencv 4.4.0. As matter of fact, I cannot help you with older version. Very sorry.

supra56 gravatar imagesupra56 ( 2020-08-05 22:04:50 -0600 )edit
1

calling a c++library (opencv) through python wrappers from c++ seems indeed quite weird ;)

berak gravatar imageberak ( 2020-08-06 10:43:36 -0600 )edit

haha yeah I see that, it's just there's a lot of python code in the other project that I would like embedded in this project which is in c++ ¯_(ツ)_/¯

2ros0 gravatar image2ros0 ( 2020-08-06 13:00:33 -0600 )edit

hmm, does yoor python wrapping code catch any c++ exceptions ?

berak gravatar imageberak ( 2020-08-06 13:50:05 -0600 )edit

It’s painful to consider. LOL

sjhalayka gravatar imagesjhalayka ( 2020-08-06 18:50:56 -0600 )edit

it's a c++ application with some embedded python using the python c api: https://docs.python.org/3/c-api/index...

2ros0 gravatar image2ros0 ( 2020-08-07 15:09:47 -0600 )edit