Skip to content

Instantly share code, notes, and snippets.

@ElHacker
Last active March 15, 2018 09:11
Show Gist options
  • Save ElHacker/1579b9d822db747a94427f92b697396e to your computer and use it in GitHub Desktop.
Save ElHacker/1579b9d822db747a94427f92b697396e to your computer and use it in GitHub Desktop.
Python-for-android opencv 3.4.1 Recipe
import os
import sh
from pythonforandroid.toolchain import (
NDKRecipe,
Recipe,
current_directory,
info,
shprint,
)
from multiprocessing import cpu_count
class OpenCVRecipe(NDKRecipe):
version = '3.4.1'
url = 'https://github.com/opencv/opencv/archive/{version}.zip'
#md5sum = '2ddfa98e867e6611254040df841186dc'
depends = ['numpy']
# patches = ['patches/p4a_build-2.4.10.1.patch']
generated_libraries = ['cv2.so']
def prebuild_arch(self, arch):
self.apply_patches(arch)
def get_recipe_env(self,arch):
env = super(OpenCVRecipe, self).get_recipe_env(arch)
env['PYTHON_ROOT'] = self.ctx.get_python_install_dir()
env['ANDROID_NDK'] = self.ctx.ndk_dir
env['ANDROID_SDK'] = self.ctx.sdk_dir
env['SITEPACKAGES_PATH'] = self.ctx.get_site_packages_dir()
return env
def build_arch(self, arch):
shprint(sh.mkdir, os.path.join(self.get_build_dir(arch.arch), "build"))
with current_directory(os.path.join(self.get_build_dir(arch.arch), "build")):
env = self.get_recipe_env(arch)
cvsrc = self.get_build_dir(arch.arch)
lib_dir = os.path.join(self.ctx.get_python_install_dir(), "lib")
shprint(sh.pwd)
shprint(sh.cmake,
'-DP4A=ON','-DANDROID_ABI={}'.format(arch.arch),
'-DCMAKE_TOOLCHAIN_FILE={}/platforms/android/android.toolchain.cmake'.format(cvsrc),
'-DPYTHON_INCLUDE_PATH={}/include/python2.7'.format(env['PYTHON_ROOT']),
'-DPYTHON_LIBRARY={}/lib/libpython2.7.so'.format(env['PYTHON_ROOT']),
'-DPYTHON_NUMPY_INCLUDE_DIR={}/numpy/core/include'.format(env['SITEPACKAGES_PATH']),
'-DANDROID_EXECUTABLE={}/tools/android'.format(os.environ['ANDROIDSDK']),
'-DBUILD_TESTS=OFF', '-DBUILD_PERF_TESTS=OFF', '-DBUILD_EXAMPLES=OFF', '-DBUILD_ANDROID_EXAMPLES=OFF',
'-DPYTHON_PACKAGES_PATH={}'.format(env['SITEPACKAGES_PATH']),
'-DANDROID_NATIVE_API_LEVEL=%s' % os.environ['ANDROIDAPI'],
'-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9',
'-DANDROID_NDK=%s' % os.environ['ANDROIDNDK'],
cvsrc,
_env=env)
shprint(sh.make,'-j',str(cpu_count()))
shprint(sh.cmake,'-DCOMPONENT=python','-P','./cmake_install.cmake')
sh.cp('-a',sh.glob('./lib/{}/lib*.a'.format(arch.arch)),lib_dir)
recipe = OpenCVRecipe()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment