This is an example application for ONNX Runtime on Android. The demo app uses image classification which is able to continuously classify the objects it sees from the device's camera in real-time and displays the most probable inference results on the screen.
This example is loosely based on Google CodeLabs - Getting Started with CameraX
We use pre-trained TorchVision MOBILENET V2 in this sample app.
- Android Studio 4.1+ (installed on Mac/Windows/Linux)
- Android SDK 29+
- Android NDK r21+
- Android device with a camera in developer mode with USB debugging enabled
Open Mobilenet v2 Quantization with ONNX Runtime Notebook, this notebook will demonstrate how to,
- Export the pre-trained MobileNet V2 FP32 model from PyTorch to a FP32 ONNX model
- Quantize the FP32 ONNX model to an uint8 ONNX model
- Convert both FP32 and uint8 ONNX models to ORT models
Note: this step is optional, you can download the FP32 and uint8 ORT models here.
Clone this GitHub repository to your computer to get the sample application.
Put the labels file and models into the sample application resource directory:
- Download the labels file here
- Copy MobileNetV2 ORT models and the labels file to
mobile/examples/image_classification/android/app/src/main/res/raw/
- Alternatively, you can run
mobile/examples/image_classification/android/download_model_files.sh
to download the labels file and models to the sample application resource directory.
Then open the sample application in Android Studio. To do this, open Android Studio and select Open an existing project
, browse folders and open the folder mobile/examples/image_classification/android/
.
Select Build -> Make Project
in the top toolbar in Android Studio and check the projects has built successfully.
Connect your Android Device to the computer and select your device in the top-down device bar.
Then Select Run -> Run app
and this will prompt the app to be installed on your device.
Now you can test and try by opening the app ort_image_classifier
on your device. The app may request your permission for using the camera.
Here's an example screenshot of the app.