किसी इमेज में मशहूर लैंडमार्क की पहचान करने के लिए, Firebase ML का इस्तेमाल किया जा सकता है.
शुरू करने से पहले
-
अगर आपने अपने ऐप्लिकेशन में पहले से Firebase नहीं जोड़ा है, तो शुरू करने के लिए गाइड में दिए गए निर्देशों का पालन करके ऐसा करें.
- Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलकर, फ़ाइल > पैकेज जोड़ें पर जाएं.
- जब कहा जाए, तब Firebase के Apple प्लैटफ़ॉर्म के SDK टूल का रिपॉज़िटरी जोड़ें:
- Firebase ML लाइब्रेरी चुनें.
- अपने टारगेट की बिल्ड सेटिंग के अन्य लिंकर फ़्लैग सेक्शन में
-ObjC
फ़्लैग जोड़ें. - प्रोसेस पूरी होने के बाद, Xcode बैकग्राउंड में आपकी डिपेंडेंसी को अपने-आप हल और डाउनलोड करना शुरू कर देगा.
- अपने ऐप्लिकेशन में, Firebase इंपोर्ट करें:
Swift
import FirebaseMLModelDownloader
Objective-C
@import FirebaseMLModelDownloader;
-
अगर आपने अब तक अपने प्रोजेक्ट के लिए, क्लाउड पर काम करने वाले एपीआई चालू नहीं किए हैं, तो अभी ऐसा करें:
- Firebase कंसोल का Firebase ML एपीआई पेज खोलें.
-
अगर आपने अब तक अपने प्रोजेक्ट को Blaze की कीमत वाले प्लान पर अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड करें पर क्लिक करें. (आपको अपग्रेड करने के लिए तब ही कहा जाएगा, जब आपका प्रोजेक्ट Blaze प्लान पर न हो.)
सिर्फ़ Blaze-लेवल के प्रोजेक्ट, क्लाउड-आधारित एपीआई का इस्तेमाल कर सकते हैं.
- अगर क्लाउड-आधारित एपीआई पहले से चालू नहीं हैं, तो क्लाउड-आधारित एपीआई चालू करें पर क्लिक करें.
Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift Package Manager का इस्तेमाल करें.
https://github.com/firebase/firebase-ios-sdk.git
इसके बाद, ऐप्लिकेशन में कुछ सेटअप करें:
लैंडमार्क डिटेक्टर को कॉन्फ़िगर करना
डिफ़ॉल्ट रूप से, क्लाउड डिटेक्टर मॉडल के स्टैबल वर्शन का इस्तेमाल करता है और
ज़्यादा से ज़्यादा 10 नतीजे दिखाता है. अगर आपको इनमें से किसी भी सेटिंग को बदलना है, तो
इनके लिए VisionCloudDetectorOptions
ऑब्जेक्ट का इस्तेमाल करें, जैसा कि यहां दिए गए उदाहरण में किया गया है:
Swift
let options = VisionCloudDetectorOptions() options.modelType = .latest options.maxResults = 20
Objective-C
FIRVisionCloudDetectorOptions *options = [[FIRVisionCloudDetectorOptions alloc] init]; options.modelType = FIRVisionCloudModelTypeLatest; options.maxResults = 20;
अगले चरण में, Cloud डिटेक्टर ऑब्जेक्ट बनाते समय VisionCloudDetectorOptions
ऑब्जेक्ट पास करें.
लैंडमार्क डिटेक्टर को चलाना
किसी इमेज में लैंडमार्क की पहचान करने के लिए, इमेज कोUIImage
या CMSampleBufferRef
के तौर पर VisionCloudLandmarkDetector
के detect(in:)
वाले तरीके में पास करें:
VisionCloudLandmarkDetector
का इंस्टेंस पाएं:Swift
lazy var vision = Vision.vision() let cloudDetector = vision.cloudLandmarkDetector(options: options) // Or, to use the default settings: // let cloudDetector = vision.cloudLandmarkDetector()
Objective-C
FIRVision *vision = [FIRVision vision]; FIRVisionCloudLandmarkDetector *landmarkDetector = [vision cloudLandmarkDetector]; // Or, to change the default settings: // FIRVisionCloudLandmarkDetector *landmarkDetector = // [vision cloudLandmarkDetectorWithOptions:options];
-
Cloud Vision को कॉल करने के लिए, इमेज को base64 कोड में बदली गई स्ट्रिंग के तौर पर फ़ॉर्मैट करना ज़रूरी है.
UIImage
को प्रोसेस करने के लिए:Swift
guard let imageData = uiImage.jpegData(compressionQuality: 1.0) else { return } let base64encodedImage = imageData.base64EncodedString()
Objective-C
NSData *imageData = UIImageJPEGRepresentation(uiImage, 1.0f); NSString *base64encodedImage = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
-
इसके बाद, इमेज को
detect(in:)
तरीके में पास करें:Swift
cloudDetector.detect(in: visionImage) { landmarks, error in guard error == nil, let landmarks = landmarks, !landmarks.isEmpty else { // ... return } // Recognized landmarks // ... }
Objective-C
[landmarkDetector detectInImage:image completion:^(NSArray<FIRVisionCloudLandmark *> *landmarks, NSError *error) { if (error != nil) { return; } else if (landmarks != nil) { // Got landmarks } }];
पहचाने गए लैंडमार्क के बारे में जानकारी पाना
अगर लैंडमार्क की पहचान हो जाती है, तोVisionCloudLandmark
ऑब्जेक्ट का कलेक्शन, पूरे होने की प्रोसेस को मैनेज करने वाले फ़ंक्शन को भेज दिया जाएगा. हर ऑब्जेक्ट से, इमेज में पहचाने गए किसी लैंडमार्क के बारे में जानकारी मिल सकती है.
उदाहरण के लिए:
Swift
for landmark in landmarks { let landmarkDesc = landmark.landmark let boundingPoly = landmark.frame let entityId = landmark.entityId // A landmark can have multiple locations: for example, the location the image // was taken, and the location of the landmark depicted. for location in landmark.locations { let latitude = location.latitude let longitude = location.longitude } let confidence = landmark.confidence }
Objective-C
for (FIRVisionCloudLandmark *landmark in landmarks) { NSString *landmarkDesc = landmark.landmark; CGRect frame = landmark.frame; NSString *entityId = landmark.entityId; // A landmark can have multiple locations: for example, the location the image // was taken, and the location of the landmark depicted. for (FIRVisionLatitudeLongitude *location in landmark.locations) { double latitude = [location.latitude doubleValue]; double longitude = [location.longitude doubleValue]; } float confidence = [landmark.confidence floatValue]; }
अगले चरण
- Cloud API का इस्तेमाल करने वाले ऐप्लिकेशन को प्रोडक्शन में डिप्लॉय करने से पहले, आपको कुछ और कदम उठाने चाहिए. इससे बिना अनुमति के एपीआई को ऐक्सेस करने से होने वाले असर को रोका और कम किया जा सकता है.