一个使用 Google Mobile Vision 来检测图片上的人脸的库。
已弃用:从 SDK 51 起,该库将不再可用。如果你需要此功能,我们推荐react-native-vision-camera
。
expo-face-detector
可让你利用 谷歌的机器学习套件 框架的强大功能来检测图片上的人脸。
¥expo-face-detector
lets you use the power of the Google's ML Kit framework to detect faces on images.
¥Known issues
面部检测器无法识别未与界面对齐的面部(界面顶部与头顶匹配)。
¥Face detector does not recognize faces that aren't aligned with the interface (top of the interface matches top of the head).
¥Installation
该模块在 Expo Go 应用 中不可用,因为它具有破坏 iOS 模拟器构建的依赖。
¥This module is not available in the Expo Go app because it has dependencies that break builds for iOS Simulators.
你可以创建 开发构建 来使用此包。
¥You can create a development build to work with this package.
-
npx expo install expo-face-detector
If you are installing this in an existing React Native app (bare workflow), start by installing expo
in your project. Then, follow the additional instructions as mentioned by library's README under "Installation in bare React Native projects" section.
¥Usage
¥Settings
为了配置检测器的行为,模块传递一个 DetectionOptions
对象,然后由该模块解释该对象。
¥To configure detector's behavior modules pass a DetectionOptions
object which is then interpreted by this module.
¥Example
你可以使用以下代码片段以快速模式检测人脸,而无需检测地标或人脸是否在微笑。
¥You can use the following snippet to detect faces in a fast mode without detecting landmarks or whether a face is smiling.
import * as React from 'react';
import { Camera } from 'expo-camera';
import * as FaceDetector from 'expo-face-detector';
const App = () => (
<Camera
// other props
onFacesDetected={handleFacesDetected}
faceDetectorSettings={{
mode: FaceDetector.FaceDetectorMode.fast,
detectLandmarks: FaceDetector.FaceDetectorLandmarks.none,
runClassifications: FaceDetector.FaceDetectorClassifications.none,
minDetectionInterval: 100,
tracking: true,
}}
/>
);
const handleFacesDetected = ({ faces }) => {
console.log(faces);
};
export default App;
import * as FaceDetector from 'expo-face-detector';
Deprecated If you require this functionality, we recommend using react-native-vision-camera
FaceDetector.detectFacesAsync(uri, options)
Name | Type | Description |
---|---|---|
uri | string |
|
options (optional) | DetectionOptions | A map of detection options. Default: {} |
Detect faces on a picture.
Returns a Promise which fulfils with DetectionResult
object.
DetectionOptions
In order to configure detector's behavior modules pass a settings object which is then interpreted by this module.
Name | Type | Description |
---|---|---|
detectLandmarks (optional) | FaceDetectorLandmarks | Whether to detect and return landmarks positions on the face (ears, eyes, mouth, cheeks, nose).
Use |
minDetectionInterval (optional) | number | Minimal interval in milliseconds between two face detection events being submitted to JS. Use, when you expect lots of faces for long time and are afraid of JS Bridge being overloaded. Default: 0 |
mode (optional) | FaceDetectorMode | Whether to detect faces in fast or accurate mode. Use |
runClassifications (optional) | FaceDetectorClassifications | Whether to run additional classifications on detected faces (smiling probability, open eye
probabilities). Use |
tracking (optional) | boolean | Flag to enable tracking of faces between frames. If true, each face will be returned with
Default: false |
DetectionResult
Name | Type | Description |
---|---|---|
faces | FaceFeature[] | Array of faces objects. |
image | Image | - |
FaceFeature
Name | Type | Description |
---|---|---|
bottomMouthPosition (optional) | Point | Position of the bottom edge of the mouth in image coordinates. Returned only if detection
classifications property is set to |
bounds | FaceFeatureBounds | An object containing face bounds. |
faceID (optional) | number | A face identifier (used for tracking, if the same face appears on consecutive frames it will
have the same |
leftCheekPosition (optional) | Point | Position of the left cheek in image coordinates. Returned only if detection classifications
property is set to |
leftEarPosition (optional) | Point | Position of the left ear in image coordinates. Returned only if detection classifications
property is set to |
leftEyeOpenProbability (optional) | number | Probability that the left eye is open. Returned only if detection classifications property is
set to |
leftEyePosition (optional) | Point | Position of the left eye in image coordinates. Returned only if detection classifications
property is set to |
leftMouthPosition (optional) | Point | Position of the left edge of the mouth in image coordinates. Returned only if detection
classifications property is set to |
mouthPosition (optional) | Point | Position of the center of the mouth in image coordinates. Returned only if detection
classifications property is set to |
noseBasePosition (optional) | Point | Position of the nose base in image coordinates. Returned only if detection classifications
property is set to |
rightCheekPosition (optional) | Point | Position of the right cheek in image coordinates. Returned only if detection classifications
property is set to |
rightEarPosition (optional) | Point | Position of the right ear in image coordinates. Returned only if detection classifications
property is set to |
rightEyeOpenProbability (optional) | number | Probability that the right eye is open. Returned only if detection classifications property is
set to |
rightEyePosition (optional) | Point | Position of the right eye in image coordinates. Returned only if detection classifications
property is set to |
rightMouthPosition (optional) | Point | Position of the right edge of the mouth in image coordinates. Returned only if detection
classifications property is set to |
rollAngle (optional) | number | Roll angle of the face (bank). |
smilingProbability (optional) | number | Probability that the face is smiling. Returned only if detection classifications property is
set to |
yawAngle (optional) | number | Yaw angle of the face (heading, turning head left or right). |
FaceFeatureBounds
Name | Type | Description |
---|---|---|
origin | Point | Position of the top left corner of a square containing the face in image coordinates, |
size | {
height: number,
width: number
} | Size of the square containing the face in image coordinates, |
Image
Name | Type | Description |
---|---|---|
height | number | Height of the image in pixels. |
orientation | number | Orientation of the image (value conforms to the EXIF orientation tag standard). |
uri | string | URI of the image. |
width | number | Width of the image in pixels. |
Point
Name | Type | Description |
---|---|---|
x | number | - |
y | number | - |
FaceDetectorClassifications
FaceDetectorClassifications Values
none
FaceDetectorClassifications.none = 1
all
FaceDetectorClassifications.all = 2
FaceDetectorLandmarks
FaceDetectorLandmarks Values
none
FaceDetectorLandmarks.none = 1
all
FaceDetectorLandmarks.all = 2
FaceDetectorMode
FaceDetectorMode Values
fast
FaceDetectorMode.fast = 1
accurate
FaceDetectorMode.accurate = 2