liveness method
Future<bool>
liveness(
- CameraController controller
)
Implementation
Future<bool> liveness(CameraController controller) async
{
try
{
final List<XFile> frames = [];
for (int i = 0; i < 3; i++)
{
frames.add(await controller.takePicture());
if (i < 2) await Future.delayed(Duration(milliseconds: 500));
}
double totalDiff = 0;
for (int i = 0; i < frames.length - 1; i++)
{
final bytes1 = await frames[i].readAsBytes();
final bytes2 = await frames[i + 1].readAsBytes();
final img1 = img.decodeImage(bytes1);
final img2 = img.decodeImage(bytes2);
if (img1 == null || img2 == null) return false;
final small1 = img.copyResize(img1, width: 64, height: 64);
final small2 = img.copyResize(img2, width: 64, height: 64);
int pixelDiff = 0;
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
final p1 = small1.getPixel(x, y);
final p2 = small2.getPixel(x, y);
pixelDiff += ((p1.r - p2.r).abs() + (p1.g - p2.g).abs() + (p1.b - p2.b).abs()).toInt();
}
}
totalDiff += pixelDiff / (64 * 64 * 3);
}
final avgDiff = totalDiff / 2;
return avgDiff > livenessMin! && avgDiff < livenessMax!;
}
catch (e, s)
{
print("Liveness error: $e\n$s");
return false;
}
}