onAvailable method

dynamic onAvailable(
  1. CameraImage image
)

Implementation

onAvailable(CameraImage image) async {
  _throttler.run(() async {
    if (livenessMode.value) {
      var cm = commands[currentCommand.value];
      var isExpression = cm == ECommand.smile;
      var attr = await luxand!.getFaceAttribute(image, isExpression);

      if (isExpression) {
        // const double SMILE_CONF_LEVEL_LOW = 0.3;
        const double SMILE_CONF_LEVEL_HIGH = 0.6;

        if (attr[0] > SMILE_CONF_LEVEL_HIGH && cm == ECommand.smile) {
          print(
              '========================================== Smile attr=${attr[0]}, SMILE_CONF_LEVEL_HIGH=$SMILE_CONF_LEVEL_HIGH');
          await approveCommand();
        }
      } else {
        if (cm == ECommand.turnLeft ||
            cm == ECommand.turnRight ||
            cm == ECommand.lookStraight) {
          const double HOR_CONF_LEVEL_LOW = 2;
          const double HOR_CONF_LEVEL_HIGH = 17;

          if (attr[0].abs() > HOR_CONF_LEVEL_HIGH) {
            if (attr[0] > 0 && cm == ECommand.turnLeft) {
              print(
                  '========================================== Turn Left attr=${attr[0]}, HOR_CONF_LEVEL_HIGH=$HOR_CONF_LEVEL_HIGH');
              await approveCommand();
            } else if (attr[0] <= 0 && cm == ECommand.turnRight) {
              print(
                  '========================================== Turn Right attr=${attr[0]}, HOR_CONF_LEVEL_HIGH=$HOR_CONF_LEVEL_HIGH');
              await approveCommand();
            }
          } else if (attr[0].abs() < HOR_CONF_LEVEL_LOW &&
              cm == ECommand.lookStraight) {
            print(
                '========================================== Look Straight attr=${attr[0]}, HOR_CONF_LEVEL_LOW=$HOR_CONF_LEVEL_LOW');
            await approveCommand();
          }
        } else {
          // const double UP_CONF_LEVEL_LOW = 2;
          const double UP_CONF_LEVEL_HIGH = 6;
          // const double DOWN_CONF_LEVEL_LOW = -2;
          const double DOWN_CONF_LEVEL_HIGH = -3;

          if (attr[1] > UP_CONF_LEVEL_HIGH && cm == ECommand.lookUp) {
            print(
                '========================================== Look Up attr=${attr[1]}, UP_CONF_LEVEL_HIGH=$UP_CONF_LEVEL_HIGH');
            await approveCommand();
          } else if (attr[1] < DOWN_CONF_LEVEL_HIGH &&
              cm == ECommand.lookDown) {
            print(
                '========================================== Look Down attr=${attr[1]}, DOWN_CONF_LEVEL_HIGH=$DOWN_CONF_LEVEL_HIGH');
            await approveCommand();
          }
        }
      }
    } else {
      if (gettingCurrentImage) {
        if (!isFoundFace.value) {
          return;
        }

        var ft = await luxand!.getFaceTemplate(image);
        var png = await convertYUV420toImageColor(image);

        if (png != null) {
          templates.add(TemplateModel(face: ft[1], eye: ft[0]));

          faces.value.add(png);
          faces.refresh();

          if (faces.length >= 3) {
            livenessMode.value = true;
            isFoundFace.value = false;

            commands = getCommands();
          }
        }

        gettingCurrentImage = false;
      } else {
        var data = await luxand!.detectFace(image);

        if (data == null) {
          return;
        }

        var isFound = data[0] > 0;
        isFoundFace.value = isFound;

        if (isFound) {
          faceX.value = data[0].toDouble();
          faceY.value = data[1].toDouble();
          faceW.value = data[2].toDouble();
          age.value = data[3];
          isMale.value = data[4] == 1;
        }
      }
    }
  });
}