Skip to content

Commit 753ef18

Browse files
committed
Fixes opencv compilation, fixes kivy#1313
Updates to latest opencv version `4.0.1`. The compilation was tested on host/target python3 and produced `libopencv_core.a` & `libopencv_ml.a` static libraries. Runtime wasn't tested.
1 parent 8c1d5c8 commit 753ef18

File tree

3 files changed

+22
-92
lines changed

3 files changed

+22
-92
lines changed

ci/constants.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ class TargetPython(Enum):
8282
# requires `libpq-dev` system dependency e.g. for `pg_config` binary
8383
'psycopg2',
8484
'netifaces',
85-
# https://github.com/kivy/python-for-android/issues/1315 ?
86-
'opencv',
8785
'protobuf_cpp',
8886
# most likely some setup in the Docker container, because it works in host
8987
'pyjnius', 'pyopenal',

pythonforandroid/recipes/opencv/__init__.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,43 @@
99

1010

1111
class OpenCVRecipe(NDKRecipe):
12-
version = '2.4.10.1'
13-
url = 'https://github.com/Itseez/opencv/archive/{version}.zip'
14-
# md5sum = '2ddfa98e867e6611254040df841186dc'
12+
version = '4.0.1'
13+
url = 'https://github.com/opencv/opencv/archive/{version}.zip'
1514
depends = ['numpy']
16-
patches = ['patches/p4a_build-2.4.10.1.patch']
17-
generated_libraries = ['cv2.so']
18-
19-
def prebuild_arch(self, arch):
20-
self.apply_patches(arch)
2115

2216
def get_recipe_env(self, arch):
2317
env = super(OpenCVRecipe, self).get_recipe_env(arch)
24-
env['PYTHON_ROOT'] = self.ctx.get_python_install_dir()
2518
env['ANDROID_NDK'] = self.ctx.ndk_dir
2619
env['ANDROID_SDK'] = self.ctx.sdk_dir
27-
env['SITEPACKAGES_PATH'] = self.ctx.get_site_packages_dir()
2820
return env
2921

22+
def should_build(self, arch):
23+
return True
24+
3025
def build_arch(self, arch):
31-
with current_directory(self.get_build_dir(arch.arch)):
26+
build_dir = os.path.join(self.get_build_dir(arch.arch), 'build')
27+
shprint(sh.mkdir, '-p', build_dir)
28+
with current_directory(build_dir):
3229
env = self.get_recipe_env(arch)
33-
cvsrc = self.get_build_dir(arch.arch)
34-
lib_dir = os.path.join(self.ctx.get_python_install_dir(), "lib")
35-
3630
shprint(sh.cmake,
37-
'-DP4A=ON', '-DANDROID_ABI={}'.format(arch.arch),
38-
'-DCMAKE_TOOLCHAIN_FILE={}/platforms/android/android.toolchain.cmake'.format(cvsrc),
39-
'-DPYTHON_INCLUDE_PATH={}/include/python2.7'.format(env['PYTHON_ROOT']),
40-
'-DPYTHON_LIBRARY={}/lib/libpython2.7.so'.format(env['PYTHON_ROOT']),
41-
'-DPYTHON_NUMPY_INCLUDE_DIR={}/numpy/core/include'.format(env['SITEPACKAGES_PATH']),
31+
'-DANDROID_ABI={}'.format(arch.arch),
32+
'-DCMAKE_TOOLCHAIN_FILE={}/build/cmake/android.toolchain.cmake'.format(self.ctx.ndk_dir),
33+
'-DPYTHON_NUMPY_INCLUDE_DIR={}/numpy/core/include'.format(self.ctx.get_site_packages_dir()),
4234
'-DANDROID_EXECUTABLE={}/tools/android'.format(env['ANDROID_SDK']),
43-
'-DBUILD_TESTS=OFF', '-DBUILD_PERF_TESTS=OFF',
35+
'-DBUILD_TESTS=OFF', '-DBUILD_PERF_TESTS=OFF', '-DENABLE_TESTING=OFF',
4436
'-DBUILD_EXAMPLES=OFF', '-DBUILD_ANDROID_EXAMPLES=OFF',
45-
'-DPYTHON_PACKAGES_PATH={}'.format(env['SITEPACKAGES_PATH']),
46-
cvsrc,
37+
'-DBUILD_opencv_imgproc=OFF', '-DBUILD_opencv_flann=OFF',
38+
'-DBUILD_opencv_python3=ON',
39+
'-DBUILD_WITH_STANDALONE_TOOLCHAIN=ON',
40+
'-DPYTHON_PACKAGES_PATH={}'.format(self.ctx.get_site_packages_dir()),
41+
'-DANDROID_STANDALONE_TOOLCHAIN={}'.format(self.ctx.ndk_dir),
42+
'-DANDROID_NATIVE_API_LEVEL={}'.format(self.ctx.android_api),
43+
self.get_build_dir(arch.arch),
4744
_env=env)
48-
shprint(sh.make, '-j', str(cpu_count()), 'opencv_python')
45+
shprint(sh.make, '-j', str(cpu_count()))
4946
shprint(sh.cmake, '-DCOMPONENT=python', '-P', './cmake_install.cmake')
50-
sh.cp('-a', sh.glob('./lib/{}/lib*.so'.format(arch.arch)), lib_dir)
47+
sh.cp('-a', sh.glob('./lib/{}/lib*.a'.format(arch.arch)), self.ctx.get_libs_dir(arch.arch))
48+
self.ctx.get_libs_dir(arch.arch)
5149

5250

5351
recipe = OpenCVRecipe()

pythonforandroid/recipes/opencv/patches/p4a_build-2.4.10.1.patch

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)