A Flutter application that performs on-device machine learning for plant analysis using both traditional classification and zero-shot learning approaches.
-
Dual ML Approach:
- Traditional Classification: Plant species identification
- Zero-shot Learning: Flexible image analysis with custom prompts
- On-device processing for privacy and speed
-
Key Capabilities:
- Image capture from camera or gallery
- Automatic image optimization
- Real-time plant classification
- Custom analysis queries
- Model management and downloads
-
Controllers:
ClassificationController
: Manages plant classification workflow- Model loading and caching
- Image processing pipeline
- Classification inference
ZeroShotController
: Handles flexible image analysis- CLIP model coordination
- Custom prompt processing
- Zero-shot inference
SettingsController
: Manages app configuration- Model download management
- Storage management
- Configuration persistence
-
Views:
MainLayout
: Navigation and layout orchestrationClassificationView
: Plant species identification UIZeroShotView
: Custom analysis interfaceSettingsView
: Model and configuration managementCompleteWorkflowView
: Combined analysis workflow
Image Input → Preprocessing → PyTorch Model → Species Prediction
│
├─ Preprocessing:
│ ├─ Size validation (≤10MB)
│ ├─ Resolution adjustment (1024x1024)
│ └─ Quality optimization (85%)
│
└─ Model Pipeline:
├─ Model loading (cached)
├─ Inference
└─ Result processing
Image Input → CLIP Image Encoder → Feature Vector
│
Custom Prompt → Text Encoder → Feature Vector
│
Similarity Matching
│
Analysis Result
-
Model Storage:
AppDocuments/ ├── CLIPImageEncoder.tflite ├── CLIPTextEncoder.tflite ├── tokenized_prompts.pb ├── model.pt └── labels.txt
-
State Management:
// Reactive States (GetX) _model: Rxn<ClassificationModel> // ML model state _labels: RxList<String> // Classification labels _image: Rxn<File> // Current image _prediction: RxString // Model prediction _isLoading: RxBool // Processing state
-
Hierarchical Error Management:
- Image Processing Errors
- Size validation
- Format validation
- Compression errors
- Model Errors
- Loading failures
- Inference errors
- Resource exhaustion
- System Errors
- Memory constraints
- Storage issues
- Permission problems
- Image Processing Errors
-
Recovery Strategies:
Error Detection → Retry Logic → Fallback Options → User Feedback
-
Memory Management:
- Model caching with static references
- Image compression pipeline
- Resource cleanup in finally blocks
- Garbage collection hints
-
Processing Pipeline:
Input Validation → Size Check → Compression → Processing → Cleanup
-
Caching Strategy:
// Static Cache static ClassificationModel? _cachedModel; static List<String>? _cachedLabels; // Cache Management if (_cachedModel != null) { return _cachedModel; // Avoid reload }
// User Flow
1. Select/Capture Image
2. Automatic Processing:
- Size validation
- Compression
- Model inference
3. Display Results
// Error Recovery
try {
await classifyImage();
} catch (e) {
// Retry logic
// Model reload if needed
// User feedback
}
// Analysis Flow
1. Image Input
2. Custom Prompt Entry
3. Parallel Processing:
- Image encoding
- Text encoding
4. Similarity Computation
5. Result Ranking
- State Management: GetX for reactive state and dependency injection
- ML Models:
- PyTorch Lite for classification
- TensorFlow Lite for zero-shot learning
- CLIP model for flexible image analysis
lib/
├── app/
│ ├── bindings/ # Dependency injection
│ ├── config/ # App configuration
│ ├── controllers/ # Business logic
│ ├── models/ # Data models
│ └── views/ # UI components
├── generated/ # Generated code
└── main.dart # Entry point
-
Prerequisites:
- Flutter SDK ^3.5.4
- Dart SDK ^3.5.4
- Android Studio / VS Code with Flutter extensions
-
Installation:
git clone [repository-url] cd ondevice_ml flutter pub get
-
Run the App:
flutter run
dependencies:
pytorch_lite: ^4.3.2
tflite_flutter: ^0.11.0
get: ^4.6.6
image_picker: ^1.0.7
camera: ^0.11.0+2
# See pubspec.yaml for complete list
- Model caching for faster loading
- Image compression and size validation
- Memory management and resource cleanup
- Error recovery mechanisms
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.