How to correctly compile OpenCV into my kivy app

562 views
Skip to first unread message

Edmond Yip

unread,
Jul 29, 2020, 10:00:43 PM7/29/20
to Kivy users support
I tweaked the requirements in buildozer.spec file many times to compile OpenCV into my app, yet it does not compile into the app. Whenever I launches the app on android it just crashes. I wonder if anyone here has experience in using OpenCV in a kivy android app.

I tried to use opencv and opencv-contrib-python, with armeabi-v7a (32bit), which does not work because opencv can only be compiled as 64-bit binaries (it is a guess)

07-29 13:55:00.222  6562  6639 I python  :  Traceback (most recent call last):
07-29 13:55:00.222  6562  6639 I python  :    File "/home/cat/Desktop/kivy_glucose/.buildozer/android/app/main.py", line 8, in <module>
07-29 13:55:00.222  6562  6639 I python  :    File "/home/cat/Desktop/kivy_glucose/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/kivyglucose/cv2/__init__.py", line 5, in <module>
07-29 13:55:00.222  6562  6639 I python  :  ImportError: dlopen failed: "/data/data/org.test.kivyglucose/files/app/_python_bundle/site-packages/cv2/cv2.so" is 64-bit instead of 32-bit
07-29 13:55:00.222  6562  6639 I python  : Python for android ended.

Then I tried to use opencv and opencv-contrib-python, with arm64-v8a (64bit), which also does not work. It says in the error that it does not expect running on 64-bit. (again, my guess). I am kind of stuck in the middle now.

07-30 09:42:11.991  4748  4820 I python  :  Traceback (most recent call last):
07-30 09:42:11.991  4748  4820 I python  :    File "/home/cat/Desktop/kivy_glucose/.buildozer/android/app/main.py", line 8, in <module>
07-30 09:42:11.991  4748  4820 I python  :    File "/home/cat/Desktop/kivy_glucose/.buildozer/android/platform/build-arm64-v8a/build/python-installs/kivyglucose/cv2/__init__.py", line 5, in <module>
07-30 09:42:11.992  4748  4820 I python  :  ImportError: dlopen failed: "/data/data/org.test.kivyglucose/files/app/_python_bundle/site-packages/cv2/cv2.so" has unexpected e_machine: 62 (EM_X86_64)
07-30 09:42:11.992  4748  4820 I python  : Python for android ended.

Thanks!

planckp...@gmail.com

unread,
Jul 29, 2020, 10:58:30 PM7/29/20
to Kivy users support
Well it looks like it is trying to import an x86_64 wheel, which would explain both messages. This would happen if opencv is not in the requirement list.

Is opencv in the buildozer.spec requirements list ?
Since OCV is not pure Python, it needs use use the recipe to build an ARM binary, 'requirements' triggers this if the package is in this list:

Don't forget  to      buildozer appclean

And looking forward, you have read in these pages that the OCV camera doesn't work on Android so you are not planning on using it, right?

Edmond Yip

unread,
Jul 30, 2020, 12:54:39 AM7/30/20
to Kivy users support
Thanks for the prompt reply!
I use kivy camera to take photo and convert the texture to numpy array, then back to texture again for display. The app ran as expected on PC but not on my android phone. 
I will clean up the .buildozer and relaunch.

Edmond Yip

unread,
Jul 30, 2020, 1:54:06 AM7/30/20
to Kivy users support
Now when I launch the app on android, the window just collapses. The log says i don't have a module named 'cv2'. Is it because compiled opencv is not called cv2?

07-30 13:46:43.304 29607 29629 I python  :  Traceback (most recent call last):
07-30 13:46:43.304 29607 29629 I python  :    File "/home/cat/Desktop/kivy_glucose/.buildozer/android/app/main.py", line 8, in <module>
07-30 13:46:43.304 29607 29629 I python  :  ModuleNotFoundError: No module named 'cv2'
07-30 13:46:43.304 29607 29629 I python  : Python for android ended.

planckp...@gmail.com

unread,
Jul 30, 2020, 3:05:45 AM7/30/20
to Kivy users support
You can see  here that it should generate cv2.so site packages in the apk:

I don't know why the module is not found,  I'd do an appclean, rebuild and eyeball the build logfile very carefully, just because it ran doesn't mean it built :(  You could also look inside the apk for cv2.so but all that will tell you is which direction to look for the cause of the issue.

Edmond Yip

unread,
Jul 30, 2020, 3:09:17 AM7/30/20
to Kivy users support
So with a big tweaking, my app can launch on android, but only returns a black screen and no notable error from logcat
log.txt

planckp...@gmail.com

unread,
Jul 30, 2020, 12:39:02 PM7/30/20
to Kivy users support
There appears to be some magic to get kivy.uix.camera to work on Android, I don't know the magic words.

However kivy_garden.xcamera (which is a wrapper around kivy.uix.camera, and adds better jpg support) seem to mostly work out the box depening what you want to do.  There are still some issues, see the Github page.
Reply all
Reply to author
Forward
0 new messages