ML Kit 提供自拍區隔的最佳化 SDK。Selfie Segmenter 素材資源會在建構期間與應用程式建立靜態連結。這會使應用程式大小增加至最多 24 MB,而 API 延遲時間則會因輸入圖片大小而異,在 iPhone X 上測得的延遲時間約為 7 毫秒至 12 毫秒。
立即試用
- 請試用範例應用程式,瞭解這個 API 的使用範例。
事前準備
在 Podfile 中加入下列 ML Kit 程式庫:
pod 'GoogleMLKit/SegmentationSelfie', '7.0.0'
安裝或更新專案的 Pod 後,請使用 .
xcworkspace
開啟 Xcode 專案。ML Kit 支援 Xcode 13.2.1 以上版本。
1. 建立 Segmenter 例項
如要對自拍圖片執行區隔,請先使用 SelfieSegmenterOptions
建立 Segmenter
的例項,並視需要指定區隔設定。
區隔器選項
區隔器模式
Segmenter
有兩種運作模式。請務必選擇符合用途的選項。
STREAM_MODE (default)
這個模式專門用於從影片或相機串流影格。在這個模式中,分割器會利用先前影格中的結果,傳回更流暢的分割結果。
SINGLE_IMAGE_MODE (default)
這個模式專為不相關的單張圖片而設計。在這個模式下,分割器會分別處理每張圖片,不會在影格上進行平滑處理。
啟用原始大小遮罩
要求分割器傳回與模型輸出大小相符的原始大小遮罩。
原始遮罩大小 (例如 256x256) 通常會小於輸入圖片大小。
如果未指定這個選項,分割器會重新調整原始遮罩的比例,以符合輸入圖片的大小。如果您想套用自訂的重新調整大小邏輯,或是您的用途不需要重新調整大小,建議您使用這個選項。
指定分割器選項:
let options = SelfieSegmenterOptions() options.segmenterMode = .singleImage options.shouldEnableRawSizeMask = true
MLKSelfieSegmenterOptions *options = [[MLKSelfieSegmenterOptions alloc] init]; options.segmenterMode = MLKSegmenterModeSingleImage; options.shouldEnableRawSizeMask = YES;
最後,取得 Segmenter
的例項。傳遞您指定的選項:
let segmenter = Segmenter.segmenter(options: options)
MLKSegmenter *segmenter = [MLKSegmenter segmenterWithOptions:options];
2. 準備輸入圖片
如要區隔自拍照,請針對每張圖片或影片影格執行下列操作。如果您已啟用串流模式,就必須從 CMSampleBuffer
建立 VisionImage
物件。
使用 UIImage
或 CMSampleBuffer
建立 VisionImage
物件。
如果您使用 UIImage
,請按照下列步驟操作:
- 使用
UIImage
建立VisionImage
物件。請務必指定正確的.orientation
。let image = VisionImage(image: UIImage) visionImage.orientation = image.imageOrientation
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image]; visionImage.orientation = image.imageOrientation;
如果您使用 CMSampleBuffer
,請按照下列步驟操作:
-
指定
CMSampleBuffer
中所含圖片資料的方向。如要取得圖片方向,請按照下列步驟操作:
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 } }
- (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
物件:let image = VisionImage(buffer: sampleBuffer) image.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition)
MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer]; image.orientation = [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation cameraPosition:cameraPosition];
3. 處理圖片
將 VisionImage
物件傳遞至 Segmenter
的其中一個圖片處理方法。您可以使用非同步的 process(image:)
方法,也可以使用同步的 results(in:)
方法。
如要同步對自拍圖片執行區隔作業,請按照下列步驟操作:
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.
NSError *error; MLKSegmentationMask *mask = [segmenter resultsInImage:image error:&error]; if (error != nil) { // Error. return; } // Success. Get a segmentation mask here.
如要以非同步方式對自拍照圖片執行區隔作業,請按照下列步驟操作:
segmenter.process(image) { mask, error in guard error == nil else { // Error. return } // Success. Get a segmentation mask here.
[segmenter processImage:image completion:^(MLKSegmentationMask * _Nullable mask, NSError * _Nullable error) { if (error != nil) { // Error. return; } // Success. Get a segmentation mask here. }];
4. 取得區隔遮罩
您可以透過下列方式取得區隔結果:
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 }
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 取得準確的分割結果,圖片至少應為 256 x 256 像素。
- 如果您在即時應用程式中執行自拍區隔作業,也許也要考量輸入圖片的整體尺寸。較小的圖片可加快處理速度,因此為了減少延遲時間,請以較低解析度拍攝圖片,但請注意上述解析度規定,並確保拍攝主體盡可能佔據整個畫面。
- 圖片對焦不佳也會影響準確度。如果您無法取得可接受的結果,請要求使用者重新拍攝圖片。
如果您想在即時應用程式中使用區隔功能,請按照下列指南操作,以獲得最佳的幀率:
- 使用
stream
區隔器模式。 - 建議您以較低解析度拍攝相片。不過,請注意這個 API 的圖片尺寸規定。
- 如要處理影片影格,請使用分割器的
results(in:)
同步 API。從 AVCaptureVideoDataOutputSampleBufferDelegate 的 captureOutput(_, didOutput:from:) 函式呼叫這個方法,即可同步取得指定影像影格中的結果。請將 AVCaptureVideoDataOutput 的 alwaysDiscardsLateVideoFrames 設為 true,以便限制對分割器的呼叫。如果在分割器執行期間有新的影片影格可用,系統就會捨棄該影格。 - 如果您使用分割器的輸出內容,在輸入圖片上疊加圖形,請先從 ML Kit 取得結果,然後在單一步驟中算繪圖片和疊加圖形。這樣一來,您只需為每個已處理的輸入影格轉譯一次顯示介面。如需範例,請參閱 ML Kit 快速入門範例中的 previewOverlayView 和 CameraViewController 類別。