controller.dart 5.01 KB
Newer Older
1 2 3 4 5 6 7 8
import 'dart:async';
import 'dart:io';
import 'dart:math';

import 'package:camera/camera.dart';
import 'package:get/get.dart';
import 'package:myhr_facescan/active_liveness/enum.dart';
import 'package:myhr_facescan/active_liveness/face_gesture_verifier.dart';
Chokmongkhon Jansanom committed
9
import 'package:myhr_facescan/models/template.dart';
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
import 'package:myhr_facescan/luxand_controller.dart';
import 'package:myhr_facescan/utils/camera.dart';
import 'package:myhr_facescan/utils/throttler.dart';

class ActiveLivenessController extends GetxController {
  ActiveLivenessController({
    required String licenseKey,
    required void Function(bool, TemplateModel?) onFinish,
    required int imageStreamMilliSecond,
    required int failAcceptableInSecond,
  })  : _imageStreamMilliSecond = imageStreamMilliSecond,
        _onFinish = onFinish,
        _licenseKey = licenseKey {
    _throttler = Throttler(milliSeconds: _imageStreamMilliSecond);
    _luxand = Get.put(LuxandController(licenseKey: _licenseKey));
    _commands = getCommands();
    _failRatio = 1 ~/ (_imageStreamMilliSecond / 1000) * failAcceptableInSecond;
  }

  CameraController? camController;
  double previewScale = 0;
  late Throttler _throttler;
  int failFrame = 0;
  // StreamSubscription<int> _timer = null;

  final String _licenseKey;
  final void Function(bool, TemplateModel?) _onFinish;
  final int _imageStreamMilliSecond;
  late List<EFaceGesture> _commands;
  late int _failRatio;

  var currentCommand = 0.obs;
  var isCameraReady = false.obs;

  var isWaitNextCommand = false.obs;
  late LuxandController _luxand;
  final verifier = FaceGestureVerifier();

  @override
  void onInit() async {
    super.onInit();

    await _luxand.initialize();
    await initCamera();

    camController!.initialize().then((_) {
      isCameraReady.value = true;
      previewScale = getCameraPreviewScale(camController!.value.aspectRatio);

      // Only open and close camera in iOS for low-tier device
      if (Platform.isIOS) {
        // _timer = Stream.periodic(const Duration(milliseconds: 500), (v) => v)
        //     .listen((count) async {
        //   _throttler.run(() async {
        //     controller.startImageStream((image) async {});

        //     Future.delayed(const Duration(milliseconds: 50), () async {
        //       await controller.stopImageStream();
        //     });
        //   });
        // });
      } else {
        camController!.startImageStream(_handleCameraAndroid);
      }
    });
  }

  initCamera() async {
    camController = await getCameraController();
  }

  @override
  void onClose() async {
    super.onClose();
    await _clearResource();
  }

  get commandName {
    switch (_commands[currentCommand.value]) {
      case EFaceGesture.lookStraight:
        return 'มองตรง';
      case EFaceGesture.turnLeft:
        return 'หันไปทางซ้าย';
      case EFaceGesture.turnRight:
        return 'หันไปทางขวา';
      case EFaceGesture.lookUp:
        return 'เงยหน้า';
      case EFaceGesture.lookDown:
        return 'ก้มหน้า';
      case EFaceGesture.smile:
        return 'ยิ้ม';
      default:
        return 'N/A';
    }
  }

  _clearResource() async {
    isCameraReady.value = false;
108 109
    await camController?.stopImageStream();
    await camController?.dispose();
110 111 112 113 114 115 116 117 118 119 120 121 122 123

    await _luxand.freeResource();
  }

  _handleCameraAndroid(CameraImage image) async {
    _throttler.run(() async {
      if (isWaitNextCommand.value) {
        return;
      }

      var command = _commands[currentCommand.value];
      var isExpression = command == EFaceGesture.smile;
      var attr = await _luxand.getFaceAttribute(image, isExpression);

Chokmongkhon Jansanom committed
124
      if (attr.isEmpty || (attr.isNotEmpty && attr[0] == 0 && attr[1] == 0)) {
125
        await _handleFail();
126
        return;
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
      }

      var isMatch = verifier.isMatch(attr[0], attr[1], command);

      if (isMatch) {
        await approveCommand();
      }
    });
  }

  _handleFail() async {
    failFrame++;

    if (failFrame >= _failRatio) {
      _onFinish(false, null);
      await _clearResource();
    }
  }

  approveCommand() async {
    if (currentCommand.value + 1 >= _commands.length) {
      _onFinish(true, null);
      await _clearResource();
    } else {
      currentCommand.value += 1;
    }

    isWaitNextCommand.value = true;

    Timer(const Duration(seconds: 2), () => isWaitNextCommand.value = false);
  }

  List<EFaceGesture> getCommands() {
    var result = <EFaceGesture>[];

    while (true) {
      if (result.contains(EFaceGesture.lookUp) &&
          result.contains(EFaceGesture.lookDown) &&
          result.contains(EFaceGesture.turnLeft) &&
          result.contains(EFaceGesture.turnRight) &&
          result.contains(EFaceGesture.smile)) {
        break;
      }

      do {
        var cm = EFaceGesture.fromInt(Random().nextInt(6));

        if (!result.contains(cm) && cm != EFaceGesture.lookStraight) {
          result.add(cm);
          break;
        }
      } while (true);
    }

    result.insert(2, EFaceGesture.lookStraight);

    return result;
  }
}