आप किसी इमेज में जाने-माने लैंडमार्क की पहचान करने के लिए Firebase ML का इस्तेमाल कर सकते हैं.
शुरू करने से पहले
- यदि आपने पहले से ही अपने ऐप में Firebase नहीं जोड़ा है, तो आरंभ करने की मार्गदर्शिका में दिए गए चरणों का पालन करके ऐसा करें।
- Xcode में, अपने ऐप प्रोजेक्ट को खोलने के साथ, File > Add Packages पर नेविगेट करें।
- संकेत दिए जाने पर, Firebase Apple प्लेटफ़ॉर्म SDK रिपॉजिटरी जोड़ें:
- फायरबेस एमएल लाइब्रेरी चुनें।
- समाप्त होने पर, Xcode स्वचालित रूप से पृष्ठभूमि में आपकी निर्भरताओं को हल करना और डाउनलोड करना शुरू कर देगा।
- अपने ऐप में, फायरबेस आयात करें:
तीव्र
import FirebaseMLModelDownloader
उद्देश्य सी
@import FirebaseMLModelDownloader;
यदि आपने अपने प्रोजेक्ट के लिए क्लाउड-आधारित API को पहले से सक्षम नहीं किया है, तो अभी ऐसा करें:
- Firebase कंसोल का Firebase ML APIs पेज खोलें।
यदि आपने अपने प्रोजेक्ट को ब्लेज़ प्राइसिंग प्लान में पहले से अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड पर क्लिक करें। (आपको केवल तभी अपग्रेड करने के लिए कहा जाएगा यदि आपका प्रोजेक्ट ब्लेज़ योजना पर नहीं है।)
केवल ब्लेज़-स्तरीय परियोजनाएँ ही क्लाउड-आधारित API का उपयोग कर सकती हैं।
- यदि क्लाउड-आधारित API पहले से सक्षम नहीं हैं, तो क्लाउड-आधारित API सक्षम करें पर क्लिक करें ।
फायरबेस निर्भरताओं को स्थापित और प्रबंधित करने के लिए स्विफ्ट पैकेज मैनेजर का उपयोग करें।
https://github.com/firebase/firebase-ios-sdk
अगला, कुछ इन-ऐप सेटअप करें:
लैंडमार्क डिटेक्टर को कॉन्फ़िगर करें
डिफ़ॉल्ट रूप से, क्लाउड डिटेक्टर मॉडल के स्थिर संस्करण का उपयोग करता है और 10 परिणाम तक लौटाता है। यदि आप इनमें से किसी भी सेटिंग को बदलना चाहते हैं, तो उन्हें निम्नलिखित उदाहरण के अनुसार VisionCloudDetectorOptions
ऑब्जेक्ट के साथ निर्दिष्ट करें:
तीव्र
let options = VisionCloudDetectorOptions() options.modelType = .latest options.maxResults = 20
उद्देश्य सी
FIRVisionCloudDetectorOptions *options = [[FIRVisionCloudDetectorOptions alloc] init]; options.modelType = FIRVisionCloudModelTypeLatest; options.maxResults = 20;
जब आप क्लाउड डिटेक्टर ऑब्जेक्ट बनाते हैं तो अगले चरण में VisionCloudDetectorOptions
ऑब्जेक्ट को पास करें।
लैंडमार्क डिटेक्टर चलाएं
किसी छवि में स्थलों को पहचानने के लिए, छवि कोUIImage
या CMSampleBufferRef
के रूप में VisionCloudLandmarkDetector
के detect(in:)
विधि में पास करें:-
VisionCloudLandmarkDetector
का उदाहरण प्राप्त करें:तीव्र
lazy var vision = Vision.vision() let cloudDetector = vision.cloudLandmarkDetector(options: options) // Or, to use the default settings: // let cloudDetector = vision.cloudLandmarkDetector()
उद्देश्य सी
FIRVision *vision = [FIRVision vision]; FIRVisionCloudLandmarkDetector *landmarkDetector = [vision cloudLandmarkDetector]; // Or, to change the default settings: // FIRVisionCloudLandmarkDetector *landmarkDetector = // [vision cloudLandmarkDetectorWithOptions:options];
- क्लाउड विजन को कॉल करने के लिए, छवि को बेस 64-एन्कोडेड स्ट्रिंग के रूप में स्वरूपित किया जाना चाहिए।
UIImage
को संसाधित करने के लिए:तीव्र
guard let imageData = uiImage.jpegData(compressionQuality: 1.0f) else { return } let base64encodedImage = imageData.base64EncodedString()
उद्देश्य सी
NSData *imageData = UIImageJPEGRepresentation(uiImage, 1.0f); NSString *base64encodedImage = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
- फिर, इमेज को
detect(in:)
मेथड में पास करें:तीव्र
cloudDetector.detect(in: visionImage) { landmarks, error in guard error == nil, let landmarks = landmarks, !landmarks.isEmpty else { // ... return } // Recognized landmarks // ... }
उद्देश्य सी
[landmarkDetector detectInImage:image completion:^(NSArray<FIRVisionCloudLandmark *> *landmarks, NSError *error) { if (error != nil) { return; } else if (landmarks != nil) { // Got landmarks } }];
मान्यता प्राप्त स्थलों के बारे में जानकारी प्राप्त करें
यदि लैंडमार्क की पहचान सफल हो जाती है, तोVisionCloudLandmark
ऑब्जेक्ट्स की एक सरणी पूर्णता हैंडलर को पास कर दी जाएगी। प्रत्येक वस्तु से, आप छवि में पहचाने गए एक लैंडमार्क के बारे में जानकारी प्राप्त कर सकते हैं।उदाहरण के लिए:
तीव्र
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 }
उद्देश्य सी
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]; }
अगले कदम
- इससे पहले कि आप क्लाउड एपीआई का उपयोग करने वाले ऐप को प्रोडक्शन में तैनात करें, आपको अनधिकृत एपीआई एक्सेस के प्रभाव को रोकने और कम करने के लिए कुछ अतिरिक्त कदम उठाने चाहिए।