A simple Android app that uses TensorFlow Lite to classify images using the MobileNet v1 model. Built with Kotlin, MVVM architecture, Jetpack Compose, and Material 3.
- Image Selection: Choose images from the device gallery
- Real-time Classification: Uses TensorFlow Lite MobileNet v1 model for image classification
- Modern UI: Built with Jetpack Compose and Material 3 design system
- MVVM Architecture: Clean separation of concerns with ViewModel and Repository pattern
- EXIF Support: Handles image rotation correctly
- Error Handling: Comprehensive error handling and retry functionality
- Android Studio Arctic Fox or later
- Android SDK 23 (Android 6.0) or higher
- Kotlin 1.8+
-
Download the Model:
- The current model file is a placeholder. To use the app, you need to download the actual MobileNet v1 model:
- Visit TensorFlow Hub
- Download the
.tflitefile - Replace the placeholder file at
app/src/main/assets/models/mobilenet_v1_1.0_224.tflite
-
Build and Run:
./gradlew assembleDebug
-
Install on Device:
adb install app/build/outputs/apk/debug/app-debug.apk
app/src/main/java/com/example/image/classification/tensorflowlite/
├── data/
│ ├── ClassificationResult.kt # Data class for classification results
│ └── UiState.kt # UI state management
├── ml/
│ └── ImageClassifier.kt # TensorFlow Lite model wrapper
├── repository/
│ └── ClassifierRepository.kt # Data layer for image classification
├── ui/
│ └── MainScreen.kt # Jetpack Compose UI
├── viewmodel/
│ └── MainViewModel.kt # ViewModel for UI state management
└── MainActivity.kt # Main activity with Compose setup
The app follows the MVVM (Model-View-ViewModel) architecture pattern:
- Model: TensorFlow Lite model and data classes
- View: Jetpack Compose UI components
- ViewModel: Manages UI state and business logic
- Repository: Handles data operations and ML model interactions
- Loads the TensorFlow Lite model from assets
- Handles image preprocessing (resize to 224x224, normalization)
- Manages EXIF rotation data
- Performs inference and returns classification results
- Manages UI state using StateFlow
- Handles image selection and classification workflow
- Provides error handling and retry functionality
- Material 3 design system
- Image picker integration
- Real-time classification results display
- Loading states and error handling
To use a different TensorFlow Lite model:
-
Replace Model File:
- Place your
.tflitemodel file inapp/src/main/assets/models/ - Update the filename in
ImageClassifier.ktif different frommobilenet_v1_1.0_224.tflite
- Place your
-
Update Labels (if needed):
- Replace
labels_mobilenet.txtwith your model's labels - Ensure the number of labels matches your model's output classes
- Replace
-
Adjust Input Size (if needed):
- Modify the input size in
ImageClassifier.preprocessImage()if your model expects different dimensions - Common sizes: 224x224, 299x299, 512x512
- Modify the input size in
-
Update Classification Options:
- Modify
ImageClassifierOptionsinImageClassifier.initializeClassifier()if needed - Adjust
setMaxResults()based on your requirements
- Modify
- TensorFlow Lite:
org.tensorflow:tensorflow-lite-task-vision:0.4.4 - Jetpack Compose: UI toolkit for modern Android development
- Material 3: Latest Material Design components
- Coil: Image loading library for Compose
- Lifecycle: ViewModel and LiveData/StateFlow support
The app requires the following permissions:
READ_EXTERNAL_STORAGE: For accessing images on Android 12 and belowREAD_MEDIA_IMAGES: For accessing images on Android 13 and above
The app has been designed to work on Android 6.0 (API 23) and above. Test on various devices to ensure compatibility.
- Model Loading Issues: Ensure the
.tflitefile is not compressed (handled byaaptOptions.noCompress("tflite")) - Image Loading Issues: Check that the app has proper permissions for image access
- Classification Errors: Verify that the model file is compatible with TensorFlow Lite Task Vision library
- Add support for camera capture
- Implement model quantization for smaller app size
- Add batch processing capabilities
- Implement custom model training integration
- Add confidence threshold filtering