AutoML Vision Edge इस्तेमाल करके, मॉडल को ट्रेनिंग देने के बाद, ऐप्लिकेशन में इमेज को लेबल करने के लिए इसका इस्तेमाल किया जा सकता है.
शुरू करने से पहले
- अगर आपने पहले से ही अपने ऐप्लिकेशन में Firebase नहीं जोड़ा है, तो शुरुआती निर्देश में दिए गए निर्देशों का पालन करके ऐसा करें.
- अपनी Podfile में ML Kit लाइब्रेरी शामिल करें:
pod 'Firebase/MLVision', '6.25.0' pod 'Firebase/MLVisionAutoML', '6.25.0'
अपने प्रोजेक्ट के पॉड को इंस्टॉल या अपडेट करने के बाद, पक्का करें कि आपने Xcode प्रोजेक्ट को इसके.xcworkspace
का इस्तेमाल करके खोला हो. - अपने ऐप्लिकेशन में, Firebase इंपोर्ट करें:
Swift
import Firebase
Objective-C
@import Firebase;
1. मॉडल लोड करें
ML Kit की मदद से, डिवाइस पर AutoML से जनरेट किए गए मॉडल चलाए जा सकते हैं. हालांकि, एमएल किट को कॉन्फ़िगर किया जा सकता है, ताकि आप अपने मॉडल को Firebase से रिमोट लोकेशन से, लोकल स्टोरेज से या दोनों जगहों से लोड कर सकें.
Firebase पर मॉडल होस्ट करके, ऐप्लिकेशन का कोई नया वर्शन रिलीज़ किए बिना, मॉडल को अपडेट किया जा सकता है. साथ ही, रिमोट कॉन्फ���िगरेशन और A/B टेस्टिंग का इस्तेमाल करके, उपयोगकर्ताओं के अलग-अलग सेट के लिए डाइनैमिक तरीके से अलग-अलग मॉडल दिखाए जा सकते हैं.
अगर मॉडल को स��र्फ़ Firebase के साथ होस्ट करके उपलब्ध कराना है, उसे अपने ऐप्लिकेशन के साथ बंडल नहीं करना है, तो अपने ऐप्लिकेशन के डाउनलोड साइज़ को कम किया जा सकता है. हालांकि, ध्यान रखें कि अगर इस मॉडल को आपके ऐप्लिकेशन के साथ बंडल नहीं किया गया है, तो मॉडल से जुड़ी कोई भी सुविधा तब तक उपलब्ध नहीं होगी, जब तक कि आपका ऐप्लिकेशन, मॉडल को पहली बार डाउनलोड नहीं करता.
मॉडल को अपने ऐप्लिकेशन के साथ बंडल करके, यह पक्का किया जा सकता है कि Firebase से होस्ट किया गया मॉडल उपलब्ध न होने पर भी, आपके ऐप्लिकेशन की एमएल सुविधाएं काम करती हों.
Firebase से होस्ट किए गए मॉडल सोर्स को कॉन्फ़िगर करना
रिमोट तरीके से होस्ट किए गए मॉडल का इस्तेमाल करने के लिए, AutoMLRemoteModel
ऑब्जेक्ट बनाएं.
साथ ही, यह बताएं कि मॉडल को पब्लिश करते समय आपने मॉडल को कौनसा नाम दिया था:
Swift
let remoteModel = AutoMLRemoteModel(
name: "your_remote_model" // The name you assigned in the Firebase console.
)
Objective-C
FIRAutoMLRemoteModel *remoteModel = [[FIRAutoMLRemoteModel alloc]
initWithName:@"your_remote_model"]; // The name you assigned in the Firebase console.
इसके बाद, उन शर्तों के बारे में बताते हुए मॉडल डाउनलोड टास्क शुरू करें जिनमें आपको डाउनलोड करने की अनुमति देनी है. अगर डिवाइस पर मॉडल मौजूद नहीं है या मॉडल का नया वर्शन उपलब्ध है, तो टास्क, Firebase से उस मॉडल को एसिंक्रोनस रूप से डाउनलोड करेगा:
Swift
let downloadConditions = ModelDownloadConditions(
allowsCellularAccess: true,
allowsBackgroundDownloading: true
)
let downloadProgress = ModelManager.modelManager().download(
remoteModel,
conditions: downloadConditions
)
Objective-C
FIRModelDownloadConditions *downloadConditions =
[[FIRModelDownloadConditions alloc] initWithAllowsCellularAccess:YES
allowsBackgroundDownloading:YES];
NSProgress *downloadProgress =
[[FIRModelManager modelManager] downloadRemoteModel:remoteModel
conditions:downloadConditions];
कई ऐप्लिकेशन, डाउनलोड टास्क को अपने शुरू करने वाले कोड में शुरू करते हैं. हालांकि, मॉडल का इस्तेमाल करने से पहले, किसी भी समय ऐसा किया जा सकता है.
लोकल मॉडल सोर्स कॉन्फ़िगर करना
मॉडल को अपने ऐप्लिकेशन के साथ बंडल करने के लिए:
- Firebase कं��ोल से डाउनलोड किए गए ZIP संग्रह से मॉडल और उसके मेटाडेटा को एक फ़ोल्डर में निकालें:
your_model_directory |____dict.txt |____manifest.json |____model.tflite
तीनों फ़ाइलें एक ही फ़ोल्डर में होनी चाहिए. हमारा सुझाव है कि आप फ़ाइलों को ठीक उसी तरह इस्तेमाल करें जिस तरह आपने उन्हें डाउनलोड किया है और बिना कोई बदलाव किए (इनमें फ़ाइल के नाम भी शामिल हैं). - फ़ोल्डर को अपने Xcode प्रोजेक्ट में कॉपी करें. ऐसा करते समय फ़ोल्डर के रेफ़रंस बनाएं विकल्प का ध्यान रखें. मॉडल फ़ाइल और मेटाडेटा को ऐप्लिकेशन बंडल में शामिल किया जाएगा और यह ML Kit में भी उपलब्ध होगा.
- मॉडल मेनिफ़ेस्ट फ़ाइल का पाथ बताते हुए
AutoMLLocalModel
ऑब्जेक्ट बनाएं:Swift
guard let manifestPath = Bundle.main.path( forResource: "manifest", ofType: "json", inDirectory: "your_model_directory" ) else { return true } let localModel = AutoMLLocalModel(manifestPath: manifestPath)
Objective-C
NSString *manifestPath = [NSBundle.mainBundle pathForResource:@"manifest" ofType:@"json" inDirectory:@"your_model_directory"]; FIRAutoMLLocalModel *localModel = [[FIRAutoMLLocalModel alloc] initWithManifestPath:manifestPath];
अपने मॉडल से इमेज लेबलर बनाएं
अपने मॉडल सोर्स को कॉन्फ़िगर करने के बाद, उनमें से किसी एक से VisionImageLabeler
ऑब्जेक्ट बनाएं.
अगर आपके पास सिर्फ़ लोकल बंडल किया गया मॉडल है, तो बस अपने AutoMLLocalModel
ऑब्जेक्ट से लेबलर बनाएं और अपने हिसाब से कॉन्फ़िडेंस स्कोर के थ्रेशोल्ड को कॉन्फ़िगर करें (अपने मॉडल का आकलन करें देखें):
Swift
let options = VisionOnDeviceAutoMLImageLabelerOptions(localModel: localModel)
options.confidenceThreshold = 0 // Evaluate your model in the Firebase console
// to determine an appropriate value.
let labeler = Vision.vision().onDeviceAutoMLImageLabeler(options: options)
Objective-C
FIRVisionOnDeviceAutoMLImageLabelerOptions *options =
[[FIRVisionOnDeviceAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel];
options.confidenceThreshold = 0; // Evaluate your model in the Firebase console
// to determine an appropriate value.
FIRVisionImageLabeler *labeler =
[[FIRVision vision] onDeviceAutoMLImageLabelerWithOptions:options];
अगर आपके पास रिमोट तौर पर होस्ट किया गया मॉडल है, तो उसे चलाने से पहले आपको यह देखना होगा कि वह डाउनलोड हो गया है या ������ं. मॉडल ������ेज�� के isModelDownloaded(remoteModel:)
तरीके का इस्तेमाल करके, मॉडल डाउनलोड
टास्क की स्थिति देखी जा सकती है.
हालांकि, लेबलर को चलाने से पहले आपको इसकी पुष्टि करनी होगी, लेकिन अगर आपके पास
रिमोट तरीके से होस्ट किया गया मॉडल और स्थानीय तौर पर बंडल किया गया मॉडल, दोनों हैं, तो VisionImageLabeler
को इंस्टैंशिएट करते समय
यह जांच करने की समझ आ सकती है: अगर डाउनलोड किया गया है,
तो रिमोट मॉडल से कोई लेबलर बनाएं. अगर ऐसा नहीं है, तो स्थानीय मॉडल से बनाएं.
Swift
var options: VisionOnDeviceAutoMLImageLabelerOptions?
if (ModelManager.modelManager().isModelDownloaded(remoteModel)) {
options = VisionOnDeviceAutoMLImageLabelerOptions(remoteModel: remoteModel)
} else {
options = VisionOnDeviceAutoMLImageLabelerOptions(localModel: localModel)
}
options.confidenceThreshold = 0 // Evaluate your model in the Firebase console
// to determine an appropriate value.
let labeler = Vision.vision().onDeviceAutoMLImageLabeler(options: options)
Objective-C
VisionOnDeviceAutoMLImageLabelerOptions *options;
if ([[FIRModelManager modelManager] isModelDownloaded:remoteModel]) {
options = [[FIRVisionOnDeviceAutoMLImageLabelerOptions alloc] initWithRemoteModel:remoteModel];
} else {
options = [[FIRVisionOnDeviceAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel];
}
options.confidenceThreshold = 0.0f; // Evaluate your model in the Firebase console
// to determine an appropriate value.
FIRVisionImageLabeler *labeler = [[FIRVision vision] onDeviceAutoMLImageLabelerWithOptions:options];
अगर आपके पास सिर्फ़ रिमोट तौर पर होस्ट किया गया मॉडल है, तो आपको मॉडल से जुड़ी फ़ंक्शनलिटी बंद करनी चाहिए. उदाहरण के लिए, अपने यूज़र इंटरफ़ेस (यूआई) का कुछ हिस्सा धूसर करना या छिपाना. ऐसा तब तक करना चाहिए, जब तक आप यह पुष्टि न कर लें कि मॉडल डाउनलोड हो गया है.
डिफ़ॉल्ट नोटिफ़िकेशन सेंटर में ऑब्ज़र्वर अटैच करके, मॉडल डाउनलोड की स्थिति देखी जा सकती है. ऑब्ज़र्वर ब्लॉक में, self
के लिए कमज़ोर रेफ़रंस का इस्तेमाल करना न भूलें. ऐसा इसलिए, क्योंकि डाउनलोड होने में कुछ समय लग सकता है. साथ ही, डाउनलोड पूरा होने तक, ओरिजनल ऑब्जेक्ट को फ़्री किया जा सकता है. उदाहरण के लिए:
Swift
NotificationCenter.default.addObserver( forName: .firebaseMLModelDownloadDidSucceed, object: nil, queue: nil ) { [weak self] notification in guard let strongSelf = self, let userInfo = notification.userInfo, let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue] as? RemoteModel, model.name == "your_remote_model" else { return } // The model was downloaded and is available on the device } NotificationCenter.default.addObserver( forName: .firebaseMLModelDownloadDidFail, object: nil, queue: nil ) { [weak self] notification in guard let strongSelf = self, let userInfo = notification.userInfo, let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue] as? RemoteModel else { return } let error = userInfo[ModelDownloadUserInfoKey.error.rawValue] // ... }
Objective-C
__weak typeof(self) weakSelf = self; [NSNotificationCenter.defaultCenter addObserverForName:FIRModelDownloadDidSucceedNotification object:nil queue:nil usingBlock:^(NSNotification *_Nonnull note) { if (weakSelf == nil | note.userInfo == nil) { return; } __strong typeof(self) strongSelf = weakSelf; FIRRemoteModel *model = note.userInfo[FIRModelDownloadUserInfoKeyRemoteModel]; if ([model.name isEqualToString:@"your_remote_model"]) { // The model was downloaded and is available on the device } }]; [NSNotificationCenter.defaultCenter addObserverForName:FIRModelDownloadDidFailNotification object:nil queue:nil usingBlock:^(NSNotification *_Nonnull note) { if (weakSelf == nil | note.userInfo == nil) { return; } __strong typeof(self) strongSelf = weakSelf; NSError *error = note.userInfo[FIRModelDownloadUserInfoKeyError]; }];
2. इनपुट इमेज तैयार करें
इसके बाद, हर उस इमेज के लिए जिसे आप लेबल करना चाहते हैं, इस सेक्शन में दिए गए विकल्पों में से किसी एक विकल्प का इस्तेमाल करके VisionImage
ऑब्जेक्ट बनाएं और उसे VisionImageLabeler
(अगले सेक्शन में बताया गया) के इंस्टेंस में पास करें.
UIImage
या CMSampleBufferRef
का इस्तेमाल करके, VisionImage
ऑब्जेक्ट बनाएं.
UIImage
का इस्तेमाल करने के लिए:
- अगर ज़रूरी हो, तो इमेज को घुमाएं, ताकि उसकी
imageOrientation
प्रॉपर्टी.up
हो. - सही तरीके से घुमाए गए
UIImage
का इस्तेमाल करके,VisionImage
ऑब्जेक्ट बनाएं. कोई भी रोटेशन मेटाडेटा तय न करें—डिफ़ॉल्ट वैल्यू,.topLeft
, का इस्तेमाल किया जाना चाहिए.Swift
let image = VisionImage(image: uiImage)
Objective-C
FIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage];
CMSampleBufferRef
का इस्तेमाल करने के लिए:
-
एक
VisionImageMetadata
ऑब्जेक्ट बनाएं, जोCMSampleBufferRef
बफ़र में शामिल इमेज डेटा का ओरिएंटेशन बताता हो.इमेज का ओरिएंटेशन पाने के लिए:
Swift
func imageOrientation( deviceOrientation: UIDeviceOrientation, cameraPosition: AVCaptureDevice.Position ) -> VisionDetectorImageOrientation { switch deviceOrientation { case .portrait: return cameraPosition == .front ? .leftTop : .rightTop case .landscapeLeft: return cameraPosition == .front ? .bottomLeft : .topLeft case .portraitUpsideDown: return cameraPosition == .front ? .rightBottom : .leftBottom case .landscapeRight: return cameraPosition == .front ? .topRight : .bottomRight case .faceDown, .faceUp, .unknown: return .leftTop } }
Objective-C
- (FIRVisionDetectorImageOrientation) imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation cameraPosition:(AVCaptureDevicePosition)cameraPosition { switch (deviceOrientation) { case UIDeviceOrientationPortrait: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationLeftTop; } else { return FIRVisionDetectorImageOrientationRightTop; } case UIDeviceOrientationLandscapeLeft: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationBottomLeft; } else { return FIRVisionDetectorImageOrientationTopLeft; } case UIDeviceOrientationPortraitUpsideDown: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationRightBottom; } else { return FIRVisionDetectorImageOrientationLeftBottom; } case UIDeviceOrientationLandscapeRight: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationTopRight; } else { return FIRVisionDetectorImageOrientationBottomRight; } default: return FIRVisionDetectorImageOrientationTopLeft; } }
इसके बाद, मेटाडेटा ऑब्जेक्ट बनाएं:
Swift
let cameraPosition = AVCaptureDevice.Position.back // Set to the capture device you used. let metadata = VisionImageMetadata() metadata.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition )
Objective-C
FIRVisionImageMetadata *metadata = [[FIRVisionImageMetadata alloc] init]; AVCaptureDevicePosition cameraPosition = AVCaptureDevicePositionBack; // Set to the capture device you used. metadata.orientation = [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation cameraPosition:cameraPosition];
CMSampleBufferRef
ऑब्जेक्ट और रोटेशन मेटाडेटा का इस्तेमाल करकेVisionImage
ऑब्जेक्ट बनाएं:Swift
let image = VisionImage(buffer: sampleBuffer) image.metadata = metadata
Objective-C
FIRVisionImage *image = [[FIRVisionImage alloc] initWithBuffer:sampleBuffer]; image.metadata = metadata;
3. इमेज लेबलर चलाएं
किसी इमेज में ऑब्जेक्ट को लेबल करने के लिए, VisionImage
ऑब्जेक्ट को VisionImageLabeler
के process()
तरीके में पास करें:
Swift
labeler.process(image) { labels, error in
guard error == nil, let labels = labels else { return }
// Task succeeded.
// ...
}
Objective-C
[labeler
processImage:image
completion:^(NSArray<FIRVisionImageLabel *> *_Nullable labels, NSError *_Nullable error) {
if (error != nil || labels == nil) {
return;
}
// Task succeeded.
// ...
}];
इमेज को लेबल करने की प्रोसेस पूरी होने पर, VisionImageLabel
ऑब्जेक्ट का कलेक्शन, पूरा होने वाले हैंडलर को पास कर दिया जाएगा. हर ऑब्जेक्ट से, आपको इमेज में पहचानी गई सुविधा
के बारे में जानकारी मिल सकती है.
उदाहरण के लिए:
Swift
for label in labels {
let labelText = label.text
let confidence = label.confidence
}
Objective-C
for (FIRVisionImageLabel *label in labels) {
NSString *labelText = label.text;
NSNumber *confidence = label.confidence;
}
रीयल-टाइम परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह
- डिटेक्टर को क��ल थ्रॉटल करें. अगर डिटेक्टर के चलने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध हो जाता है, तो फ़्रेम को छोड़ दें.
- अगर इनपुट इमेज पर ग्राफ़िक ओवरले करने के लिए, डिटेक्टर के आउटपुट का इस्तेमाल किया जाता है, तो सबसे पहले एम����� क���� क��� इस्तेमाल क��ें. ����के बाद, ��क ही चरण में इमेज और ओवरले को रेंडर करें. ऐसा करने पर, हर इनपुट फ़्रेम के लिए आपको डिसप्ले प्लैटफ़ॉर्म पर सिर्फ़ एक बार रेंडर करना होगा. उदाहरण के लिए, शोकेस सैंपल ऐप्लिकेशन में previewOverlayView और FIRDetectionOverlayView क्लास देखें.