iOS पर एमएल किट की मदद से सेल्फ़ी को अलग-अलग सेगमेंट में बांटना

ML Kit, सेल्फ़ी के सेगमेंटेशन के लिए ऑप्टिमाइज़ किया गया SDK टूल उपलब्ध कराता है. सेल्फ़ी सेगमेंटर एसेट, बिल्ड के समय आपके ऐप्लिकेशन से स्टैटिक तौर पर लिंक होती हैं. इससे आपके ऐप्लिकेशन का साइज़ 24 एमबी तक बढ़ जाएगा. साथ ही, इनपुट इमेज के साइज़ के हिसाब से, एपीआई के इंतज़ार का समय ~7 से ~12 मिलीसेकंड तक हो सकता है. यह समय, iPhone X पर मेज़र किया गया है.

इसे आज़माएं

शुरू करने से पहले

  1. अपनी Podfile में ये ML Kit लाइब्रेरी शामिल करें:

    pod 'GoogleMLKit/SegmentationSelfie', '7.0.0'
    
  2. अपने प्रोजेक्ट के Pods इंस्टॉल या अपडेट करने के बाद, .xcworkspace का इस्तेमाल करके अपना Xcode प्रोजेक्ट खोलें. ML Kit, Xcode के 13.2.1 या इसके बाद के वर्शन पर काम करता है.

1. Segmenter का इंस्टेंस बनाना

सेल्फ़ी इमेज पर सेगमेंटेशन करने के लिए, सबसे पहले SelfieSegmenterOptions के साथ Segmenter का इंस्टेंस बनाएं. इसके बाद, सेगमेंटेशन सेटिंग तय करें.

सेगमेंटर के विकल्प

सेगमेंटर मोड

Segmenter दो मोड में काम करता है. पक्का करें कि आपने वह विकल्प चुना हो जो आपके काम के हिसाब से हो.

STREAM_MODE (default)

इस मोड को वीडियो या कैमरे से फ़्रेम स्ट्रीम करने के लिए डिज़ाइन किया गया है. इस मोड में, सेगमेंटर पिछले फ़्रेम के नतीजों का फ़ायदा उठाकर, बेहतर सेगमेंटेशन के नतीजे दिखाएगा.

SINGLE_IMAGE_MODE (default)

यह मोड, एक-दूसरे से मिलती-जुलती नहीं होने वाली इमेज के लिए डिज़ाइन किया गया है. इस मोड में, सेगमेंटर हर इमेज को अलग से प्रोसेस करेगा. साथ ही, फ़्रेम को स्मूद नहीं करेगा.

रॉ साइज़ मास्क की सुविधा चालू करना

सेगमेंटर से, मॉडल के आउटपुट साइज़ से मेल खाने वाला रॉ साइज़ मास्क दिखाने के लिए कहता है.

आम तौर पर, रॉ मास्क का साइज़ (उदाहरण के लिए, 256x256) इनपुट इमेज के साइज़ से छोटा होता है.

इस विकल्प को तय किए बिना, सेगमेंटर, इनपुट इमेज के साइज़ से मैच करने के लिए रॉ मास्क का साइज़ फिर से तय करेगा. अगर आपको अपने हिसाब से रीस्केलिंग लॉजिक लागू करना है या आपके इस्तेमाल के उदाहरण के लिए रीस्केलिंग की ज़रूरत नहीं है, तो इस विकल्प का इस्तेमाल करें.

सेगमेंट बनाने के विकल्पों की जानकारी दें:

SwiftObjective-C
let options = SelfieSegmenterOptions()
options.segmenterMode = .singleImage
options.shouldEnableRawSizeMask = true
MLKSelfieSegmenterOptions *options = [[MLKSelfieSegmenterOptions alloc] init];
options.segmenterMode = MLKSegmenterModeSingleImage;
options.shouldEnableRawSizeMask = YES;

आखिर में, Segmenter का एक इंस्टेंस पाएं. आपने जो विकल्प तय किए हैं उन्हें पास करें:

SwiftObjective-C
let segmenter = Segmenter.segmenter(options: options)
MLKSegmenter *segmenter = [MLKSegmenter segmenterWithOptions:options];

2. इनपुट इमेज तैयार करना

सेल्फ़ी को सेगमेंट में बांटने के लिए, वीडियो की हर इमेज या फ़्रेम के लिए यह तरीका अपनाएं. अगर आपने स्ट्रीम मोड चालू किया है, तो आपको CMSampleBuffer से VisionImage ऑब्जेक्ट बनाने होंगे.

UIImage या CMSampleBuffer का इस्तेमाल करके, VisionImage ऑब्जेक्ट बनाएं.

UIImage का इस्तेमाल करने के लिए, यह तरीका अपनाएं:

  • UIImage की मदद से, VisionImage ऑब्जेक्ट बनाएं. पक्का करें कि आपने सही .orientation डाला हो.
    SwiftObjective-C
    let image = VisionImage(image: UIImage)
    visionImage.orientation = image.imageOrientation
    MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
    visionImage.orientation = image.imageOrientation;

CMSampleBuffer का इस्तेमाल करने के लिए, यह तरीका अपनाएं:

  • CMSampleBuffer में मौजूद इमेज डेटा के ओरिएंटेशन की जानकारी दें.

    इमेज का ओरिएंटेशन देखने के लिए:

    SwiftObjective-C
    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 ऑब्जेक्ट बनाएं:
    SwiftObjective-C
    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:) तरीके में से किसी एक का इस्तेमाल किया जा सकता है.

सेल्फ़ी इमेज पर एक साथ अलग-अलग सेगमेंट बनाने के लिए:

SwiftObjective-C
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.

सेल्फ़ी इमेज पर अलग-अलग समय पर सेगमेंटेशन करने के लिए:

SwiftObjective-C
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. सेगमेंटेशन मास्क पाना

सेगमेंटेशन का नतीजा इस तरह देखा जा सकता है:

SwiftObjective-C
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 को सेगमेंटेशन का सटीक नतीजा देने के लिए, इमेज कम से कम 256x256 पिक्सल की होनी चाहिए.
  • अगर रीयल-टाइम ऐप्लिकेशन में सेल्फ़ी सेगमेंटेशन किया जाता है, तो इनपुट इमेज के कुल डाइमेंशन पर भी ध्यान दिया जा सकता है. छोटी इमेज को तेज़ी से प्रोसेस किया जा सकता है. इसलिए, इंतज़ार का समय कम करने के लिए, इमेज को कम रिज़ॉल्यूशन में कैप्चर करें. हालांकि, रिज़ॉल्यूशन से जुड़ी ऊपर बताई गई ज़रूरी शर्तों को ध्यान में रखें. साथ ही, पक्का करें कि इमेज में ऑब्जेक्ट ज़्यादा से ज़्यादा जगह ले रहा हो.
  • इमेज का फ़ोकस खराब होने पर भी नतीजों की सटीकता पर असर पड़ सकता है. अगर आपको सही नतीजे नहीं मिलते हैं, तो उपयोगकर्ता से इमेज फिर से लेने के लिए कहें.

अगर आपको रीयल-टाइम ऐप्लिकेशन में सेगमेंटेशन का इस्तेमाल करना है, तो सबसे अच्छा फ़्रेम रेट पाने के लिए इन दिशा-निर्देशों का पालन करें:

  • stream सेगमेंटर मोड का इस्तेमाल करें.
  • इमेज को कम रिज़ॉल्यूशन में कैप्चर करें. हालांकि, इस एपीआई के लिए इमेज के डाइमेंशन से जुड़ी ज़रूरी शर्तों का भी ध्यान रखें.
  • वीडियो फ़्रेम को प्रोसेस करने के लिए, सेगमेंटर के results(in:) सिंक्रोनस एपीआई का इस्तेमाल करें. दिए गए वीडियो फ़्रेम से सिंक करके नतीजे पाने के लिए, AVCaptureVideoDataOutputSampleBufferDelegate के captureOutput(_, didOutput:from:) फ़ंक्शन से इस मेथड को कॉल करें. सेगमेंटर को कॉल को कम करने के लिए, AVCaptureVideoDataOutput के alwaysDiscardsLateVideoFrames को 'सही' के तौर पर सेट करें. अगर सेगमेंटर चलने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध होता है, तो उसे हटा दिया जाएगा.
  • अगर इनपुट इमेज पर ग्राफ़िक ओवरले करने के लिए, सेगमेंटर के आउटपुट का इस्तेमाल किया जाता है, तो पहले ML Kit से नतीजा पाएं. इसके बाद, एक ही चरण में इमेज और ओवरले को रेंडर करें. ऐसा करने पर, प्रोसेस किए गए हर इनपुट फ़्रेम के लिए, डिसप्ले प्लैटफ़ॉर्म पर सिर्फ़ एक बार रेंडर किया जाता है. उदाहरण के लिए, ML Kit के शुरुआती सैंपल में previewOverlayView और CameraViewController क्लास देखें.