iOS पर ML Kit की मदद से पोज़ का पता लगाएं

ML Kit, पोज़ की पहचान करने के लिए ऑप्टिमाइज़ किए गए दो SDK टूल देता है.

SDK टूल का नामPoseDetectionPoseDetectionAccurate
लागू करनाबेस डिटेक्टर की ऐसेट, बिल्ड के दौरान आपके ऐप्लिकेशन के साथ स्टैटिक रूप से लिंक होती हैं.सटीक डिटेक्टर के एसेट, बिल्ड के दौरान आपके ऐप्लिकेशन के साथ स्टैटिक रूप से लिंक होते हैं.
ऐप्लिकेशन का साइज़ज़्यादा से ज़्यादा 29.6 एमबीज़्यादा से ज़्यादा 33.2 एमबी
परफ़ॉर्मेंसiPhone X: ~45FPSiPhone X: ~29 फ़्रेम प्रति सेकंड

इसे आज़माएं

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

  1. अपनी Podfile में, नीचे दिए गए ML Kit पॉड शामिल करें:

    # If you want to use the base implementation:
    pod 'GoogleMLKit/PoseDetection', '3.2.0'
    
    # If you want to use the accurate implementation:
    pod 'GoogleMLKit/PoseDetectionAccurate', '3.2.0'
    
  2. अपने प्रोजेक्ट के पॉड को इंस्टॉल या अपडेट करने के बाद, Xcode प्रोजेक्ट के xcworkspace का इस्तेमाल करके उसे खोलें. ML Kit, Xcode के 13.2.1 या इसके बाद के वर्शन पर काम करता है.

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

किसी इमेज में पोज़ का पता लगाने के लिए, पहले PoseDetector का इंस्टेंस बनाएं और वैकल्पिक रूप से, डिटेक्टर की सेटिंग तय करें.

PoseDetector विकल्प

पहचान मोड

PoseDetector, पहचान करने वाले दो मोड में काम करता है. पक्का करें कि आपने वही विकल्प चुना है जो मिलता-जुलता है आपके इस्तेमाल का उदाहरण.

stream (डिफ़ॉल्ट)
पोज़ डिटेक्टर पहले, प्रमुख व्यक्ति को चुनें और फिर पोज़ डिटेक्शन चलाएं. बाद के फ़्रेम में, व्यक्ति की पहचान करने का तरीका तब तक नहीं चलाया जाएगा, जब तक व्यक्ति जो अब भरोसेमंद नहीं होते या अब पूरे भरोसे के साथ नहीं दिखते. पोज़ डिटेक्टर यह काम करेगा यह सबसे प्रमुख व्यक्ति को ट्रैक करने की कोशिश करता है और हर अनुमान. इससे इंतज़ार का समय कम होता है और बेहतर तरीके से पता लगाया जाता है. इस मोड का इस्तेमाल तब करें, जब वीडियो स्ट्रीम में पोज़ का पता लगाना हो.
singleImage
पोज़ डिटेक्टर, किसी व्यक्ति का पता लगाएगा और फिर पोज़ देगा पता लगाया जा सकता है. हर इमेज के लिए, व्यक्ति की पहचान करने वाला चरण चलेगा. इससे इंतज़ार का समय की संख्या बढ़ जाती है और इसमें लोगों को ट्रैक करने की ज़रूरत नहीं होती. पोज़ का इस्तेमाल करते समय इस मोड का इस्तेमाल करें पता लगाने की सुविधा का इस्तेमाल करें या जहां ट्रैकिंग की ज़रूरत नहीं है.

पोज़ डिटेक्टर के विकल्प तय करें:

Swift

// Base pose detector with streaming, when depending on the PoseDetection SDK
let options = PoseDetectorOptions()
options.detectorMode = .stream

// Accurate pose detector on static images, when depending on the
// PoseDetectionAccurate SDK
let options = AccuratePoseDetectorOptions()
options.detectorMode = .singleImage

Objective-C

// Base pose detector with streaming, when depending on the PoseDetection SDK
MLKPoseDetectorOptions *options = [[MLKPoseDetectorOptions alloc] init];
options.detectorMode = MLKPoseDetectorModeStream;

// Accurate pose detector on static images, when depending on the
// PoseDetectionAccurate SDK
MLKAccuratePoseDetectorOptions *options =
    [[MLKAccuratePoseDetectorOptions alloc] init];
options.detectorMode = MLKPoseDetectorModeSingleImage;

आखिर में, PoseDetector का इंस्टेंस पाएं. तय किए गए विकल्पों को पास करें:

Swift

let poseDetector = PoseDetector.poseDetector(options: options)

Objective-C

MLKPoseDetector *poseDetector =
    [MLKPoseDetector poseDetectorWithOptions:options];

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

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

एक VisionImage ऑब्जेक्ट को UIImage या CMSampleBuffer.

अगर 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;
      }
    }
          
  • इसका इस्तेमाल करके एक VisionImage ऑब्जेक्ट बनाएं CMSampleBuffer ऑब्जेक्ट और ओरिएंटेशन:

    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 को पोज डिटेक्टर के इमेज प्रोसेसिंग के तरीकों में से किसी एक पर पास करें. आपके पास एसिंक्रोनस process(image:) तरीके या सिंक्रोनस results() तरीके का इस्तेमाल करने का विकल्प होता है.

ऑब्जेक्ट का सिंक्रोनस रूप से पता लगाने के लिए:

Swift

var results: [Pose]
do {
  results = try poseDetector.results(in: image)
} catch let error {
  print("Failed to detect pose with error: \(error.localizedDescription).")
  return
}
guard let detectedPoses = results, !detectedPoses.isEmpty else {
  print("Pose detector returned no results.")
  return
}

// Success. Get pose landmarks here.

Objective-C

NSError *error;
NSArray *poses = [poseDetector resultsInImage:image error:&error];
if (error != nil) {
  // Error.
  return;
}
if (poses.count == 0) {
  // No pose detected.
  return;
}

// Success. Get pose landmarks here.

एसिंक्रोनस तरीके से ऑब्जेक्ट का पता लगाने के लिए:

Swift

poseDetector.process(image) { detectedPoses, error in
  guard error == nil else {
    // Error.
    return
  }
  guard !detectedPoses.isEmpty else {
    // No pose detected.
    return
  }

  // Success. Get pose landmarks here.
}

Objective-C

[poseDetector processImage:image
                completion:^(NSArray * _Nullable poses,
                             NSError * _Nullable error) {
                    if (error != nil) {
                      // Error.
                      return;
                    }
                    if (poses.count == 0) {
                      // No pose detected.
                      return;
                    }

                    // Success. Get pose landmarks here.
                  }];

4. पहचाने गए पोज़ के बारे में जानकारी पाएं

अगर इमेज में किसी व्यक्ति का पता चलता है, तो पोज़ डिटेक्शन एपीआई Pose ऑब्जेक्ट का अरे, पूरे करने वाले हैंडलर को या अरे को नतीजे के तौर पर दिखाता है, यह इस बात पर निर्भर करेगा कि आपने एसिंक्रोनस तरीका इस्तेमाल किया है या सिंक्रोनस तरीका.

अगर व्यक्ति इमेज में पूरी तरह से नहीं है, तो मॉडल फ़्रेम के बाहर मौजूद निर्देशांक कम कर देते हैं, ताकि InFrameConफ़िडेंस वैल्यू.

अगर किसी भी व्यक्ति को जानकारी नहीं मिली, तो कलेक्शन खाली है.

Swift

for pose in detectedPoses {
  let leftAnkleLandmark = pose.landmark(ofType: .leftAnkle)
  if leftAnkleLandmark.inFrameLikelihood > 0.5 {
    let position = leftAnkleLandmark.position
  }
}

Objective-C

for (MLKPose *pose in detectedPoses) {
  MLKPoseLandmark *leftAnkleLandmark =
      [pose landmarkOfType:MLKPoseLandmarkTypeLeftAnkle];
  if (leftAnkleLandmark.inFrameLikelihood > 0.5) {
    MLKVision3DPoint *position = leftAnkleLandmark.position;
  }
}

परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह

आपके नतीजों की क्वालिटी, इनपुट इमेज की क्वालिटी पर निर्भर करती है:

  • ML किट में पोज़ का सटीक तरीके से पता लगाने के लिए, इमेज में मौजूद व्यक्ति को जिसे काफ़ी पिक्सल डेटा से दिखाया जाता है; अच्छी परफ़ॉर्मेंस के लिए, सब्जेक्ट को कम से कम 256x256 पिक्सल का हो.
  • अगर आपको किसी रीयल-टाइम ऐप्लिकेशन में पोज़ का पता चलता है, तो इनपुट इमेज के कुल डाइमेंशन. छोटी इमेज को प्रोसेस किया जा सकता है तेज़ी से काम करता है, इसलिए इंतज़ार का समय कम करने के लिए, कम रिज़ॉल्यूशन पर इमेज कैप्चर करें, लेकिन ऊपर दी गई समस्या हल करने की ज़रूरी शर्तों को ध्यान में रखें. साथ ही, पक्का करें कि विषय में सभी लोग शामिल हों इमेज का ज़्यादातर हिस्सा.
  • खराब इमेज फ़ोकस की वजह से भी सटीक जानकारी पर असर पड़ सकता है. अगर आपको अच्छे नतीजे नहीं मिलते, तो उपयोगकर्ता से इमेज को फिर से कैप्चर करने के लिए कहें.

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

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

अगले चरण