ML Kit, पोज़ का पता लगाने के लिए दो ऑप्टिमाइज़ किए गए SDK टूल उपलब्ध कराता है.
SDK टूल का नाम | PoseDetection | PoseDetectionAccurate |
---|---|---|
लागू करना | बेस डिटेक्टर के लिए ऐसेट, बिल्ड के समय आपके ऐप्लिकेशन से स्टैटिक तौर पर लिंक होती हैं. | सटीक डिटेक्टर के लिए एसेट, बिल्ड के समय आपके ऐप्लिकेशन से स्टैटिक तौर पर लिंक होती हैं. |
ऐप्लिकेशन का साइज़ | 29.6 एमबी तक | 33.2 एमबी तक |
परफ़ॉर्मेंस | iPhone X: ~45FPS | iPhone X: ~29FPS |
इसे आज़माएं
- इस एपीआई के इस्तेमाल का उदाहरण देखने के लिए, सैंपल ऐप्लिकेशन आज़माएं.
शुरू करने से पहले
अपनी Podfile में ये ML Kit पॉड शामिल करें:
# If you want to use the base implementation: pod 'GoogleMLKit/PoseDetection', '7.0.0' # If you want to use the accurate implementation: pod 'GoogleMLKit/PoseDetectionAccurate', '7.0.0'
अपने प्रोजेक्ट के पॉड इंस्टॉल या अपडेट करने के बाद,
xcworkspace
का इस्तेमाल करके अपना Xcode प्रोजेक्ट खोलें. 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. इनपुट इमेज तैयार करना
पोज़ का पता लगाने के लिए, वीडियो की हर इमेज या फ़्रेम के लिए यह तरीका अपनाएं.
अगर आपने स्ट्रीम मोड चालू किया है, तो आपको 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
को पोज़ डिटेक्टर के इमेज प्रोसेसिंग के किसी एक तरीके में पास करें. एसिंक्रोनस 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
ऑब्जेक्ट का कलेक्शन, पूरे होने की जानकारी देने वाले फ़ंक्शन को भेजता है या कलेक्शन को दिखाता है. यह इस बात पर निर्भर करता है कि आपने असाइनोक्रोनस या सिंक्रोनस तरीके को कॉल किया है या नहीं.
अगर व्यक्ति पूरी तरह से इमेज में नहीं था, तो मॉडल, फ़्रेम के बाहर मौजूद लैंडमार्क के निर्देशांक असाइन करता है और उन्हें कम InFrameConfidence वैल्यू देता है.
अगर किसी व्यक्ति का पता नहीं चलता है, तो ऐरे खाली होता है.
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 Kit को पोज़ का सटीक तरीके से पता लगाने के लिए, इमेज में व्यक्ति का ज़रूरत के मुताबिक पिक्सल डेटा होना चाहिए. बेहतर परफ़ॉर्मेंस के लिए, विषय कम से कम 256x256 पिक्सल का होना चाहिए.
- अगर आपको रीयल-टाइम ऐप्लिकेशन में पोज़ का पता चलता है, तो हो सकता है कि आप इनपुट इमेज के सभी डाइमेंशन पर भी ध्यान देना चाहें. छोटी इमेज को तेज़ी से प्रोसेस किया जा सकता है. इसलिए, इंतज़ार का समय कम करने के लिए, इमेज को कम रिज़ॉल्यूशन में कैप्चर करें. हालांकि, रिज़ॉल्यूशन से जुड़ी ऊपर दी गई ज़रूरी शर्तों को ध्यान में रखें और पक्का करें कि ऑब्जेक्ट, इमेज में ज़्यादा से ज़्यादा जगह ले रहा हो.
- इमेज का फ़ोकस खराब होने पर भी नतीजों की सटीकता पर असर पड़ सकता है. अगर आपको सही नतीजे नहीं मिलते हैं, तो उपयोगकर्ता से इमेज फिर से लेने के लिए कहें.
अगर आपको रीयल-टाइम ऐप्लिकेशन में पोज़ डिटेक्शन का इस्तेमाल करना है, तो सबसे अच्छा फ़्रेमरेट पाने के लिए इन दिशा-निर्देशों का पालन करें:
- PoseDetection SDK टूल और
stream
की पहचान करने वाले मोड का इस्तेमाल करें. - इमेज को कम रिज़ॉल्यूशन में कैप्चर करें. हालांकि, इस एपीआई के लिए इमेज के डाइमेंशन से जुड़ी ज़रूरी शर्तों का भी ध्यान रखें.
- वीडियो फ़्रेम को प्रोसेस करने के लिए, डिटेक्टर के
results(in:)
सिंक्रोनस एपीआई का इस्तेमाल करें. दिए गए वीडियो फ़्रेम से सिंक किए गए नतीजे पाने के लिए, AVCaptureVideoDataOutputSampleBufferDelegate के captureOutput(_, didOutput:from:) फ़ंक्शन से इस मेथड को कॉल करें. डिटेक्टर को कॉल को कम करने के लिए, AVCaptureVideoDataOutput के alwaysDiscardsLateVideoFrames को 'सही' के तौर पर सेट रखें. अगर डिटेक्टर चालू होने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध होता है, तो उसे हटा दिया जाएगा. - अगर इनपुट इमेज पर ग्राफ़िक ओवरले करने के लिए, डिटेक्टर के आउटपुट का इस्तेमाल किया जाता है, तो पहले ML Kit से नतीजा पाएं. इसके बाद, एक ही चरण में इमेज और ओवरले को रेंडर करें. ऐसा करने पर, प्रोसेस किए गए हर इनपुट फ़्रेम के लिए, डिसप्ले प्लैटफ़ॉर्म पर सिर्फ़ एक बार रेंडर किया जाता है. उदाहरण के लिए, शोकेस सैंपल ऐप्लिकेशन में previewOverlayView और MLKDetectionOverlayView क्लास देखें.
अगले चरण
- पोज़ की कैटगरी तय करने के लिए, पोज़ के लैंडमार्क का इस्तेमाल करने का तरीका जानने के लिए, पोज़ की कैटगरी तय करने से जुड़ी सलाह देखें.
- इस्तेमाल किए जा रहे इस एपीआई का उदाहरण देखने के लिए, GitHub पर ML Kit का क्विकस्टार्ट सैंपल देखें.