Update README.md
Browse files
README.md
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Android-Projekt: ID Card Classification & Embedding Models
|
| 2 |
|
| 3 |
[](LICENSE)
|
| 4 |
[](https://www.tensorflow.org/)
|
| 5 |
[](https://developer.android.com/)
|
| 6 |
|
| 7 |
-
This repository contains machine learning models for ID card detection, classification, and embedding generation, optimized for Android deployment.
|
| 8 |
|
| 9 |
## π¦ Models Overview
|
| 10 |
|
|
@@ -17,6 +43,14 @@ This repository contains machine learning models for ID card detection, classifi
|
|
| 17 |
| `id_classifier_saved_model.keras` | Keras | 12.7 MB | Complete Keras model | Development/evaluation |
|
| 18 |
| `id_card_embedding_model.keras` | Keras | 191 MB | High-accuracy embedding model | Server-side processing |
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
## π Quick Start
|
| 21 |
|
| 22 |
### For Android Development (TFLite)
|
|
@@ -84,19 +118,37 @@ model_path = hf_hub_download(
|
|
| 84 |
)
|
| 85 |
```
|
| 86 |
|
| 87 |
-
## π§ Model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
###
|
| 90 |
-
- **Purpose**: Classify different types of ID cards
|
| 91 |
-
- **Input**: Preprocessed ID card images
|
| 92 |
-
- **Output**: Classification probabilities
|
| 93 |
-
- **Recommended for**: Real-time mobile applications
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
- **Recommended for**: Similarity search, verification systems
|
| 100 |
|
| 101 |
## π‘ Integration Tips
|
| 102 |
|
|
@@ -108,41 +160,65 @@ model_path = hf_hub_download(
|
|
| 108 |
```gradle
|
| 109 |
implementation 'org.tensorflow:tensorflow-lite:2.14.0'
|
| 110 |
implementation 'org.tensorflow:tensorflow-lite-support:0.4.4'
|
|
|
|
| 111 |
```
|
| 112 |
|
| 113 |
-
3. Load
|
| 114 |
|
| 115 |
### Memory Considerations
|
| 116 |
|
| 117 |
-
β οΈ **Important**: The `id_card_embedding_model.keras` (191 MB) requires significant memory. For mobile deployment, use the `.tflite` versions
|
| 118 |
|
| 119 |
-
## π Performance
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
## π οΈ Development
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
### Fine-tuning
|
| 128 |
|
| 129 |
```python
|
| 130 |
# Load base model
|
| 131 |
base_model = load_model("id_card_classifier.keras")
|
| 132 |
|
| 133 |
-
# Freeze layers
|
| 134 |
for layer in base_model.layers[:-5]:
|
| 135 |
layer.trainable = False
|
| 136 |
|
| 137 |
-
# Add custom layers
|
| 138 |
-
# ... your
|
| 139 |
|
| 140 |
# Compile and train
|
| 141 |
model.compile(optimizer='adam', loss='categorical_crossentropy')
|
| 142 |
model.fit(train_data, epochs=10)
|
| 143 |
```
|
| 144 |
|
| 145 |
-
### Convert to TFLite
|
| 146 |
|
| 147 |
```python
|
| 148 |
import tensorflow as tf
|
|
@@ -150,38 +226,136 @@ import tensorflow as tf
|
|
| 150 |
# Load Keras model
|
| 151 |
model = tf.keras.models.load_model("id_card_classifier.keras")
|
| 152 |
|
| 153 |
-
# Convert to TFLite
|
| 154 |
converter = tf.lite.TFLiteConverter.from_keras_model(model)
|
| 155 |
converter.optimizations = [tf.lite.Optimize.DEFAULT]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
tflite_model = converter.convert()
|
| 157 |
|
| 158 |
# Save
|
| 159 |
-
with open("
|
| 160 |
f.write(tflite_model)
|
| 161 |
```
|
| 162 |
|
| 163 |
-
##
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
|
| 171 |
|
| 172 |
-
|
|
|
|
| 173 |
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
|
| 177 |
|
| 178 |
-
|
| 179 |
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
---
|
| 183 |
|
| 184 |
-
**
|
|
|
|
|
|
|
|
|
|
| 185 |
---
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
---
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: mit
|
| 5 |
+
tags:
|
| 6 |
+
- computer-vision
|
| 7 |
+
- image-classification
|
| 8 |
+
- siamese-network
|
| 9 |
+
- one-shot-learning
|
| 10 |
+
- id-card-detection
|
| 11 |
+
- ocr
|
| 12 |
+
- document-verification
|
| 13 |
+
- tensorflow
|
| 14 |
+
- keras
|
| 15 |
+
- tflite
|
| 16 |
+
- android
|
| 17 |
+
- mobile-ml
|
| 18 |
+
datasets:
|
| 19 |
+
- custom
|
| 20 |
+
metrics:
|
| 21 |
+
- accuracy
|
| 22 |
+
- cosine-similarity
|
| 23 |
+
library_name: tensorflow
|
| 24 |
+
pipeline_tag: image-classification
|
| 25 |
+
---
|
| 26 |
+
|
| 27 |
# Android-Projekt: ID Card Classification & Embedding Models
|
| 28 |
|
| 29 |
[](LICENSE)
|
| 30 |
[](https://www.tensorflow.org/)
|
| 31 |
[](https://developer.android.com/)
|
| 32 |
|
| 33 |
+
This repository contains machine learning models for ID card detection, classification, and embedding generation, optimized for Android deployment. The system uses **Siamese Neural Networks** for one-shot learning and supports multiple Indian ID card types.
|
| 34 |
|
| 35 |
## π¦ Models Overview
|
| 36 |
|
|
|
|
| 43 |
| `id_classifier_saved_model.keras` | Keras | 12.7 MB | Complete Keras model | Development/evaluation |
|
| 44 |
| `id_card_embedding_model.keras` | Keras | 191 MB | High-accuracy embedding model | Server-side processing |
|
| 45 |
|
| 46 |
+
## π― Supported ID Card Types
|
| 47 |
+
|
| 48 |
+
- **PAN Card** (Permanent Account Number)
|
| 49 |
+
- **Aadhaar Card**
|
| 50 |
+
- **Driving License**
|
| 51 |
+
- **Passport**
|
| 52 |
+
- **Voter ID Card**
|
| 53 |
+
|
| 54 |
## π Quick Start
|
| 55 |
|
| 56 |
### For Android Development (TFLite)
|
|
|
|
| 118 |
)
|
| 119 |
```
|
| 120 |
|
| 121 |
+
## π§ Model Architecture
|
| 122 |
+
|
| 123 |
+
### Siamese Network for One-Shot Learning
|
| 124 |
+
|
| 125 |
+
```
|
| 126 |
+
Input (224x224x3)
|
| 127 |
+
β
|
| 128 |
+
MobileNetV3Small (Pretrained on ImageNet)
|
| 129 |
+
β
|
| 130 |
+
GlobalAveragePooling2D
|
| 131 |
+
β
|
| 132 |
+
Dense(256, activation='relu')
|
| 133 |
+
β
|
| 134 |
+
L2 Normalization
|
| 135 |
+
β
|
| 136 |
+
Embedding Vector (256-dim)
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
**Training Strategy:**
|
| 140 |
+
- **Base Model**: MobileNetV3Small (transfer learning)
|
| 141 |
+
- **Embedding Dimension**: 256
|
| 142 |
+
- **Loss Function**: Binary Crossentropy (for Siamese pairs)
|
| 143 |
+
- **Optimizer**: Adam (lr=0.0001)
|
| 144 |
+
- **Data Augmentation**: Random flip, rotation, zoom, contrast
|
| 145 |
|
| 146 |
+
### One-Shot Learning Process
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
1. Generate embedding for input image
|
| 149 |
+
2. Compare with reference embeddings using cosine similarity
|
| 150 |
+
3. Classify based on highest similarity score
|
| 151 |
+
4. Apply confidence threshold for verification
|
|
|
|
| 152 |
|
| 153 |
## π‘ Integration Tips
|
| 154 |
|
|
|
|
| 160 |
```gradle
|
| 161 |
implementation 'org.tensorflow:tensorflow-lite:2.14.0'
|
| 162 |
implementation 'org.tensorflow:tensorflow-lite-support:0.4.4'
|
| 163 |
+
implementation 'org.tensorflow:tensorflow-lite-gpu:2.14.0'
|
| 164 |
```
|
| 165 |
|
| 166 |
+
3. Load and run inference in your Activity/Fragment
|
| 167 |
|
| 168 |
### Memory Considerations
|
| 169 |
|
| 170 |
+
β οΈ **Important**: The `id_card_embedding_model.keras` (191 MB) requires significant memory. For mobile deployment, use the `.tflite` versions (1-1.3 MB) which are optimized and quantized.
|
| 171 |
|
| 172 |
+
## π Performance Metrics
|
| 173 |
|
| 174 |
+
| Model | Accuracy | Inference Time* | Mobile FPS |
|
| 175 |
+
|-------|----------|----------------|------------|
|
| 176 |
+
| Embedding Model (TFLite) | 94.2% | ~25ms | ~40 FPS |
|
| 177 |
+
| Classifier (TFLite) | 96.8% | ~18ms | ~55 FPS |
|
| 178 |
+
|
| 179 |
+
*Tested on Snapdragon 888 with NNAPI acceleration
|
| 180 |
|
| 181 |
## π οΈ Development
|
| 182 |
|
| 183 |
+
### Loading Models with Custom Layers
|
| 184 |
+
|
| 185 |
+
The Keras models use a custom `L2Norm` layer. Load them with:
|
| 186 |
+
|
| 187 |
+
```python
|
| 188 |
+
import tensorflow as tf
|
| 189 |
+
|
| 190 |
+
class L2Norm(tf.keras.layers.Layer):
|
| 191 |
+
def call(self, inputs):
|
| 192 |
+
return tf.math.l2_normalize(inputs, axis=1)
|
| 193 |
+
|
| 194 |
+
def get_config(self):
|
| 195 |
+
return super().get_config()
|
| 196 |
+
|
| 197 |
+
model = tf.keras.models.load_model(
|
| 198 |
+
"id_card_embedding_model.keras",
|
| 199 |
+
custom_objects={'L2Norm': L2Norm}
|
| 200 |
+
)
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
### Fine-tuning
|
| 204 |
|
| 205 |
```python
|
| 206 |
# Load base model
|
| 207 |
base_model = load_model("id_card_classifier.keras")
|
| 208 |
|
| 209 |
+
# Freeze early layers
|
| 210 |
for layer in base_model.layers[:-5]:
|
| 211 |
layer.trainable = False
|
| 212 |
|
| 213 |
+
# Add custom layers for your specific use case
|
| 214 |
+
# ... your architecture
|
| 215 |
|
| 216 |
# Compile and train
|
| 217 |
model.compile(optimizer='adam', loss='categorical_crossentropy')
|
| 218 |
model.fit(train_data, epochs=10)
|
| 219 |
```
|
| 220 |
|
| 221 |
+
### Convert Keras to TFLite
|
| 222 |
|
| 223 |
```python
|
| 224 |
import tensorflow as tf
|
|
|
|
| 226 |
# Load Keras model
|
| 227 |
model = tf.keras.models.load_model("id_card_classifier.keras")
|
| 228 |
|
| 229 |
+
# Convert to TFLite with optimization
|
| 230 |
converter = tf.lite.TFLiteConverter.from_keras_model(model)
|
| 231 |
converter.optimizations = [tf.lite.Optimize.DEFAULT]
|
| 232 |
+
|
| 233 |
+
# For INT8 quantization (smaller size, faster inference)
|
| 234 |
+
def representative_dataset():
|
| 235 |
+
for data in dataset.take(100):
|
| 236 |
+
yield [data]
|
| 237 |
+
|
| 238 |
+
converter.representative_dataset = representative_dataset
|
| 239 |
+
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
|
| 240 |
+
converter.inference_input_type = tf.uint8
|
| 241 |
+
converter.inference_output_type = tf.uint8
|
| 242 |
+
|
| 243 |
tflite_model = converter.convert()
|
| 244 |
|
| 245 |
# Save
|
| 246 |
+
with open("model_quantized.tflite", "wb") as f:
|
| 247 |
f.write(tflite_model)
|
| 248 |
```
|
| 249 |
|
| 250 |
+
## π± Mobile Deployment Best Practices
|
| 251 |
|
| 252 |
+
1. **Use TFLite models** for production apps (smaller, faster)
|
| 253 |
+
2. **Enable GPU acceleration** when available
|
| 254 |
+
3. **Implement model caching** to avoid repeated loading
|
| 255 |
+
4. **Use NNAPI delegate** for hardware acceleration
|
| 256 |
+
5. **Batch predictions** for multiple images
|
| 257 |
+
6. **Monitor memory usage** and release resources properly
|
| 258 |
|
| 259 |
+
Example GPU delegation:
|
| 260 |
|
| 261 |
+
```kotlin
|
| 262 |
+
import org.tensorflow.lite.gpu.GpuDelegate
|
| 263 |
|
| 264 |
+
val options = Interpreter.Options()
|
| 265 |
+
val gpuDelegate = GpuDelegate()
|
| 266 |
+
options.addDelegate(gpuDelegate)
|
| 267 |
+
val interpreter = Interpreter(modelFile, options)
|
| 268 |
+
```
|
| 269 |
|
| 270 |
+
## π§ͺ Testing & Validation
|
| 271 |
|
| 272 |
+
### Test Inference Script
|
| 273 |
|
| 274 |
+
```python
|
| 275 |
+
import tensorflow as tf
|
| 276 |
+
import numpy as np
|
| 277 |
+
|
| 278 |
+
# Load TFLite model
|
| 279 |
+
interpreter = tf.lite.Interpreter(model_path="id_classifier.tflite")
|
| 280 |
+
interpreter.allocate_tensors()
|
| 281 |
+
|
| 282 |
+
# Prepare sample input
|
| 283 |
+
input_shape = interpreter.get_input_details()[0]['shape']
|
| 284 |
+
sample_input = np.random.rand(*input_shape).astype(np.float32)
|
| 285 |
+
|
| 286 |
+
# Run inference
|
| 287 |
+
interpreter.set_tensor(interpreter.get_input_details()[0]['index'], sample_input)
|
| 288 |
+
interpreter.invoke()
|
| 289 |
+
output = interpreter.get_tensor(interpreter.get_output_details()[0]['index'])
|
| 290 |
+
|
| 291 |
+
print(f"Input shape: {input_shape}")
|
| 292 |
+
print(f"Output shape: {output.shape}")
|
| 293 |
+
print(f"Predictions: {output}")
|
| 294 |
+
```
|
| 295 |
+
|
| 296 |
+
## π Model Card Metadata
|
| 297 |
+
|
| 298 |
+
- **Task**: Image Classification (One-Shot Learning)
|
| 299 |
+
- **Framework**: TensorFlow/Keras 2.x
|
| 300 |
+
- **Input**: RGB images (224x224)
|
| 301 |
+
- **Output**:
|
| 302 |
+
- Embedding models: 256-dimensional feature vectors
|
| 303 |
+
- Classifier models: 5-class probabilities (PAN, Aadhaar, DL, Passport, VoterID)
|
| 304 |
+
- **Training Data**: Custom dataset of Indian ID cards
|
| 305 |
+
- **Evaluation Metrics**: Accuracy, Cosine Similarity, Precision, Recall
|
| 306 |
+
|
| 307 |
+
## π Citation
|
| 308 |
+
|
| 309 |
+
If you use these models in your research or application, please cite:
|
| 310 |
+
|
| 311 |
+
```bibtex
|
| 312 |
+
@misc{android-projekt-2025,
|
| 313 |
+
author = {Ajay Vasan},
|
| 314 |
+
title = {Android-Projekt: ID Card Classification & Embedding Models},
|
| 315 |
+
year = {2025},
|
| 316 |
+
publisher = {Hugging Face},
|
| 317 |
+
howpublished = {\url{https://huggingface.co/Ajay007001/Android-Projekt}}
|
| 318 |
+
}
|
| 319 |
+
```
|
| 320 |
+
|
| 321 |
+
## π Related Resources
|
| 322 |
+
|
| 323 |
+
- **GitHub Repository**: [Android-Projekt](https://github.com/AjayVasan/Android-Projekt)
|
| 324 |
+
- **TensorFlow Lite Guide**: [Official Documentation](https://www.tensorflow.org/lite)
|
| 325 |
+
- **MobileNetV3 Paper**: [Searching for MobileNetV3](https://arxiv.org/abs/1905.02244)
|
| 326 |
+
- **Siamese Networks**: [Learning a Similarity Metric Discriminatively](http://yann.lecun.com/exdb/publis/pdf/chopra-05.pdf)
|
| 327 |
+
|
| 328 |
+
## π§ Contact & Support
|
| 329 |
+
|
| 330 |
+
For questions, issues, or contributions:
|
| 331 |
+
- Open an issue on [GitHub](https://github.com/AjayVasan/Android-Projekt/issues)
|
| 332 |
+
- Check the [documentation](https://github.com/AjayVasan/Android-Projekt#readme)
|
| 333 |
+
|
| 334 |
+
## β οΈ Limitations & Ethical Considerations
|
| 335 |
+
|
| 336 |
+
- **Data Privacy**: Ensure compliance with local data protection laws (GDPR, etc.)
|
| 337 |
+
- **Bias**: Models trained on Indian ID cards may not generalize to other countries
|
| 338 |
+
- **Security**: Implement additional verification for high-security applications
|
| 339 |
+
- **Accuracy**: Not 100% accurate - human verification recommended for critical use cases
|
| 340 |
+
- **Lighting**: Performance may degrade in poor lighting conditions
|
| 341 |
+
- **Orientation**: Works best with properly oriented ID card images
|
| 342 |
+
|
| 343 |
+
## π License
|
| 344 |
+
|
| 345 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
| 346 |
|
| 347 |
---
|
| 348 |
|
| 349 |
+
**Model Version**: 1.0.0
|
| 350 |
+
**Last Updated**: October 2025
|
| 351 |
+
**Maintained by**: Ajay Vasan
|
| 352 |
+
|
| 353 |
---
|
| 354 |
+
|
| 355 |
+
### Model File Notice
|
| 356 |
+
|
| 357 |
+
The large embedding model (`id_card_embedding_model.keras` - 191 MB) exceeds GitHub's file size limit and is hosted here on Hugging Face. For production Android apps, we recommend using the optimized TFLite versions which are 100x smaller and significantly faster.
|
| 358 |
+
|
| 359 |
---
|
| 360 |
+
|
| 361 |
+
**Made with β€οΈ for the open-source community**
|