classifyImage method
- AppLocalizations localizations
Classifies the currently selected image using the local model.
Processes the image and updates the UI with classification results. Handles unknown species by providing localized descriptions.
Parameters:
- localizations: Used for localized strings and error messages
Updates:
- _state: Updates to loading/success/error states
- _result: Contains classification results on success
- _errorMessage: Contains error details if classification fails
Implementation
Future<void> classifyImage(AppLocalizations localizations) async {
if (_imageFile == null) {
_errorMessage = localizations.errorNoImageSelected;
notifyListeners();
return;
}
try {
_state = ClassificationState.loading;
_errorMessage = null;
notifyListeners();
// 1. Get the enriched (or marked) result from the repository
final resultFromRepo = await _repository.classifyImage(
_imageFile!,
localizations.localeName,
);
// 2. Check for the "unknown species" marker
if (resultFromRepo.species.id == '0') {
// If it's the unknown species, create a NEW, LOCALIZED species object
final localizedUnknownSpecies = MosquitoSpecies(
id: resultFromRepo.species.id,
name: resultFromRepo.species.name,
commonName: localizations.classificationServiceUnknownSpeciesCommonName,
description: localizations.classificationServiceUnknownSpeciesDescription,
habitat: localizations.classificationServiceUnknownSpeciesHabitat,
distribution: localizations.classificationServiceUnknownSpeciesDistribution,
imageUrl: resultFromRepo.species.imageUrl, // Keep the placeholder image
diseases: [], // Should be empty
);
// 3. Update the final result with our new localized object
_result = ClassificationResult(
species: localizedUnknownSpecies, // Use the localized version
confidence: resultFromRepo.confidence,
inferenceTime: resultFromRepo.inferenceTime,
relatedDiseases: resultFromRepo.relatedDiseases,
imageFile: resultFromRepo.imageFile,
);
} else {
// If the species was found, the result from the repo is already perfect
_result = resultFromRepo;
}
_state = ClassificationState.success;
notifyListeners();
} catch (e) {
_state = ClassificationState.error;
_errorMessage = localizations.errorClassificationFailed(e.toString());
notifyListeners();
}
}