diff --git a/pythonforandroid/recipes/opencv/__init__.py b/pythonforandroid/recipes/opencv/__init__.py new file mode 100644 index 0000000000..7e70162ea3 --- /dev/null +++ b/pythonforandroid/recipes/opencv/__init__.py @@ -0,0 +1,53 @@ +import os +import sh +from pythonforandroid.toolchain import ( + NDKRecipe, + Recipe, + current_directory, + info, + shprint, +) +from multiprocessing import cpu_count + + +class OpenCVRecipe(NDKRecipe): + version = '2.4.10.1' + url = 'https://github.com/Itseez/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): + with current_directory(self.get_build_dir(arch.arch)): + 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.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(env['ANDROID_SDK']), + '-DBUILD_TESTS=OFF', '-DBUILD_PERF_TESTS=OFF', '-DBUILD_EXAMPLES=OFF', '-DBUILD_ANDROID_EXAMPLES=OFF', + '-DPYTHON_PACKAGES_PATH={}'.format(env['SITEPACKAGES_PATH']), + cvsrc, + _env=env) + shprint(sh.make,'-j',str(cpu_count()),'opencv_python') + shprint(sh.cmake,'-DCOMPONENT=python','-P','./cmake_install.cmake') + sh.cp('-a',sh.glob('./lib/{}/lib*.so'.format(arch.arch)),lib_dir) + +recipe = OpenCVRecipe() diff --git a/pythonforandroid/recipes/opencv/patches/p4a_build-2.4.10.1.patch b/pythonforandroid/recipes/opencv/patches/p4a_build-2.4.10.1.patch new file mode 100644 index 0000000000..a7a60aa3b3 --- /dev/null +++ b/pythonforandroid/recipes/opencv/patches/p4a_build-2.4.10.1.patch @@ -0,0 +1,66 @@ +diff --git a/cmake/OpenCVDetectPython.cmake b/cmake/OpenCVDetectPython.cmake +index 31c2c1e..c890917 100644 +--- a/cmake/OpenCVDetectPython.cmake ++++ b/cmake/OpenCVDetectPython.cmake +@@ -36,7 +36,7 @@ if(PYTHON_EXECUTABLE) + unset(PYTHON_VERSION_FULL) + endif() + +- if(NOT ANDROID AND NOT IOS) ++ if(P4A OR NOT ANDROID AND NOT IOS) + ocv_check_environment_variables(PYTHON_LIBRARY PYTHON_INCLUDE_DIR) + if(CMAKE_CROSSCOMPILING) + find_host_package(PythonLibs ${PYTHON_VERSION_MAJOR_MINOR}) +@@ -51,7 +51,7 @@ if(PYTHON_EXECUTABLE) + endif() + endif() + +- if(NOT ANDROID AND NOT IOS) ++ if(P4A OR NOT ANDROID AND NOT IOS) + if(CMAKE_HOST_UNIX) + execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import *; print get_python_lib()" + RESULT_VARIABLE PYTHON_CVPY_PROCESS +@@ -117,7 +117,7 @@ if(PYTHON_EXECUTABLE) + OUTPUT_STRIP_TRAILING_WHITESPACE) + endif() + endif() +- endif(NOT ANDROID AND NOT IOS) ++ endif(P4A OR NOT ANDROID AND NOT IOS) + + if(BUILD_DOCS) + find_host_program(SPHINX_BUILD sphinx-build) +diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt +index 3c0f2fd..7ba234a 100644 +--- a/modules/python/CMakeLists.txt ++++ b/modules/python/CMakeLists.txt +@@ -5,7 +5,7 @@ + if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug") + ocv_module_disable(python) + endif() +-if(ANDROID OR IOS OR NOT PYTHONLIBS_FOUND OR NOT PYTHON_USE_NUMPY) ++if(ANDROID AND NOT P4A OR IOS OR NOT PYTHONLIBS_FOUND OR NOT PYTHON_USE_NUMPY) + ocv_module_disable(python) + endif() + +diff --git a/modules/androidcamera/src/camera_activity.cpp b/modules/androidcamera/src/camera_activity.cpp +index 84db3e1..4222526 100644 +--- a/modules/androidcamera/src/camera_activity.cpp ++++ b/modules/androidcamera/src/camera_activity.cpp +@@ -7,6 +7,7 @@ + #include + #include + #include ++#include + #include + #include "camera_activity.hpp" + #include "camera_wrapper.h" +@@ -342,6 +343,8 @@ std::string CameraWrapperConnector::getPathLibFolder() + + char* pathEnd = strrchr(pathBegin, '/'); + pathEnd[1] = 0; ++ pathBegin = realpath((std::string(pathBegin)+"../../../../lib").c_str(), lineBuf); ++ pathBegin = strcat(pathBegin, "/"); + + LOGD("Libraries folder found: %s", pathBegin); + +