isFacePresent static method

Future<(bool, bool)> isFacePresent(
  1. Uint8List uint8list
)

Implementation

static Future<(bool faceDetected, bool isBlurry)> isFacePresent(
    Uint8List uint8list) async {
  try {
    // Step 1: Face detection
    final inputImage = InputImage.fromFilePath(
      (await _writeToTempFile(uint8list)).path,
    );

    final faceDetector = FaceDetector(
      options: FaceDetectorOptions(
        performanceMode: FaceDetectorMode.fast,
        enableLandmarks: false,
        enableContours: false,
        enableClassification: false,
        enableTracking: false,
      ),
    );

    final faces = await faceDetector.processImage(inputImage);
    await faceDetector.close();

    final bool faceDetected = faces.isNotEmpty;

    // Step 2: Blur detection
    final decodedImage = img.decodeImage(uint8list);
    if (decodedImage == null) {
      return (faceDetected, true); // Assume blurry if unreadable
    }

    final grayscale = img.grayscale(decodedImage);
    final laplacian = _computeLaplacian(grayscale);
    final blurScore = _variance(laplacian);

    final bool isBlurry = blurScore < 60; // You can adjust this threshold

    return (faceDetected, isBlurry);
  } catch (e) {
    print("Error detecting face/blur: $e");
    return (false, true); // Assume no face and blurry if failed
  }
}