Detect Faces with ML Kit on iOS Stay organized with collections Save and categorize content based on your preferences.
This page describes an old version of the Face Detection API, which was part of ML Kit for Firebase. Development of this API has been moved to the standalone ML Kit SDK, which you can use with or without Firebase.Learn more.
See Detect faces with ML Kit on iOS for the latest documentation.
You can use ML Kit to detect faces in images and video.
Before you begin
- If you have not already added Firebase to your app, do so by following the steps in thegetting started guide.
- Include the ML Kit libraries in your Podfile:
After you install or update your project's Pods, be sure to open your Xcode project using itspod 'Firebase/MLVision', '6.25.0'# If you want to detect face contours (landmark detection and classification# don't require this additional model):pod 'Firebase/MLVisionFaceModel', '6.25.0'
.xcworkspace. - In your app, import Firebase:
Swift
importFirebase
Objective-C
@importFirebase;
Input image guidelines
For ML Kit to accurately detect faces, input images must contain faces that are represented by sufficient pixel data. In general, each face you want to detect in an image should be at least 100x100 pixels. If you want to detect the contours of faces, ML Kit requires higher resolution input: each face should be at least 200x200 pixels.
If you are detecting faces in a real-time application, you might also want to consider the overall dimensions of the input images. Smaller images can be processed faster, so to reduce latency, capture images at lower resolutions (keeping in mind the above accuracy requirements) and ensure that the subject's face occupies as much of the image as possible. Also seeTips to improve real-time performance.
Poor image focus can hurt accuracy. If you aren't getting acceptable results, try asking the user to recapture the image.
The orientation of a face relative to the camera can also affect what facial features ML Kit detects. SeeFace Detection Concepts.
1. Configure the face detector
Before you apply face detection to an image, if you want to change any of theface detector's default settings, specify those settings with aVisionFaceDetectorOptions object. You can changethe following settings:| Settings | |
|---|---|
performanceMode | fast (default) |accurateFavor speed or accuracy when detecting faces. |
landmarkMode | none (default) |allWhether to attempt to detect the facial "landmarks"—eyes, ears, nose, cheeks, mouth—of all detected faces. |
contourMode | none (default) |allWhether to detect the contours of facial features. Contours are detected for only the most prominent face in an image. |
classificationMode | none (default) |allWhether or not to classify faces into categories such as "smiling", and "eyes open". |
minFaceSize | CGFloat (default:0.1)The minimum size, relative to the image, of faces to detect. |
isTrackingEnabled | false (default) |trueWhether 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. |
For example, build aVisionFaceDetectorOptionsobject like one of the following examples:
Swift
// High-accuracy landmark detection and face classificationletoptions=VisionFaceDetectorOptions()options.performanceMode=.accurateoptions.landmarkMode=.alloptions.classificationMode=.all// Real-time contour detection of multiple facesletoptions=VisionFaceDetectorOptions()options.contourMode=.all
Objective-C
// High-accuracy landmark detection and face classificationFIRVisionFaceDetectorOptions*options=[[FIRVisionFaceDetectorOptionsalloc]init];options.performanceMode=FIRVisionFaceDetectorPerformanceModeAccurate;options.landmarkMode=FIRVisionFaceDetectorLandmarkModeAll;options.classificationMode=FIRVisionFaceDetectorClassificationModeAll;// Real-time contour detection of multiple facesFIRVisionFaceDetectorOptions*options=[[FIRVisionFaceDetectorOptionsalloc]init];options.contourMode=FIRVisionFaceDetectorContourModeAll;
2. Run the face detector
To detect faces in an image, pass the image as aUIImage or aCMSampleBufferRef to theVisionFaceDetector'sdetect(in:)method:- Get an instance of
VisionFaceDetector:Swift
lazyvarvision=Vision.vision()letfaceDetector=vision.faceDetector(options:options)
Objective-C
FIRVision*vision=[FIRVisionvision];FIRVisionFaceDetector*faceDetector=[visionfaceDetector];// Or, to change the default settings:// FIRVisionFaceDetector *faceDetector =// [vision faceDetectorWithOptions:options];
Create a
VisionImageobject using aUIImageor aCMSampleBufferRef.To use a
UIImage:- If necessary, rotate the image so that its
imageOrientationproperty is.up. - Create a
VisionImageobject using the correctly-rotatedUIImage. Do not specify any rotation metadata—the default value,.topLeft, must be used.Swift
letimage=VisionImage(image:uiImage)
Objective-C
FIRVisionImage*image=[[FIRVisionImagealloc]initWithImage:uiImage];
To use a
CMSampleBufferRef:Create a
VisionImageMetadataobject that specifies the orientation of the image data contained in theCMSampleBufferRefbuffer.To get the image orientation:
Swift
funcimageOrientation(deviceOrientation:UIDeviceOrientation,cameraPosition:AVCaptureDevice.Position)->VisionDetectorImageOrientation{switchdeviceOrientation{case.portrait:returncameraPosition==.front?.leftTop:.rightTopcase.landscapeLeft:returncameraPosition==.front?.bottomLeft:.topLeftcase.portraitUpsideDown:returncameraPosition==.front?.rightBottom:.leftBottomcase.landscapeRight:returncameraPosition==.front?.topRight:.bottomRightcase.faceDown,.faceUp,.unknown:return.leftTop}}
Objective-C
-(FIRVisionDetectorImageOrientation)imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientationcameraPosition:(AVCaptureDevicePosition)cameraPosition{switch(deviceOrientation){caseUIDeviceOrientationPortrait:if(cameraPosition==AVCaptureDevicePositionFront){returnFIRVisionDetectorImageOrientationLeftTop;}else{returnFIRVisionDetectorImageOrientationRightTop;}caseUIDeviceOrientationLandscapeLeft:if(cameraPosition==AVCaptureDevicePositionFront){returnFIRVisionDetectorImageOrientationBottomLeft;}else{returnFIRVisionDetectorImageOrientationTopLeft;}caseUIDeviceOrientationPortraitUpsideDown:if(cameraPosition==AVCaptureDevicePositionFront){returnFIRVisionDetectorImageOrientationRightBottom;}else{returnFIRVisionDetectorImageOrientationLeftBottom;}caseUIDeviceOrientationLandscapeRight:if(cameraPosition==AVCaptureDevicePositionFront){returnFIRVisionDetectorImageOrientationTopRight;}else{returnFIRVisionDetectorImageOrientationBottomRight;}default:returnFIRVisionDetectorImageOrientationTopLeft;}}
Then, create the metadata object:
Swift
letcameraPosition=AVCaptureDevice.Position.back// Set to the capture device you used.letmetadata=VisionImageMetadata()metadata.orientation=imageOrientation(deviceOrientation:UIDevice.current.orientation,cameraPosition:cameraPosition)
Objective-C
FIRVisionImageMetadata*metadata=[[FIRVisionImageMetadataalloc]init];AVCaptureDevicePositioncameraPosition=AVCaptureDevicePositionBack;// Set to the capture device you used.metadata.orientation=[selfimageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientationcameraPosition:cameraPosition];
- Create a
VisionImageobject using theCMSampleBufferRefobject and the rotation metadata:Swift
letimage=VisionImage(buffer:sampleBuffer)image.metadata=metadata
Objective-C
FIRVisionImage*image=[[FIRVisionImagealloc]initWithBuffer:sampleBuffer];image.metadata=metadata;
- If necessary, rotate the image so that its
- Then, pass the image to the
detect(in:)method:Swift
faceDetector.process(visionImage){faces,erroringuarderror==nil,letfaces=faces,!faces.isEmptyelse{// ...return}// Faces detected// ...}
Objective-C
[faceDetectordetectInImage:imagecompletion:^(NSArray<FIRVisionFace*>*faces,NSError*error){if(error!=nil){return;}elseif(faces!=nil){// Recognized faces}}];
3. Get information about detected faces
If the face detection operation succeeds, the face detector passes an arrayofVisionFace objects to the completion handler. EachVisionFace object represents a face that was detected in the image. Foreach face, you can get its bounding coordinates in the input image, as well asany other information you configured the face detector to find. For example:Swift
forfaceinfaces{letframe=face.frameifface.hasHeadEulerAngleY{letrotY=face.headEulerAngleY// Head is rotated to the right rotY degrees}ifface.hasHeadEulerAngleZ{letrotZ=face.headEulerAngleZ// Head is rotated upward rotZ degrees}// If landmark detection was enabled (mouth, ears, eyes, cheeks, and// nose available):ifletleftEye=face.landmark(ofType:.leftEye){letleftEyePosition=leftEye.position}// If contour detection was enabled:ifletleftEyeContour=face.contour(ofType:.leftEye){letleftEyePoints=leftEyeContour.points}ifletupperLipBottomContour=face.contour(ofType:.upperLipBottom){letupperLipBottomPoints=upperLipBottomContour.points}// If classification was enabled:ifface.hasSmilingProbability{letsmileProb=face.smilingProbability}ifface.hasRightEyeOpenProbability{letrightEyeOpenProb=face.rightEyeOpenProbability}// If face tracking was enabled:ifface.hasTrackingID{lettrackingId=face.trackingID}}
Objective-C
for(FIRVisionFace*faceinfaces){// Boundaries of face in imageCGRectframe=face.frame;if(face.hasHeadEulerAngleY){CGFloatrotY=face.headEulerAngleY;// Head is rotated to the right rotY degrees}if(face.hasHeadEulerAngleZ){CGFloatrotZ=face.headEulerAngleZ;// Head is tilted sideways rotZ degrees}// If landmark detection was enabled (mouth, ears, eyes, cheeks, and// nose available):FIRVisionFaceLandmark*leftEar=[facelandmarkOfType:FIRFaceLandmarkTypeLeftEar];if(leftEar!=nil){FIRVisionPoint*leftEarPosition=leftEar.position;}// If contour detection was enabled:FIRVisionFaceContour*upperLipBottomContour=[facecontourOfType:FIRFaceContourTypeUpperLipBottom];if(upperLipBottomContour!=nil){NSArray<FIRVisionPoint*>*upperLipBottomPoints=upperLipBottomContour.points;if(upperLipBottomPoints.count>0){NSLog("Detected the bottom contour of the subject's upper lip.")}}// If classification was enabled:if(face.hasSmilingProbability){CGFloatsmileProb=face.smilingProbability;}if(face.hasRightEyeOpenProbability){CGFloatrightEyeOpenProb=face.rightEyeOpenProbability;}// If face tracking was enabled:if(face.hasTrackingID){NSIntegertrackingID=face.trackingID;}}
Example of face contours
When you have face contour detection enabled, you get a list of points for each facial feature that was detected. These points represent the shape of the feature. See theFace Detection Concepts Overview for details about how contours are represented.
The following image illustrates how these points map to a face (click the image to enlarge):
Real-time face detection
If you want to use face detection in a real-time application, follow theseguidelines to achieve the best framerates:
Configure the face detector to use eitherface contour detection or classification and landmark detection, but not both:
Contour detection
Landmark detection
Classification
Landmark detection and classification
Contour detection and landmark detection
Contour detection and classification
Contour detection, landmark detection, and classificationEnable
fastmode (enabled by default).Consider capturing images at a lower resolution. However, also keep in mindthis API's image dimension requirements.
- Throttle calls to the detector. If a new video frame becomes available while the detector is running, drop the frame.
- If you are using the output of the detector to overlay graphics on the input image, first get the result from ML Kit, then render the image and overlay in a single step. By doing so, you render to the display surface only once for each input frame. See thepreviewOverlayView andFIRDetectionOverlayView classes in the showcase sample app for an example.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-01-21 UTC.