mirror of
https://github.com/mosip/inji-wallet.git
synced 2026-01-09 05:27:57 -05:00
* [INJIMOB-2381] update package.json and resolve dependencies for react 0.73.0 Signed-off-by: Abhishek Paul <paul.apaul.abhishek.ap@gmail.com> * [INJIMOB-2381] setup for 0.73.6 Signed-off-by: Abhishek Paul <paul.apaul.abhishek.ap@gmail.com> * [INJIMOB-2381] fix font & image not loading issue changes include - move common image assets to android & iOS folders - update gradle version to 8.2 - add expo-font asset for Inter regular - replace useFont with expo-font - remove flipper (reason: 0.74 react native decoupled flipper) - npm package updates - close icon changed to close text in QRScanner.tsx Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> Signed-off-by: Abhishek Paul <paul.apaul.abhishek.ap@gmail.com> * [INJIMOB-2381] update android:mosip script as per updated react native version Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] fix android build failure issue Changes include: - rename android assets folder as per convention - remove unused biometrics dependency - use env from react-native-dotenv as per module registered in babel config - revert conversion of close icon to image in QRScanner component Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] fix iOS build issue Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] rm unused local images linking in adnroid Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] fix splash screen not rendering issue in android Issue: Local assets were not loaded via React Native Image component in android, to fix this @react-native/assets-registry has been added Reference: https://github.com/expo/expo/issues/31240#issuecomment-2459049911 Other changes: - remove usage of expo-image as we dont have any explicit usecase for expo-image and its unused Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] rm unused intro slider image assets Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] revert back using local font assets in android Reason: Previously expo-font was used for fonts, to bring back the same compatiblity it is bought back Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] use GestureHandlerRootView in root level to handle gestures ref - https://docs.swmansion.com/react-native-gesture-handler/docs/1.x/#js Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] remove custom assets and linking for images in iOS Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] use default parameters instead of defaultProps Reason: React 18 has marked defaultProps as deprecated and it is removed in React 19 version. To avoid the issue of deprated usage, its modified. Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] remove unused terminal option in android build script Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] remove font assets in android Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] optimize import Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] fix failing test Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] set package.resolved version to 2 Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] fix setting of camera view in face scanner Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] fix setting of camera view in face compare Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] remove flipper config file Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] remove unused debug logs Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] refactor use component instead of type Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] refactor use GestureHandlerRootView only on root level Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] refactor remove unused config linking of font assets Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] refactor remove unused image assets Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] fix camera persmission issue in QRScanner use expo-camera to request and getstatus of camera permission instead of expo-barcode-scanner to avoid multiple re-renders of scan screen in flow of camera permission denial Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] remove package expo-barcode-scanner expo-barcode-scanner is deprecated and its suggested to use expo-camera to handle the scanning flows. The existing bar-code-scanner implementations have been replaced with expo-camera. Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] use extracted enums instead of legacy expo-camera Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] remove patch for react-native-0.71.8 Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] revert date-fs version to 2.28.0 date-fns is not supporting filipino langauge, so we are maintaining patch for that. The patch created is for version 2.28.0 and updating the version of date-fns is causing conflicts in patch application if updated as well. To avoid this issue, version is kept as 2.28.0 Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] update readme Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] remove usage of legacy expo-camera in getting image type Other changes - remove resolutions block from package.json - sync pbxproj Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> * [INJIMOB-2381] add active prop to expo-camera as per suggestions expo-camera suggests "Only one Camera preview can be active at any given time. If you have multiple screens in your app, you should unmount Camera components whenever a screen is unfocused.". To handle this active prop is used which is set to false on unMount of the component whenever rendering Camera from expo-camera Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> --------- Signed-off-by: Abhishek Paul <paul.apaul.abhishek.ap@gmail.com> Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com> Signed-off-by: adityankannan-tw <109274996+adityankannan-tw@users.noreply.github.com> Co-authored-by: Abhishek Paul <paul.apaul.abhishek.ap@gmail.com> Co-authored-by: adityankannan-tw <109274996+adityankannan-tw@users.noreply.github.com>
268 lines
7.5 KiB
TypeScript
268 lines
7.5 KiB
TypeScript
import hexRgb, {RgbaObject} from 'hex-rgb';
|
|
import {LIVENESS_THRESHOLD, isAndroid} from '../../shared/constants';
|
|
import {closest} from 'color-diff';
|
|
import * as FaceDetector from 'expo-face-detector';
|
|
import ImageEditor from '@react-native-community/image-editor';
|
|
import {getColors} from 'react-native-image-colors';
|
|
import {faceCompare} from '@iriscan/biometric-sdk-react-native';
|
|
|
|
let FaceCropPicArray: any[] = new Array();
|
|
let EyeCropPicArray: any[] = new Array();
|
|
let predictedColorResults: any[] = new Array();
|
|
let facePoints;
|
|
let calculatedThreshold;
|
|
let faceCompareOuptut;
|
|
let capturedFaceImage;
|
|
let leftEyeWasClosed = false;
|
|
let rightEyeWasClosed = false;
|
|
let lastBlinkTimestamp = 0;
|
|
let blinkCounter = 0;
|
|
|
|
const offsetX = 200;
|
|
const offsetY = 350;
|
|
const captureInterval = 650;
|
|
const eyeOpenProbability = 0.85;
|
|
const blinkConfidenceScore = 0.1;
|
|
const blinkThreshold = 0.4;
|
|
const blinkTimeInterval = 900;
|
|
const eyeCropHeightConst = 50;
|
|
const XAndYBoundsMax = 280;
|
|
const XAndYBoundsMin = 300;
|
|
const rollAngleThreshold = 10;
|
|
const yawAngleThreshold = 3;
|
|
const colorFiltered = ['background', 'dominant'];
|
|
const rxDataURI = /data:(?<mime>[\w/\-.]+);(?<encoding>\w+),(?<data>.*)/;
|
|
const colorComparePalette = [
|
|
{R: 255, G: 0, B: 0},
|
|
{R: 0, G: 255, B: 0},
|
|
{R: 0, G: 0, B: 255},
|
|
];
|
|
|
|
export enum ImageType {
|
|
jpg = 'jpg',
|
|
}
|
|
|
|
export const imageCaptureConfig = {
|
|
base64: true,
|
|
quality: 1,
|
|
imageType: ImageType.jpg,
|
|
};
|
|
|
|
export const faceDetectorConfig: FaceDetectorConfig = {
|
|
mode: FaceDetector.FaceDetectorMode.accurate,
|
|
detectLandmarks: FaceDetector.FaceDetectorLandmarks.all,
|
|
runClassifications: FaceDetector.FaceDetectorClassifications.all,
|
|
contourMode: FaceDetector.FaceDetectorClassifications.all,
|
|
minDetectionInterval: captureInterval,
|
|
tracking: true,
|
|
};
|
|
|
|
export const checkBlink = face => {
|
|
const leftEyeOpenProbability = face.leftEyeOpenProbability;
|
|
const rightEyeOpenProbability = face.rightEyeOpenProbability;
|
|
|
|
const currentTime = new Date().getTime();
|
|
|
|
const leftEyeClosed = leftEyeOpenProbability < blinkThreshold;
|
|
const rightEyeClosed = rightEyeOpenProbability < blinkThreshold;
|
|
|
|
if (leftEyeClosed && rightEyeClosed) {
|
|
leftEyeWasClosed = true;
|
|
rightEyeWasClosed = true;
|
|
}
|
|
|
|
if (
|
|
leftEyeWasClosed &&
|
|
rightEyeWasClosed &&
|
|
!leftEyeClosed &&
|
|
!rightEyeClosed
|
|
) {
|
|
if (
|
|
lastBlinkTimestamp === 0 ||
|
|
currentTime - lastBlinkTimestamp > blinkTimeInterval
|
|
) {
|
|
blinkCounter = blinkCounter + 1;
|
|
lastBlinkTimestamp = currentTime;
|
|
}
|
|
leftEyeWasClosed = false;
|
|
rightEyeWasClosed = false;
|
|
}
|
|
};
|
|
|
|
export const getFaceBounds = face => {
|
|
const {bounds, yawAngle, rollAngle} = face;
|
|
|
|
const withinXBounds =
|
|
bounds.origin.x + bounds.size.width >= XAndYBoundsMax &&
|
|
bounds.origin.x <= XAndYBoundsMin;
|
|
const withinYBounds =
|
|
bounds.origin.y + bounds.size.height >= XAndYBoundsMax &&
|
|
bounds.origin.y <= XAndYBoundsMin;
|
|
const withinYawAngle =
|
|
yawAngle > -yawAngleThreshold && yawAngle < yawAngleThreshold;
|
|
const withinRollAngle =
|
|
rollAngle > -rollAngleThreshold && rollAngle < rollAngleThreshold;
|
|
|
|
return [withinXBounds, withinYBounds, withinYawAngle, withinRollAngle];
|
|
};
|
|
|
|
export const getNormalizedFacePoints = (facePoints: any): number[] => {
|
|
return isAndroid()
|
|
? [
|
|
facePoints.LEFT_EYE.x,
|
|
facePoints.LEFT_EYE.y,
|
|
facePoints.RIGHT_EYE.x,
|
|
facePoints.RIGHT_EYE.y,
|
|
]
|
|
: [
|
|
facePoints.leftEyePosition.x,
|
|
facePoints.leftEyePosition.y,
|
|
facePoints.rightEyePosition.x,
|
|
facePoints.rightEyePosition.y,
|
|
];
|
|
};
|
|
|
|
export const filterColor = color => {
|
|
return (
|
|
typeof color === 'string' &&
|
|
color.startsWith('#') &&
|
|
!colorFiltered.includes(color)
|
|
);
|
|
};
|
|
|
|
export const getEyeColorPredictionResult = async (
|
|
LeftrgbaColors: RgbaObject[],
|
|
color: RgbaObject,
|
|
) => {
|
|
LeftrgbaColors.forEach(colorRGBA => {
|
|
let colorRGB = {};
|
|
colorRGB['R'] = colorRGBA.red;
|
|
colorRGB['G'] = colorRGBA.green;
|
|
colorRGB['B'] = colorRGBA.blue;
|
|
|
|
const closestColor = closest(colorRGB, colorComparePalette);
|
|
|
|
const result =
|
|
color.red === closestColor.R &&
|
|
color.blue === closestColor.B &&
|
|
color.green === closestColor.G;
|
|
|
|
predictedColorResults.push(result);
|
|
});
|
|
};
|
|
|
|
export const cropEyeAreaFromFace = async (picArray, vcImage, capturedImage) => {
|
|
try {
|
|
await Promise.all(
|
|
picArray.map(async pic => {
|
|
facePoints = (
|
|
await FaceDetector.detectFacesAsync(pic.image.uri, faceDetectorConfig)
|
|
).faces[0];
|
|
|
|
if (
|
|
facePoints.leftEyeOpenProbability > eyeOpenProbability &&
|
|
facePoints.rightEyeOpenProbability > eyeOpenProbability
|
|
) {
|
|
capturedFaceImage = await ImageEditor.cropImage(pic.image.uri, {
|
|
offset: {
|
|
x: facePoints.bounds.origin.x,
|
|
y: facePoints.bounds.origin.y,
|
|
},
|
|
size: {
|
|
width: facePoints.bounds.size.width,
|
|
height: facePoints.bounds.size.height,
|
|
},
|
|
});
|
|
|
|
FaceCropPicArray.push({color: pic.color, image: capturedFaceImage});
|
|
}
|
|
}),
|
|
);
|
|
|
|
await Promise.all(
|
|
FaceCropPicArray.map(async pic => {
|
|
let [leftEyeX, leftEyeY, rightEyeX, rightEyeY] =
|
|
getNormalizedFacePoints(facePoints);
|
|
|
|
const leftCroppedImage = await ImageEditor.cropImage(pic.image.uri, {
|
|
offset: {
|
|
x: leftEyeX - offsetX,
|
|
y: leftEyeY - offsetY,
|
|
},
|
|
size: {
|
|
width: offsetX * 2,
|
|
height: offsetY / 2 - eyeCropHeightConst,
|
|
},
|
|
});
|
|
|
|
const rightCroppedImage = await ImageEditor.cropImage(pic.image.uri, {
|
|
offset: {
|
|
x: rightEyeX - offsetX,
|
|
y: rightEyeY - offsetY,
|
|
},
|
|
size: {
|
|
width: offsetX * 2,
|
|
height: offsetY / 2 - eyeCropHeightConst,
|
|
},
|
|
});
|
|
|
|
EyeCropPicArray.push({
|
|
color: pic.color,
|
|
leftEye: leftCroppedImage,
|
|
rightEye: rightCroppedImage,
|
|
});
|
|
}),
|
|
);
|
|
|
|
await Promise.all(
|
|
EyeCropPicArray.map(async pic => {
|
|
const leftEyeColors = await getColors(pic.leftEye.uri);
|
|
const rightEyeColors = await getColors(pic.rightEye.uri);
|
|
|
|
const leftRGBAColors = Object.values(leftEyeColors)
|
|
.filter(filterColor)
|
|
.map(color => hexRgb(color));
|
|
|
|
const rightRGBAColors = Object.values(rightEyeColors)
|
|
.filter(filterColor)
|
|
.map(color => hexRgb(color));
|
|
|
|
const rgbColor = hexRgb(pic.color);
|
|
await getEyeColorPredictionResult(leftRGBAColors, rgbColor);
|
|
await getEyeColorPredictionResult(rightRGBAColors, rgbColor);
|
|
}),
|
|
);
|
|
} catch (err) {
|
|
console.error('Unable to crop the images::', err);
|
|
return false;
|
|
}
|
|
|
|
calculatedThreshold =
|
|
predictedColorResults.filter(element => element).length /
|
|
predictedColorResults.length;
|
|
|
|
const matches = rxDataURI.exec(vcImage).groups;
|
|
const vcFace = matches.data;
|
|
|
|
faceCompareOuptut = await faceCompare(vcFace, capturedImage.base64);
|
|
|
|
if (blinkCounter > 0) {
|
|
calculatedThreshold = calculatedThreshold + blinkConfidenceScore;
|
|
}
|
|
|
|
if (calculatedThreshold > LIVENESS_THRESHOLD && faceCompareOuptut) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export interface FaceDetectorConfig {
|
|
mode: FaceDetector.FaceDetectorMode;
|
|
detectLandmarks: FaceDetector.FaceDetectorLandmarks;
|
|
runClassifications: FaceDetector.FaceDetectorClassifications;
|
|
contourMode: FaceDetector.FaceDetectorClassifications;
|
|
minDetectionInterval: number;
|
|
tracking: boolean;
|
|
}
|