Gathering detailed insights and metrics for react-native-vision-camera-face-detector
Gathering detailed insights and metrics for react-native-vision-camera-face-detector
Gathering detailed insights and metrics for react-native-vision-camera-face-detector
Gathering detailed insights and metrics for react-native-vision-camera-face-detector
Vision Camera Frame Processor Plugin to detect faces using MLKit Face Detector
npm install react-native-vision-camera-face-detector
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
131 Stars
240 Commits
22 Forks
3 Watching
3 Branches
4 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
TypeScript (39.93%)
Kotlin (26.67%)
Swift (21.29%)
JavaScript (8.26%)
Objective-C (2.25%)
Ruby (1.61%)
Cumulative downloads
Total Downloads
Last day
25.9%
680
Compared to previous day
Last week
27%
3,572
Compared to previous week
Last month
59.9%
13,191
Compared to previous month
Last year
0%
42,912
Compared to previous year
3
react-native-vision-camera-face-detector
is a React Native library that integrates with the Vision Camera module to provide face detection functionality. It allows you to easily detect faces in real-time using device's front and back camera.
If you like this package please give it a ⭐ on GitHub.
1yarn add react-native-vision-camera-face-detector
Then you need to add react-native-worklets-core
plugin to babel.config.js
. More details here.
Recommended way:
1import { 2 StyleSheet, 3 Text, 4 View 5} from 'react-native' 6import { 7 useEffect, 8 useState, 9 useRef 10} from 'react' 11import { 12 Frame, 13 useCameraDevice 14} from 'react-native-vision-camera' 15import { 16 Face, 17 Camera, 18 FaceDetectionOptions 19} from 'react-native-vision-camera-face-detector' 20 21export default function App() { 22 const faceDetectionOptions = useRef<FaceDetectionOptions>( { 23 // detection options 24 } ).current 25 26 const device = useCameraDevice('front') 27 28 useEffect(() => { 29 (async () => { 30 const status = await Camera.requestCameraPermission() 31 console.log({ status }) 32 })() 33 }, [device]) 34 35 function handleFacesDetection( 36 faces: Face[], 37 frame: Frame 38 ) { 39 console.log( 40 'faces', faces.length, 41 'frame', frame.toString() 42 ) 43 } 44 45 return ( 46 <View style={{ flex: 1 }}> 47 {!!device? <Camera 48 style={StyleSheet.absoluteFill} 49 device={device} 50 faceDetectionCallback={ handleFacesDetection } 51 faceDetectionOptions={ faceDetectionOptions } 52 /> : <Text> 53 No Device 54 </Text>} 55 </View> 56 ) 57}
Or use it following vision-camera docs:
1import { 2 StyleSheet, 3 Text, 4 View 5} from 'react-native' 6import { 7 useEffect, 8 useState, 9 useRef 10} from 'react' 11import { 12 Camera, 13 useCameraDevice, 14 useFrameProcessor 15} from 'react-native-vision-camera' 16import { 17 Face, 18 runAsync, 19 useFaceDetector, 20 FaceDetectionOptions 21} from 'react-native-vision-camera-face-detector' 22import { Worklets } from 'react-native-worklets-core' 23 24export default function App() { 25 const faceDetectionOptions = useRef<FaceDetectionOptions>( { 26 // detection options 27 } ).current 28 29 const device = useCameraDevice('front') 30 const { detectFaces } = useFaceDetector( faceDetectionOptions ) 31 32 useEffect(() => { 33 (async () => { 34 const status = await Camera.requestCameraPermission() 35 console.log({ status }) 36 })() 37 }, [device]) 38 39 const handleDetectedFaces = Worklets.createRunOnJS( ( 40 faces: Face[] 41 ) => { 42 console.log( 'faces detected', faces ) 43 }) 44 45 const frameProcessor = useFrameProcessor((frame) => { 46 'worklet' 47 runAsync(frame, () => { 48 'worklet' 49 const faces = detectFaces(frame) 50 // ... chain some asynchronous frame processor 51 // ... do something asynchronously with frame 52 handleDetectedFaces(faces) 53 }) 54 // ... chain frame processors 55 // ... do something with frame 56 }, [handleDetectedFaces]) 57 58 return ( 59 <View style={{ flex: 1 }}> 60 {!!device? <Camera 61 style={StyleSheet.absoluteFill} 62 device={device} 63 isActive={true} 64 frameProcessor={frameProcessor} 65 /> : <Text> 66 No Device 67 </Text>} 68 </View> 69 ) 70}
As face detection is a heavy process you should run it in an asynchronous thread so it can be finished without blocking your camera preview.
You should read vision-camera
docs about this feature.
Option | Description | Default |
---|---|---|
performanceMode | Favor speed or accuracy when detecting faces. | fast |
landmarkMode | Whether to attempt to identify facial landmarks : eyes, ears, nose, cheeks, mouth, and so on. | none |
contourMode | Whether to detect the contours of facial features. Contours are detected for only the most prominent face in an image. | none |
classificationMode | Whether or not to classify faces into categories such as 'smiling', and 'eyes open'. | none |
minFaceSize | Sets the smallest desired face size, expressed as the ratio of the width of the head to width of the image. | 0.15 |
trackingEnabled | Whether or not to assign faces an ID, which can be used to track faces across images. Note that when contour detection is enabled, only one face is detected, so face tracking doesn't produce useful results. For this reason, and to improve detection speed, don't enable both contour detection and face tracking. | false |
autoScale | Should auto scale face bounds, contour and landmarks on native side? If this option is disabled all detection results will be relative to frame coordinates, not to screen/preview. You shouldn't use this option if you want to draw on screen using Skia Frame Processor . See this and this for more details. | false |
windowWidth | * Required if you want to use autoScale . You must handle your own logic to get screen sizes, with or without statusbar size, etc... | 1.0 |
windowHeight | * Required if you want to use autoScale . You must handle your own logic to get screen sizes, with or without statusbar size, etc... | 1.0 |
Here is a common issue when trying to use this package and how you can try to fix it:
Regular javascript function cannot be shared. Try decorating the function with the 'worklet' keyword...
:
react-native-reanimated
maybe you're missing this step.Execution failed for task ':react-native-vision-camera-face-detector:compileDebugKotlin'...
:
If you find other errors while using this package you're wellcome to open a new issue or create a PR with the fix.
This package was tested using the following:
react-native
: 0.74.3
(new arch disabled)react-native-vision-camera
: 4.5.0
react-native-worklets-core
: 1.3.3
react-native-reanimated
: 3.12.1
expo
: 51.0.17
Min O.S version:
Android
: SDK 26
(Android 8)IOS
: 14
Make sure to follow tested versions and your device is using the minimum O.S version before opening issues.
Made with ❤️ by luicfrr
No vulnerabilities found.
No security vulnerabilities found.