ClassificationService class
Service for handling mosquito species classification using PyTorch models.
This service provides functionality to load a pre-trained mosquito classification model and classify images to identify mosquito species. It handles model loading, image preprocessing, and prediction results using on-device machine learning.
Usage Example
final pytorchWrapper = PytorchWrapper();
final classificationService = ClassificationService(
pytorchWrapper: pytorchWrapper,
);
// Load the model (required before classification)
await classificationService.loadModel();
// Classify an image
final imageFile = File('/path/to/mosquito_image.jpg');
final result = await classificationService.classifyImage(imageFile);
print('Species: ${result['scientificName']}');
print('Confidence: ${result['confidence']}');
Model Architecture
The service uses a PyTorch Lite model optimized for mobile deployment:
- Model: Pre-trained CNN for mosquito species classification
- Input: 224x224 RGB images with ImageNet normalization
- Output: Species probabilities across trained classes
- Performance: Optimized for real-time inference on mobile devices
Supported Platforms
- ✅ Android (API 21+)
- ✅ iOS (iOS 12.0+)
- ❌ Web (PyTorch Lite not supported)
- ❌ Desktop (PyTorch Lite not supported)
Error Handling
The service handles various error conditions:
- Platform not supported (throws PlatformException)
- Model loading failures (throws Exception)
- Invalid image formats (handled by PyTorch Lite)
- Memory constraints (handled by PyTorch Lite)
See also:
- ClassificationRepository for higher-level classification operations
- PytorchWrapper for the underlying PyTorch integration
- ClassificationModel for the model interface
Constructors
- ClassificationService({required PytorchWrapper pytorchWrapper})
- Creates a new classification service with the provided PyTorch wrapper.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isModelLoaded → bool
-
Checks if the classification model is loaded and ready for use.
no setter
- model → ClassificationModel?
-
Gets the current model instance.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- stopwatch → Stopwatch
-
Stopwatch for performance measurement.
final
Methods
-
classifyImage(
File imageFile) → Future< Map< String, dynamic> > - Classifies a mosquito image and returns the predicted species and confidence.
-
loadModel(
) → Future< void> - Loads the mosquito classification model from the specified path.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited