To integrate real-time face recognition in a React Native Expo app, you can follow these steps using the react-native-faces library. Ensure you have the Expo SDK installed and have the required permissions enabled in your app.
Install the library:
bash Copy code
npm install react-native-faces
Link the library:
bash Copy code
npm link react-native-faces
In your android/app/build.gradle:
gradle Copy code
dependencies {
implementation project(':react-native-faces')
}
In ios/Info.plist:
Add the following keys for FaceID and Face Recognition:
xml Copy code
<key>NSFaceIDUsageDescription</key>
<string>YourUsageDescriptionHere</string>
<key>NSFace Recognition Usage Description</key>
<string>YourUsageDescriptionHere</string>
Create a component for face recognition:
javascript Copy code
import * as Face from 'react-native-faces';
const FaceRecognition = () => {
const [isFacing, setIsFacing] = useState(false);
const [detectedFace, setDetectedFace] = useState(null);
const startFaceRecognition = async () => {
// Request permissions for camera and FaceID (if needed)
await Expo Permissions.requestAsync(['camera', 'android.permission.CAMERA']);
// Enable camera and start monitoring for face detection
const cameraRef = await Face.useCamera();
if (cameraRef) {
setIsFacing(true);
cameraRef.takePictureAsync((image) => {
Face.detectFace(image)
.then((faces) => {
if (faces.length > 0) {
setDetectedFace(faces[0]);
}
})
.finally(() => setIsFacing(false));
});
}
};
const stopFaceRecognition = () => {
Face.stopCamera();
setIsFacing(false);
};
return (
<View>
{isFacing && (
<Button title="Stop" onPress={stopFaceRecognition} />
{detectedFace && (
<View>
<Text>Detected Face: {detectedFace.name}</Text>
<Image source={{ uri: detectedFace.imageURI }} style={{ width: 200, height: 200 }} />
</View>
)}
<Button title="Start" onPress={startFaceRecognition} />
)}
</View>
);
};
Use the component in your main App component:
javascript Copy code
import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import FaceRecognition from './FaceRecognition';
const App = () => {
return (
<View style={styles.container}>
<FaceRecognition />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
});
export default App;
Note: This example assumes you have the necessary permissions set up. For a complete solution, you should handle permission requests and error handling properly. Also, keep in mind that real-time face recognition might require more processing power and might not be supported on all devices.