ML Kit 提供經過最佳化的自拍區隔 SDK,Selfie Segmenter 資產會在建構時靜態連結至您的應用程式。這會使應用程式大小增加最多 24 MB,且 API 延遲時間可能介於約 7 毫秒到約 12 毫秒之間 (視輸入圖片大小而定,以 iPhone X 測得)。
立即試用
- 請試用範例應用程式,瞭解這個 API 的使用範例。
事前準備
在 Podfile 中加入下列 ML Kit 程式庫:
pod 'GoogleMLKit/SegmentationSelfie', '8.0.0'
安裝或更新專案的 Pod 後,請使用 .
xcworkspace
開啟 Xcode 專案。Xcode 13.2.1 以上版本支援 ML Kit。
1. 建立 Segmenter 的執行個體
如要對自拍圖片執行區隔作業,請先建立 Segmenter
的執行個體 (使用 SelfieSegmenterOptions
),並視需要指定區隔設定。
區隔器選項
區隔器模式
Segmenter
有兩種運作模式。請務必選擇符合您用途的範本。
STREAM_MODE (default)
這個模式是專為串流影片或相機的影格而設計。在此模式下,分割器會利用前幾格的結果,傳回更平滑的分割結果。
SINGLE_IMAGE_MODE (default)
這個模式適用於不相關的單張圖片。在此模式下,分割器會獨立處理每張圖片,不會對影格進行平滑處理。
啟用原始大小遮罩
要求分割器傳回符合模型輸出大小的原始大小遮罩。
原始遮罩大小 (例如 256x256) 通常小於輸入圖片大小。
如未指定這個選項,分割器會重新調整原始遮罩大小,以符合輸入圖片大小。如要套用自訂重新調整比例邏輯,或您的用途不需要重新調整比例,請考慮使用這個選項。
指定區隔器選項:
Swift
let options = SelfieSegmenterOptions() options.segmenterMode = .singleImage options.shouldEnableRawSizeMask = true
Objective-C
MLKSelfieSegmenterOptions *options = [[MLKSelfieSegmenterOptions alloc] init]; options.segmenterMode = MLKSegmenterModeSingleImage; options.shouldEnableRawSizeMask = YES;
最後,取得 Segmenter
的執行個體。傳送您指定的選項:
Swift
let segmenter = Segmenter.segmenter(options: options)
Objective-C
MLKSegmenter *segmenter = [MLKSegmenter segmenterWithOptions:options];
2. 準備輸入圖片
如要分割自拍影像,請針對每張圖片或每個影片影格執行下列操作:如果啟用串流模式,就必須從 CMSampleBuffer
建立 VisionImage
物件。
使用 UIImage
或 CMSampleBuffer
建立 VisionImage
物件。
如果你使用 UIImage
,請按照下列步驟操作:
- 使用
UIImage
建立VisionImage
物件。請務必指定正確的.orientation
。Swift
let image = VisionImage(image: UIImage) visionImage.orientation = image.imageOrientation
Objective-C
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image]; visionImage.orientation = image.imageOrientation;
如果你使用 CMSampleBuffer
,請按照下列步驟操作:
-
指定
CMSampleBuffer
中所含圖片資料的方向。如要取得圖片方向,請按照下列步驟操作:
Swift
func imageOrientation( deviceOrientation: UIDeviceOrientation, cameraPosition: AVCaptureDevice.Position ) -> UIImage.Orientation { switch deviceOrientation { case .portrait: return cameraPosition == .front ? .leftMirrored : .right case .landscapeLeft: return cameraPosition == .front ? .downMirrored : .up case .portraitUpsideDown: return cameraPosition == .front ? .rightMirrored : .left case .landscapeRight: return cameraPosition == .front ? .upMirrored : .down case .faceDown, .faceUp, .unknown: return .up } }
Objective-C
- (UIImageOrientation) imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation cameraPosition:(AVCaptureDevicePosition)cameraPosition { switch (deviceOrientation) { case UIDeviceOrientationPortrait: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored : UIImageOrientationRight; case UIDeviceOrientationLandscapeLeft: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored : UIImageOrientationUp; case UIDeviceOrientationPortraitUpsideDown: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored : UIImageOrientationLeft; case UIDeviceOrientationLandscapeRight: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored : UIImageOrientationDown; case UIDeviceOrientationUnknown: case UIDeviceOrientationFaceUp: case UIDeviceOrientationFaceDown: return UIImageOrientationUp; } }
- 使用
CMSampleBuffer
物件和方向建立VisionImage
物件:Swift
let image = VisionImage(buffer: sampleBuffer) image.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition)
Objective-C
MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer]; image.orientation = [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation cameraPosition:cameraPosition];
3. 處理圖片
將 VisionImage
物件傳遞至 Segmenter
的其中一個圖片處理方法。您可以使用非同步 process(image:)
方法或同步 results(in:)
方法。
如要對自拍照執行同步區隔作業,請按照下列步驟操作:
Swift
var mask: [SegmentationMask] do { mask = try segmenter.results(in: image) } catch let error { print("Failed to perform segmentation with error: \(error.localizedDescription).") return } // Success. Get a segmentation mask here.
Objective-C
NSError *error; MLKSegmentationMask *mask = [segmenter resultsInImage:image error:&error]; if (error != nil) { // Error. return; } // Success. Get a segmentation mask here.
如要以非同步方式對自拍照執行分割作業,請按照下列步驟操作:
Swift
segmenter.process(image) { mask, error in guard error == nil else { // Error. return } // Success. Get a segmentation mask here.
Objective-C
[segmenter processImage:image completion:^(MLKSegmentationMask * _Nullable mask, NSError * _Nullable error) { if (error != nil) { // Error. return; } // Success. Get a segmentation mask here. }];
4. 取得區隔遮罩
您可以按照下列方式取得區隔結果:
Swift
let maskWidth = CVPixelBufferGetWidth(mask.buffer) let maskHeight = CVPixelBufferGetHeight(mask.buffer) CVPixelBufferLockBaseAddress(mask.buffer, CVPixelBufferLockFlags.readOnly) let maskBytesPerRow = CVPixelBufferGetBytesPerRow(mask.buffer) var maskAddress = CVPixelBufferGetBaseAddress(mask.buffer)!.bindMemory( to: Float32.self, capacity: maskBytesPerRow * maskHeight) for _ in 0...(maskHeight - 1) { for col in 0...(maskWidth - 1) { // Gets the confidence of the pixel in the mask being in the foreground. let foregroundConfidence: Float32 = maskAddress[col] } maskAddress += maskBytesPerRow / MemoryLayout<Float32>.size }
Objective-C
size_t width = CVPixelBufferGetWidth(mask.buffer); size_t height = CVPixelBufferGetHeight(mask.buffer); CVPixelBufferLockBaseAddress(mask.buffer, kCVPixelBufferLock_ReadOnly); size_t maskBytesPerRow = CVPixelBufferGetBytesPerRow(mask.buffer); float *maskAddress = (float *)CVPixelBufferGetBaseAddress(mask.buffer); for (int row = 0; row < height; ++row) { for (int col = 0; col < width; ++col) { // Gets the confidence of the pixel in the mask being in the foreground. float foregroundConfidence = maskAddress[col]; } maskAddress += maskBytesPerRow / sizeof(float); }
如需如何使用區隔結果的完整範例,請參閱 ML Kit 快速入門範例。
提升成效的訣竅
結果品質取決於輸入圖片的品質:
- 如要讓 ML Kit 取得準確的區隔結果,圖片必須至少為 256x256 像素。
- 如果您在即時應用程式中執行自拍分割作業,可能也需要考量輸入圖片的整體尺寸。較小的圖片處理速度較快,因此如要縮短延遲時間,請以較低的解析度拍攝圖片,但請注意上述解析度規定,並確保主體盡可能占滿圖片。
- 圖片對焦不佳也會影響準確度。如果結果不符合要求,請要求使用者重新拍攝圖片。
如要在即時應用程式中使用區隔功能,請按照下列指南操作,以達到最佳影格速率:
- 使用
stream
分段器模式。 - 建議您以較低的解析度拍攝圖片。但請注意,這個 API 也有圖片尺寸規定。
- 如要處理影片影格,請使用區隔器的
results(in:)
同步 API。從 AVCaptureVideoDataOutputSampleBufferDelegate 的 captureOutput(_, didOutput:from:) 函式呼叫這個方法,即可從指定的視訊影格同步取得結果。將 AVCaptureVideoDataOutput 的 alwaysDiscardsLateVideoFrames 設為 true,即可節流對區隔器的呼叫。如果區隔器執行時有新的視訊影格可用,系統會捨棄該影格。 - 如果您使用區隔器輸出內容,在輸入圖片上疊加圖像,請先從 ML Kit 取得結果,然後在單一步驟中算繪圖片並疊加圖像。這樣一來,每個處理過的輸入影格只會轉譯到顯示表面一次。如需範例,請參閱 ML Kit 快速入門範例中的 previewOverlayView 和 CameraViewController 類別。