classifyImage method

Future<ClassificationResult> classifyImage(
  1. File imageFile,
  2. String languageCode
)

Classifies a mosquito image and returns enriched results with species data.

This method performs a complete classification workflow:

  1. Runs local ML model inference on the image
  2. Enriches results with species data from database
  3. Retrieves associated diseases for identified species
  4. 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,
  );
}