classifyImage method
Classifies a mosquito image and returns enriched results with species data.
This method performs a complete classification workflow:
- Runs local ML model inference on the image
- Enriches results with species data from database
- Retrieves associated diseases for identified species
- Returns comprehensive classification results
imageFile The image file to classify.
languageCode The language code (e.g., 'en', 'es') for localized content.
Returns a ClassificationResult with species, confidence, and related diseases.
Throws an exception if classification fails.
Implementation
Future<ClassificationResult> classifyImage(File imageFile, String languageCode) async {
final stopwatch = Stopwatch()..start();
// 1. Get RAW prediction from the service
final rawResult = await _classificationService.classifyImage(imageFile);
final String scientificName = rawResult['scientificName'];
final double confidence = rawResult['confidence'] * 100; // Convert to percentage
print("[DEBUG] Repository: Searching for species with name: '$scientificName'");
// 2. ENRICH the result using MosquitoRepository to fetch full data
MosquitoSpecies? speciesFromDb = await _mosquitoRepository.getMosquitoSpeciesByName(scientificName, languageCode);
print("[DEBUG] Repository: Result from DB is: ${speciesFromDb == null ? 'NULL' : speciesFromDb.name}");
final MosquitoSpecies finalSpecies;
if (speciesFromDb == null) {
// Logic for "unknown" species
finalSpecies = MosquitoSpecies(
id: '0', // Special ID for unknown
name: scientificName, // Show what the model actually predicted
commonName: "Species Not Identified", // Generic fallback
description: "The details for this species are not available in the local database.",
habitat: "N/A",
distribution: "N/A",
imageUrl: "assets/images/species/species_not_defined.jpg",
diseases: [],
);
} else {
finalSpecies = speciesFromDb;
}
// 3. Fetch full Disease objects for the related diseases
List<Disease> relatedDiseases = [];
if (finalSpecies.id != '0') {
relatedDiseases = await _mosquitoRepository.getDiseasesByVector(finalSpecies.name, languageCode);
}
stopwatch.stop();
// 4. Assemble and return the complete, rich ClassificationResult
return ClassificationResult(
species: finalSpecies,
confidence: confidence,
inferenceTime: stopwatch.elapsedMilliseconds,
relatedDiseases: relatedDiseases,
imageFile: imageFile,
);
}