Prerequisites
Before building FaceNet Android from source, ensure you have:Android Studio
Android Studio Hedgehog (2023.1.1) or newerDownload from developer.android.com
Android SDK
Android SDK with API level 26+ (Android 8.0)Minimum SDK: 26Target SDK: 34
JDK
Java Development Kit 8 or higherUsually bundled with Android Studio
Git
Git version controlFor cloning the repository
Download options
Option 1: Download pre-built APK
The simplest way to install FaceNet Android is to download the APK:- Visit the GitHub Releases page
- Download the latest
app-release.apkfile - Transfer it to your Android device
- Enable installation from unknown sources in your device settings
- Tap the APK file to install
Option 2: Build from source
For developers who want to modify the app or understand its internals:Open in Android Studio
- Launch Android Studio
- Select File > Open
- Navigate to the cloned directory and click OK
Wait for Gradle sync
Android Studio will automatically:
- Download Gradle dependencies
- Download TensorFlow Lite libraries
- Download ObjectBox plugins
- Configure the build system
Configuration
FaceNet Android offers several configuration options to optimize performance and accuracy.Choosing the FaceNet model
The app includes two FaceNet models with different embedding dimensions:- facenet.tflite: 128-dimensional embeddings (smaller, faster)
- facenet_512.tflite: 512-dimensional embeddings (more accurate, default)
Edit FaceNet.kt
Open
app/src/main/java/com/ml/shubham0204/facenet_android/domain/embeddings/FaceNet.ktUpdate the database schema
Open
app/src/main/java/com/ml/shubham0204/facenet_android/data/DataModels.kt and modify the @HnswIndex dimensions:Enable flat index search (precise NN search)
By default, ObjectBox uses HNSW (Hierarchical Navigable Small World) for approximate nearest-neighbor search. This is fast but may miss the true nearest neighbor, especially with larger datasets. To enable precise flat search:Edit FaceDetectionOverlay.kt
Open
app/src/main/java/com/ml/shubham0204/facenet_android/presentation/components/FaceDetectionOverlay.ktFlat search triggers a linear scan across all records, which is slower but guarantees finding the true nearest neighbor. The implementation uses 4 parallel coroutines to speed up the search.
| Search method | Speed | Accuracy | Best for |
|---|---|---|---|
| HNSW (default) | Fast | ~95% accurate | Large databases (>1000 faces) |
| Flat search | Slower | 100% accurate | Small databases (<500 faces) |
Choose face detection method
FaceNet Android supports two face detection backends:- MLKit (default): Google’s optimized face detection for Android
- Mediapipe: Cross-platform face detection using BlazeFace
Comparison:
| Detector | Performance | Accuracy | Model size |
|---|---|---|---|
| MLKit | Fast | High | Included in Google Play Services |
| Mediapipe | Moderate | High | BlazeFace short-range model (~1MB) |
Adjust recognition threshold
The cosine similarity threshold determines how strict face matching is. The default threshold is 0.3. To modify it:- Open
app/src/main/java/com/ml/shubham0204/facenet_android/domain/ImageVectorUseCase.kt - Find line 93 and change the threshold value:
- < 0.3: Very strict, may reject valid matches (low false positives)
- 0.3 - 0.4: Balanced (recommended)
- > 0.4: Lenient, may accept invalid matches (high false positives)
Build configuration
Key build settings fromapp/build.gradle.kts:
Key dependencies
The app uses these major libraries:GPU acceleration
FaceNet inference can be accelerated using GPU delegates: FromFaceNet.kt:48-59:
FaceNet.kt:27:
TFLite model sources
FaceNet models
Bothfacenet.tflite and facenet_512.tflite are converted from the deepface library using this Python script:
Anti-spoofing models
Thespoof_model_scale TFLite models are converted from PyTorch weights in Silent-Face-Anti-Spoofing via ONNX.
Conversion notebook: Liveness_PT_Model_to_TF.ipynb
BlazeFace model
Theblaze_face_short_range model is from Mediapipe’s Face Detector solution.
Troubleshooting
Gradle sync fails
Problem: “Could not resolve all dependencies” Solution: Ensure you have a stable internet connection and try:ObjectBox build errors
Problem: “ObjectBox annotation processor failed” Solution: Clean and rebuild the project:- Build > Clean Project
- Build > Rebuild Project
- If the error persists, delete the
app/buildfolder and rebuild
App crashes on startup
Problem: “Failed to load model” Solution: Verify that TFLite model files exist inapp/src/main/assets/:
facenet.tflitefacenet_512.tflitespoof_model_scale_1.tflitespoof_model_scale_2.tfliteblaze_face_short_range.tflite(if using Mediapipe)
Poor recognition accuracy
Problem: Faces not being recognized correctly Solutions:- Add more images per person (5-10 recommended)
- Use well-lit, frontal face photos
- Enable flat search for precise matching
- Adjust the cosine similarity threshold
- Switch to the 512-dimensional model for better accuracy
Next steps
Quick start guide
Learn how to use the app for face recognition
Architecture overview
Understand the technical implementation