PyBox / Blog / Machine learning

TensorFlow, Keras and MediaPipe on Android

Most mobile coding apps stop at print statements. PyBox installs the real packages, so TensorFlow, Keras, PyTorch and MediaPipe run on the phone, offline once downloaded. Here is what I tested, with the results.

TensorFlow, Keras and MediaPipe on Android: the PyBox screen this guide uses
I build PyBox, so this is a list of what I ran myself on an Android build of it. Get it on Google Play.

The versions

PackageVersion testedNotes
Python3.14.8The CPython that PyBox ships
TensorFlow2.21Android arm64 wheel, installed with pip
Keras3.15TensorFlow backend
PyTorch2.14Trained a digit classifier to about 98 percent
MediaPipe1.1.1Tasks API for vision and text
scikit-learn, SciPy, pandascurrentInstalled on demand

Keras and TensorFlow results

These are PyBox project templates. Each one ran to the end on an Android emulator, using only the phone's CPU.

ProjectWhat it doesResult
Keras MNIST CNNTrains a small CNN on 20,000 digits, saves and reloads it96.8 percent test accuracy
MobileNetV2 classifierPretrained ImageNet model on your photosA cheeseburger photo scores 75 percent cheeseburger
Movie review sentimentEmbedding model on the IMDB reviewsAbout 86 percent test accuracy
TensorFlow Lite converterConverts a Keras model and runs it with the Lite interpreter53.9 kB float, 17.5 kB quantized, same 93.3 percent accuracy
Time series forecastGRU network against a naive baselineRMSE 1.53 against 1.94
Denoising autoencoderCleans noisy Fashion-MNIST imagesError from 0.067 down to 0.020
Custom training looptf.GradientTape by handRecovers y = 3x + 2 to three decimals

MediaPipe results

ProjectResult on the test image
Hand landmarksTwo hands found at 94 and 96 percent, 21 points each, raised fingers counted
Gesture recognizerThumb up, victory and pointing up all named correctly
Face landmarks478 points and expression scores, such as a 95 percent smile
Face blurFace found at 92 percent and blurred
Pose landmarker33 points, with elbow and knee angles in degrees
Object detectionEfficientDet-Lite0 found the person at 93 percent
Image classifierEfficientNet-Lite0 names the burger at 89 percent
Text similarityMatches "I can't log in" to the password and login lines

Two things I had to work around

  • Import order. On Android, loading matplotlib's Agg renderer after PyTorch or TensorFlow can crash Python. The templates import matplotlib first and leave a comment saying why.
  • Models download once. Weights and MediaPipe model files come from Google's servers the first time, then stay in the project folder.

Try it

Open PyBox, choose New Project, and search the templates for Keras, TensorFlow or MediaPipe. If you want to learn first, see Python projects you can build on Android. A live hand tracking project using the camera is in the templates too. I only ran it as far as opening the camera on an emulator without one, so treat that one as untested on real hardware.

Questions

Does TensorFlow run on Android in PyBox?

Yes. PyBox installs TensorFlow 2.21 from an Android arm64 wheel, and Keras 3.15 runs on the TensorFlow backend. Training, saving and TensorFlow Lite conversion all ran on the phone in my tests.

Does MediaPipe work in Python on Android?

Yes, through the Tasks API (mediapipe 1.1.1): hand, face, pose, gesture, object detection, image classification and text embedding all ran. The old mp.solutions API is not in that version.

Can I train models on a phone?

Small ones, yes. A CNN on 20,000 MNIST images reaches about 97 percent in three epochs. Big models are better trained on a computer and run on the phone.

Anshuman MishraBuilds PyBox at Vytros, a Python IDE for Android. About the developer.

Code Python on your phone.