isReal method

Future<bool> isReal(
  1. File imageFile
)

Implementation

Future<bool> isReal(File imageFile) async {
  if (!_modelLoaded) {
    print('[Liveness] ❗ Model not loaded');
    return false;
  }

  try {
    print('[Liveness] 📷 Input image path: ${imageFile.path}');
    print('[Liveness] 📦 Image size: ${await imageFile.length()} bytes');

    final bytes = await imageFile.readAsBytes();
    img.Image? image = img.decodeImage(bytes);
    if (image == null) {
      print('[Liveness] ❌ Failed to decode image');
      return false;
    }

    print('[Liveness] ✅ Image decoded: ${image.width}x${image.height}');

    final resized = img.copyResizeCropSquare(image, size: 224);
    print('[Liveness] 🔧 Image resized to 224x224');

    // Create input tensor [1, 224, 224, 3] with float32
    final input = List.generate(1, (_) =>
        List.generate(224, (y) =>
            List.generate(224, (x) {
              final pixel = resized.getPixel(x, y);
              return [
                pixel.r / 255.0,
                pixel.g / 255.0,
                pixel.b / 255.0
              ];
            })));

    // Create output tensor [1, 1] with float32
    final output = List.generate(1, (_) => List.filled(1, 0.0));

    // Debug input tensor info (optional)
    final inputTensor = _interpreter.getInputTensor(0);
    print('[Liveness] 🧬 Input Tensor Type: ${inputTensor.type}');
    print('[Liveness] 📏 Input Tensor Shape: ${inputTensor.shape}');

    print('[Liveness] 🚀 Running inference...');
    _interpreter.run(input, output);

    final score = output[0][0];
    print('[Liveness] 🤖 Output score: $score');

    final result = score > 0.5;
    print('[Liveness] ✅ Result: ${result ? "Real face" : "Fake face"}');

    return result;
  } catch (e) {
    print('[Liveness] ❌ Liveness check failed: $e');
    return false;
  }
}