Skip to content

Commit 5896993

Browse files
authored
Merge pull request kivy#765 from frmdstryr/master
Add opencv recipe
2 parents 0d9e613 + 9f1e6a1 commit 5896993

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
import sh
3+
from pythonforandroid.toolchain import (
4+
NDKRecipe,
5+
Recipe,
6+
current_directory,
7+
info,
8+
shprint,
9+
)
10+
from multiprocessing import cpu_count
11+
12+
13+
class OpenCVRecipe(NDKRecipe):
14+
version = '2.4.10.1'
15+
url = 'https://github.com/Itseez/opencv/archive/{version}.zip'
16+
#md5sum = '2ddfa98e867e6611254040df841186dc'
17+
depends = ['numpy']
18+
patches = ['patches/p4a_build-2.4.10.1.patch']
19+
generated_libraries = ['cv2.so']
20+
21+
def prebuild_arch(self, arch):
22+
self.apply_patches(arch)
23+
24+
def get_recipe_env(self,arch):
25+
env = super(OpenCVRecipe, self).get_recipe_env(arch)
26+
env['PYTHON_ROOT'] = self.ctx.get_python_install_dir()
27+
env['ANDROID_NDK'] = self.ctx.ndk_dir
28+
env['ANDROID_SDK'] = self.ctx.sdk_dir
29+
env['SITEPACKAGES_PATH'] = self.ctx.get_site_packages_dir()
30+
return env
31+
32+
def build_arch(self, arch):
33+
with current_directory(self.get_build_dir(arch.arch)):
34+
env = self.get_recipe_env(arch)
35+
cvsrc = self.get_build_dir(arch.arch)
36+
lib_dir = os.path.join(self.ctx.get_python_install_dir(), "lib")
37+
38+
shprint(sh.cmake,
39+
'-DP4A=ON','-DANDROID_ABI={}'.format(arch.arch),
40+
'-DCMAKE_TOOLCHAIN_FILE={}/platforms/android/android.toolchain.cmake'.format(cvsrc),
41+
'-DPYTHON_INCLUDE_PATH={}/include/python2.7'.format(env['PYTHON_ROOT']),
42+
'-DPYTHON_LIBRARY={}/lib/libpython2.7.so'.format(env['PYTHON_ROOT']),
43+
'-DPYTHON_NUMPY_INCLUDE_DIR={}/numpy/core/include'.format(env['SITEPACKAGES_PATH']),
44+
'-DANDROID_EXECUTABLE={}/tools/android'.format(env['ANDROID_SDK']),
45+
'-DBUILD_TESTS=OFF', '-DBUILD_PERF_TESTS=OFF', '-DBUILD_EXAMPLES=OFF', '-DBUILD_ANDROID_EXAMPLES=OFF',
46+
'-DPYTHON_PACKAGES_PATH={}'.format(env['SITEPACKAGES_PATH']),
47+
cvsrc,
48+
_env=env)
49+
shprint(sh.make,'-j',str(cpu_count()),'opencv_python')
50+
shprint(sh.cmake,'-DCOMPONENT=python','-P','./cmake_install.cmake')
51+
sh.cp('-a',sh.glob('./lib/{}/lib*.so'.format(arch.arch)),lib_dir)
52+
53+
recipe = OpenCVRecipe()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
diff --git a/cmake/OpenCVDetectPython.cmake b/cmake/OpenCVDetectPython.cmake
2+
index 31c2c1e..c890917 100644
3+
--- a/cmake/OpenCVDetectPython.cmake
4+
+++ b/cmake/OpenCVDetectPython.cmake
5+
@@ -36,7 +36,7 @@ if(PYTHON_EXECUTABLE)
6+
unset(PYTHON_VERSION_FULL)
7+
endif()
8+
9+
- if(NOT ANDROID AND NOT IOS)
10+
+ if(P4A OR NOT ANDROID AND NOT IOS)
11+
ocv_check_environment_variables(PYTHON_LIBRARY PYTHON_INCLUDE_DIR)
12+
if(CMAKE_CROSSCOMPILING)
13+
find_host_package(PythonLibs ${PYTHON_VERSION_MAJOR_MINOR})
14+
@@ -51,7 +51,7 @@ if(PYTHON_EXECUTABLE)
15+
endif()
16+
endif()
17+
18+
- if(NOT ANDROID AND NOT IOS)
19+
+ if(P4A OR NOT ANDROID AND NOT IOS)
20+
if(CMAKE_HOST_UNIX)
21+
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import *; print get_python_lib()"
22+
RESULT_VARIABLE PYTHON_CVPY_PROCESS
23+
@@ -117,7 +117,7 @@ if(PYTHON_EXECUTABLE)
24+
OUTPUT_STRIP_TRAILING_WHITESPACE)
25+
endif()
26+
endif()
27+
- endif(NOT ANDROID AND NOT IOS)
28+
+ endif(P4A OR NOT ANDROID AND NOT IOS)
29+
30+
if(BUILD_DOCS)
31+
find_host_program(SPHINX_BUILD sphinx-build)
32+
diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt
33+
index 3c0f2fd..7ba234a 100644
34+
--- a/modules/python/CMakeLists.txt
35+
+++ b/modules/python/CMakeLists.txt
36+
@@ -5,7 +5,7 @@
37+
if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
38+
ocv_module_disable(python)
39+
endif()
40+
-if(ANDROID OR IOS OR NOT PYTHONLIBS_FOUND OR NOT PYTHON_USE_NUMPY)
41+
+if(ANDROID AND NOT P4A OR IOS OR NOT PYTHONLIBS_FOUND OR NOT PYTHON_USE_NUMPY)
42+
ocv_module_disable(python)
43+
endif()
44+
45+
diff --git a/modules/androidcamera/src/camera_activity.cpp b/modules/androidcamera/src/camera_activity.cpp
46+
index 84db3e1..4222526 100644
47+
--- a/modules/androidcamera/src/camera_activity.cpp
48+
+++ b/modules/androidcamera/src/camera_activity.cpp
49+
@@ -7,6 +7,7 @@
50+
#include <string>
51+
#include <vector>
52+
#include <algorithm>
53+
+#include <stdlib.h>
54+
#include <opencv2/core/version.hpp>
55+
#include "camera_activity.hpp"
56+
#include "camera_wrapper.h"
57+
@@ -342,6 +343,8 @@ std::string CameraWrapperConnector::getPathLibFolder()
58+
59+
char* pathEnd = strrchr(pathBegin, '/');
60+
pathEnd[1] = 0;
61+
+ pathBegin = realpath((std::string(pathBegin)+"../../../../lib").c_str(), lineBuf);
62+
+ pathBegin = strcat(pathBegin, "/");
63+
64+
LOGD("Libraries folder found: %s", pathBegin);
65+
66+

0 commit comments

Comments
 (0)