Converting python code with OpenCV to C using Cyth

2019-08-02 23:46发布

Hi i am facing issues When i import cv2 in my python code and convert the python code to C using Cython and then using the .c and .h file in Visual studio and using it in the main.cpp .It says no module named cv2 occurs when i execute the Main.cpp . I am able to create the openCVTestcode.c and openCVTestcode.h file and i have added to the Visual studio project and calling it from main.cpp. it works fine while importing numpy and it prints the result also. The issue is when i use openCv. i get no module named cv2 error. Should we add openCV path or so in Visual studio project? But this was not needed for Numpy.

When i use cimport cv2 it says cimported module has no attribute 'imread'. I have a openCVTestcode.pyx file and i am compiling usingpython -m cython openCVTestcode.pyxfrom the anaconda prompt. From this i get the .c and .h files which i am adding to a visual studio project and calling the function in the .h files from the main.cpp

The code for openCVtestcode.pyx is :

import cv2
cdef public void openCVtest():
    image = cv2.imread("Test.png")
    cv2.imshow("test",image)
    cv2.waitKey(0)

The code for Main.cpp is

int main(void) {
Py_Initialize();  //Needed!
PyInit_openCVTestcode();     //Needed! called PyInit_hello() for Python3
openCVtest();
Py_Finalize();    //Needed!
return 0;
}

I am able to build it. But the .exe created says

Traceback (most recent call last):
File "openCVTestcode.pyx", line 2, in init openCVTestcode
ModuleNotFoundError: No module named 'cv2'
Exception ignored in: 'openCVTestcode.openCVtest'
Traceback (most recent call last):
File "openCVTestcode.pyx", line 2, in init openCVTestcode
ModuleNotFoundError: No module named 'cv2'

0条回答
登录 后发表回答