classifyImage method

Future<void> classifyImage(
  1. File image
)

Implementation

Future<void> classifyImage(File image) async {
  var connectivityResult = await (Connectivity().checkConnectivity());
  if (connectivityResult == ConnectivityResult.none) {
    Get.dialog(
      AlertDialog(
        title: Text('err'.tr),
        content: Text('internet'.tr),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text('ok'.tr),
          ),
        ],
      ),
      barrierDismissible: false,
    );
    return;
  }
  _loading.value = true;

  ImageClassifier imageClassifier = ImageClassifier();

  try {
    List<Predictions> predictions = await imageClassifier.classify(image);
    _output.assignAll(predictions);

    bool containsTeeth = _output.any((prediction) =>
        prediction.tagName == 'teeth' && prediction.probability! > 0.5);

    if (!containsTeeth) {
      Get.dialog(
        AlertDialog(
          title: Text('noTeeth'.tr),
          content: Text('pTakePhoto'.tr),
          actions: [
            TextButton(
              onPressed: () => Get.back(),
              child: Text('ok'.tr),
            ),
          ],
        ),
        barrierDismissible: false,
      );
    } else {
      bool hasCavity = await imageClassifier.checkForCavity(image);

      _resultText = hasCavity ? 'hasCavity'.tr : 'hasNoCavity'.tr;

      Get.dialog(
        AlertDialog(
          title: Text('teethRes'.tr),
          content: Text(_resultText),
          actions: [
            TextButton(
              onPressed: () => Get.back(),
              child: Text('ok'.tr),
            ),
          ],
        ),
        barrierDismissible: false,
      );

      _pinchToZoomOverlayVisible.value = true;
    }
  } catch (e) {
    if (kDebugMode) {
      print('Error while processing image classification: $e');
    }

    Get.dialog(
      AlertDialog(
        title: const Text('Error'),
        content: const Text('An error occurred. Please try again later.'),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: const Text('OK'),
          ),
        ],
      ),
      barrierDismissible: false,
    );
  } finally {
    _loading.value = false;
  }
}