Skip to main content

Installing Anaconda on Android (aarch64) devices

Categories   Smartphone Termux Tutorial
Tags   Termux
Table of Contents

🇹🇼 中文版

This tutorial is for those who want to install Anaconda on Android phone.

The benefit of Anaconda is that you could have multiple Python versions installed on the same device. And you can switch to different environments quickly.

An example of OpenCV program running on Termux on Android phone.

OpenCV on Termux on Android

Because most Android devices are 64bit (aarch64), the Anaconda and Miniconda installers from official website will not work on Termux, therefore we have to use miniforge. The commands are almost as same as original Anaconda.

1. Setup Termux and Proot Linux
#

  1. Install Termux

  2. Though Termux does provide python packages, however the installer only works in Linux environment. Thus, we must setup a Proot Debian. You can skip installing desktop environment parts if you just want to run Python scripts.

2. Install miniforge3
#

  1. Log into Proot Debian as root.
proot-distro login debian
  1. Install dependencies of miniforge3
apt update
apt install wget python3 python3-pip vim
  1. When executing python commands you would notice that it would use Python from Termux instead of Proot, so you have to execute follwoing commands to specify the path of Python installation
alias python=/usr/bin/python3
alias python3=/usr/bin/python3
alias pip=/usr/bin/pip
alias pip3=/usr/bin/pip
  1. Download installer from miniforge repository
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-aarch64.sh
bash Miniforge3-Linux-aarch64.sh
  1. Press Enter. Press Q. Then type yes to confirm the license.

  2. By default miniforge will be installed to /root/miniforge3

  3. Type yes to initialize conda environment.

  4. Relogin to Proot Debian.

exit
proot-distro login debian
  1. You will see the prompt become (base),that means you are in conda environments. Type conda deactivate to exit.

  2. If you want to disable auto activatation of conda environment, type conda config --set auto_activate_base false to disable it.

3. Usage of conda commands
#

When you are in conda environment, you can install Pyhton packages with conda install or pip install.

You can also create multiple virtual enviornments with different Python installed.

For example, you can create a environment with old Pyhton 3.6:

conda create --name test1 python=3.6

Then activate the enviornment.

conda activate test1

Now you can install Python packages which only work on old Pyhton versions.

4. An example of OpenCV program
#

Let’s detect the faces of two girls from Girls’ Last Tour from the image below. We will use codes from lbpcascade_animeface

  1. Create conda environment
conda create --name facedetection python=3.10.9
conda activate facedetection
  1. Install required packages
conda install -c conda-forge opencv
wget https://raw.githubusercontent.com/nagadomi/lbpcascade_animeface/master/lbpcascade_animeface.xml
  1. Create a python script
vim detectface.py
  1. The script will detect faces from input.jpg and create a output.jpg
import cv2
def detectFace(filename):
    cascade = cv2.CascadeClassifier("./lbpcascade_animeface.xml")
    image = cv2.imread(filename, cv2.IMREAD_COLOR)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.equalizeHist(gray)
    color = (0, 0, 255)
    faces = cascade.detectMultiScale(gray,
                                     scaleFactor = 1.1,
                                     minNeighbors = 5,
                                     minSize = (24, 24))
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 20)
    cv2.waitKey(0)
    cv2.imwrite("output.jpg", image)

detectFace('input.jpg')
  1. You may wonder how to move pictures into Proot Debian? Don’t forget that internal storage is mounted at /sdcard , so just type:
mv /sdcard/Downloads/input.jpg ~/
  1. Run the script
python detectface.py
  1. Move the output.jpg back to sdcard
mv output.jpg /sdcard/Downloads/
  1. The result.

References
#

Related

How to run Stable Diffusion on Termux on Android phone
Categories   Smartphone Termux Tutorial
Tags   Stable Diffusion AI Android Termux
How to play Minecraft Java Edition on Android using Termux proot
Categories   Smartphone Termux Tutorial
Tags   Raspberry Pi Minecraft Termux
Run Windows exe on Android through Proot Exagear on Termux
Categories   Smartphone Termux Tutorial
Tags   Termux Windows Android

Leave a comment

Choose the comment system you like. Giscus for codes, Disqus for anonymous comments.

Please login to your Github account to leave a comment. You can post codes here for discussion. For images, please upload to Imgur and copy links. Your comments would be visible to everyone on Github Discussions.

This comment system is provided by Disqus, you may see forcing Disqus ADs on this page. Sometimes your comment may need to be reviewed, resulting in delayed display. Thank you for your understanding.