Skip to content

Commit 2fd6f02

Browse files
committed
opencv: fix build
1 parent d129d08 commit 2fd6f02

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

pythonforandroid/recipes/opencv/__init__.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class OpenCVRecipe(NDKRecipe):
1515
build of most of the libraries of the opencv's package, so we can
1616
process images, videos, objects, photos...
1717
'''
18-
version = '4.5.1'
18+
version = '4.12.0'
1919
url = 'https://github.com/opencv/opencv/archive/{version}.zip'
2020
depends = ['numpy']
2121
patches = ['patches/p4a_build.patch']
@@ -68,8 +68,9 @@ def build_arch(self, arch):
6868
python_link_version = self.ctx.python_recipe.link_version
6969
python_library = join(python_link_root,
7070
'libpython{}.so'.format(python_link_version))
71-
python_include_numpy = join(python_site_packages,
72-
'numpy', 'core', 'include')
71+
python_include_numpy = join(
72+
self.ctx.get_python_install_dir(arch.arch), "numpy/_core/include",
73+
)
7374

7475
shprint(sh.cmake,
7576
'-DP4A=ON',
@@ -136,7 +137,19 @@ def build_arch(self, arch):
136137

137138
self.get_build_dir(arch.arch),
138139
_env=env)
139-
shprint(sh.make, '-j' + str(cpu_count()), 'opencv_python' + python_major)
140+
141+
try:
142+
shprint(sh.make, '-j' + str(cpu_count()), 'VERBOSE=1', 'opencv_python' + python_major)
143+
except Exception:
144+
# Patch link.txt and retry
145+
link_txt = 'modules/python3/CMakeFiles/opencv_python3.dir/link.txt'
146+
with open(link_txt, 'r+') as f:
147+
content = f.read().replace('-version', '--version')
148+
f.seek(0)
149+
f.write(content)
150+
f.truncate()
151+
shprint(sh.make, '-j' + str(cpu_count()), 'opencv_python' + python_major)
152+
140153
# Install python bindings (cv2.so)
141154
shprint(sh.cmake, '-DCOMPONENT=python', '-P', './cmake_install.cmake')
142155
# Copy third party shared libs that we need in our final apk

pythonforandroid/recipes/opencv/patches/p4a_build.patch

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
This patch allow that the opencv's build command correctly detects our version
2-
of python, so we can successfully build the python bindings (cv2.so)
3-
--- opencv-4.0.1/cmake/OpenCVDetectPython.cmake.orig 2018-12-22 08:03:30.000000000 +0100
4-
+++ opencv-4.0.1/cmake/OpenCVDetectPython.cmake 2019-01-31 11:33:10.896502978 +0100
5-
@@ -175,7 +175,7 @@ if(NOT ${found})
1+
diff '--color=auto' -uNr opencv-4.12.0/cmake/OpenCVDetectPython.cmake opencv-4.12.0.mod/cmake/OpenCVDetectPython.cmake
2+
--- opencv-4.12.0/cmake/OpenCVDetectPython.cmake 2025-07-02 13:24:13.000000000 +0530
3+
+++ opencv-4.12.0.mod/cmake/OpenCVDetectPython.cmake 2025-09-20 22:22:14.961944470 +0530
4+
@@ -175,7 +175,7 @@
65
endif()
76
endif()
8-
9-
- if(NOT ANDROID AND NOT IOS)
10-
+ if(P4A OR NOT ANDROID AND NOT IOS)
7+
8+
- if(NOT ANDROID AND NOT IOS AND NOT XROS)
9+
+ if(P4A OR NOT ANDROID AND NOT IOS AND NOT XROS)
1110
if(CMAKE_HOST_UNIX)
12-
execute_process(COMMAND ${_executable} -c "from distutils.sysconfig import *; print(get_python_lib())"
11+
execute_process(COMMAND ${_executable} -c "from sysconfig import *; print(get_path('purelib'))"
1312
RESULT_VARIABLE _cvpy_process
14-
@@ -244,7 +244,7 @@ if(NOT ${found})
15-
OUTPUT_STRIP_TRAILING_WHITESPACE)
16-
endif()
17-
endif()
18-
- endif(NOT ANDROID AND NOT IOS)
19-
+ endif(P4A OR NOT ANDROID AND NOT IOS)
20-
endif()
21-
22-
# Export return values
23-
--- opencv-4.0.1/modules/python/CMakeLists.txt.orig 2018-12-22 08:03:30.000000000 +0100
24-
+++ opencv-4.0.1/modules/python/CMakeLists.txt 2019-01-31 11:47:17.100494908 +0100
13+
diff '--color=auto' -uNr opencv-4.12.0/modules/python/CMakeLists.txt opencv-4.12.0.mod/modules/python/CMakeLists.txt
14+
--- opencv-4.12.0/modules/python/CMakeLists.txt 2025-07-02 13:24:13.000000000 +0530
15+
+++ opencv-4.12.0.mod/modules/python/CMakeLists.txt 2025-09-20 22:23:15.124356524 +0530
2516
@@ -3,7 +3,7 @@
2617
# ----------------------------------------------------------------------------
2718
if(DEFINED OPENCV_INITIAL_PASS) # OpenCV build
28-
19+
2920
-if(ANDROID OR APPLE_FRAMEWORK OR WINRT)
3021
+if(ANDROID AND NOT P4A OR APPLE_FRAMEWORK OR WINRT)
3122
ocv_module_disable_(python2)

0 commit comments

Comments
 (0)