I have a python file in which I import cv2.When I do not import cv2,I can use cython to convert the python file to a .so file and I can use it in java by jni.But when I import cv2 and add relative code,the .so file produced by cython can't be use.When I use it in java,it will thrown a error which indicate progress can't import cv2.How to solve this problem?Thanks.
Here is error:
/root/miniconda3/lib/python3.8/site-packages/numpy/__init__.py:142: UserWarning: mkl-service package
failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init
OpenCV bindings requires "numpy" package.
Install it via command:
pip install numpy
AttributeError: module '__main__' has no attribute 'JNI_API_testFunction'
error
I use python to run my Test.py file and it work out.It means that my python can import cv2 and numpy correctly.I use this command to convert my Test.py to a .so file:
python Setup.py build_ext --inplace
Here is Setup.py:
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
sourcefiles = ['Test.pyx', 'main.c']
extensions = [Extension("Test", sourcefiles,
include_dirs=['/root/java/jdk-18.0.2/include/',
'/root/java/jdk-18.0.2/include/linux/',
'/root/miniconda3/include/python3.8/'],
library_dirs=['/root/miniconda3/lib/'],
libraries=['python3.8']),
]
setup(ext_modules=cythonize(extensions, language_level = 3))
I use System.load() function to load my .so file in java.If I do not import cv2 and numpy and delete code about cv2 and numpy,I can call testFunction().It means that my java code is correct.But if I import cv2 and numpy and I add code about cv2 and numpy,for example,cv2.imread(),I will get a error. I think it can find cv2 and numpy because it have run numpy's init.py file.I think the problem result from cython because I get athis information in a Chinese community: The directory level information of the code file will be lost in the file code processed by cython. There is no difference between the code after c.py conversion and the code after M / c.py conversion.In a.py or b.py code, if there is a reference to C Py module, the loss of directory information will cause the two to report an error when executing import M.C, because the corresponding module cannot be found. the author say the problem can be solved by modifying the source code of cython,but he don't give the method.