You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

android_opencv_python.sh 2.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/bash -e
  2. # This script is a workaround of installing opencv-python in termux.
  3. SRC_DIR=$(readlink -f "`dirname $0`/../../../")
  4. cd ${SRC_DIR}
  5. source scripts/whl/android/utils.sh
  6. function install_apt_package() {
  7. APT_PACKAGE="build-essential cmake libjpeg-turbo libpng python clang"
  8. echo "try to install: ${APT_PACKAGE}"
  9. apt install ${APT_PACKAGE}
  10. }
  11. function build_opencv_python() {
  12. python3 -m pip install numpy
  13. mge_python_env_root="${HOME}/mge_python_env"
  14. mkdir -p ${mge_python_env_root}
  15. cd ${mge_python_env_root}
  16. opencv_repo_dir=${mge_python_env_root}/opencv
  17. if [ -d ${opencv_repo_dir}/.git ];then
  18. echo "already find opencv repo"
  19. cd ${opencv_repo_dir}
  20. git reset --hard
  21. git clean -xdf
  22. git fetch
  23. else
  24. cd ${mge_python_env_root}
  25. rm -rf ${opencv_repo_dir}
  26. git clone https://github.com/opencv/opencv.git
  27. fi
  28. # Build and test latest version by default. You can modify OPENCV_VER to build and test another version!!
  29. python3_site=`python3 -c 'import site; print(site.getsitepackages()[0])'`
  30. OPENCV_VER="3.4.15"
  31. git checkout ${OPENCV_VER}
  32. if [ -e ${python3_site}/cv2/__init__.py ];then
  33. echo "python3 already build cv2, skip build it, if you want to rebuild, you can do: rm -rf ${python3_site}/cv2"
  34. else
  35. cd ${opencv_repo_dir}
  36. git checkout ${OPENCV_VE}
  37. git apply ${SRC_DIR}/scripts/whl/android/cv_patch/*.patch
  38. mkdir -p build
  39. cd build
  40. echo "will install to ${python3_site}"
  41. PYTHON3_EXECUTABLE=`command -v python3`
  42. LDFLAGS=" -llog -lpython3" cmake -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_opencv_python3=on \
  43. -DBUILD_opencv_python2=off -DWITH_QT=OFF -DWITH_GTK=OFF -DBUILD_ANDROID_PROJECTS=OFF \
  44. -DBUILD_ANDROID_EXAMPLES=OFF -DBUILD_FAT_JAVA_LIB=OFF -DBUILD_ANDROID_SERVICE=OFF \
  45. -DHAVE_opencv_python3=ON -D__INSTALL_PATH_PYTHON3=${python3_site} \
  46. -DPYTHON3_EXECUTABLE=${PYTHON3_EXECUTABLE} \
  47. -DOPENCV_PYTHON_INSTALL_PATH=${python3_site} -DCMAKE_INSTALL_PREFIX=${python3_site} .. \
  48. && make -j$(nproc) && make install
  49. # check if build successfully
  50. cd ~
  51. python3 -c 'import cv2;print(cv2.__version__)'
  52. fi
  53. }
  54. ############install env now###########
  55. echo "run at root dir: ${SRC_DIR}"
  56. check_termux_env
  57. install_apt_package
  58. build_opencv_python