आप किसी छवि में प्रसिद्ध स्थलों को पहचानने के लिए एमएल किट का उपयोग कर सकते हैं।
शुरू करने से पहले
- यदि आपने पहले से अपने ऐप में Firebase नहीं जोड़ा है, तो आरंभ करने की मार्गदर्शिका में दिए गए चरणों का पालन करके ऐसा करें।
- अपने पॉडफाइल में एमएल किट लाइब्रेरी शामिल करें:
pod 'Firebase/MLVision', '6.25.0'
अपने प्रोजेक्ट के पॉड्स को इंस्टॉल या अपडेट करने के बाद, अपने एक्सकोड प्रोजेक्ट को इसके.xcworkspace
का उपयोग करके खोलना सुनिश्चित करें। - अपने ऐप्लिकेशन में, Firebase आयात करें:
तीव्र
import Firebase
उद्देश्य सी
@import Firebase;
यदि आपने अपने प्रोजेक्ट के लिए पहले से क्लाउड-आधारित API सक्षम नहीं किया है, तो अभी करें:
- फायरबेस कंसोल का एमएल किट एपीआई पेज खोलें।
यदि आपने पहले से ही अपने प्रोजेक्ट को ब्लेज़ मूल्य निर्धारण योजना में अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड पर क्लिक करें। (आपको केवल तभी अपग्रेड करने के लिए कहा जाएगा जब आपका प्रोजेक्ट ब्लेज़ प्लान पर न हो।)
केवल ब्लेज़-स्तरीय प्रोजेक्ट ही क्लाउड-आधारित API का उपयोग कर सकते हैं।
- यदि क्लाउड-आधारित API पहले से सक्षम नहीं हैं, तो क्लाउड-आधारित API सक्षम करें क्लिक करें .
लैंडमार्क डिटेक्टर को कॉन्फ़िगर करें
डिफ़ॉल्ट रूप से, क्लाउड डिटेक्टर मॉडल के स्थिर संस्करण का उपयोग करता है और अधिकतम 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];
UIImage
याCMSampleBufferRef
का उपयोग करके एकVisionImage
ऑब्जेक्ट बनाएं।UIImage
का उपयोग करने के लिए:- यदि आवश्यक हो, तो छवि को घुमाएं ताकि उसकी छवि
imageOrientation
गुण.up
। - सही ढंग से घुमाए गए
UIImage
का उपयोग करके एकVisionImage
ऑब्जेक्ट बनाएं। कोई रोटेशन मेटाडेटा निर्दिष्ट न करें—डिफ़ॉल्ट मान,.topLeft
, का उपयोग किया जाना चाहिए।तीव्र
let image = VisionImage(image: uiImage)
उद्देश्य सी
FIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage];
CMSampleBufferRef
का उपयोग करने के लिए:एक
VisionImageMetadata
ऑब्जेक्ट बनाएं जोCMSampleBufferRef
बफ़र में निहित छवि डेटा के उन्मुखीकरण को निर्दिष्ट करता है।छवि अभिविन्यास प्राप्त करने के लिए:
तीव्र
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 } }
उद्देश्य सी
- (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; } }
फिर, मेटाडेटा ऑब्जेक्ट बनाएं:
तीव्र
let cameraPosition = AVCaptureDevice.Position.back // Set to the capture device you used. let metadata = VisionImageMetadata() metadata.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition )
उद्देश्य सी
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
ऑब्जेक्ट बनाएं:तीव्र
let image = VisionImage(buffer: sampleBuffer) image.metadata = metadata
उद्देश्य सी
FIRVisionImage *image = [[FIRVisionImage alloc] initWithBuffer:sampleBuffer]; image.metadata = metadata;
- यदि आवश्यक हो, तो छवि को घुमाएं ताकि उसकी छवि
- फिर, इमेज को
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]; }
अगले कदम
- क्लाउड एपीआई का उपयोग करने वाले ऐप के उत्पादन के लिए तैनात करने से पहले, आपको अनधिकृत एपीआई एक्सेस के प्रभाव को रोकने और कम करने के लिए कुछ अतिरिक्त कदम उठाने चाहिए।