אתה יכול להשתמש ב-Firebase ML כדי לזהות נקודות ציון ידועות בתמונה.
לפני שאתה מתחיל
- אם עדיין לא הוספת את Firebase לאפליקציה שלך, עשה זאת על ידי ביצוע השלבים במדריך לתחילת העבודה .
- ב-Xcode, כשפרויקט האפליקציה שלך פתוח, נווט אל קובץ > הוסף חבילות .
- כשתתבקש, הוסף את מאגר Firebase Apple platforms SDK:
- בחר את ספריית Firebase ML.
- בסיום, Xcode יתחיל באופן אוטומטי לפתור ולהוריד את התלות שלך ברקע.
- באפליקציה שלך, ייבא את Firebase:
מָהִיר
import FirebaseMLModelDownloader
Objective-C
@import FirebaseMLModelDownloader;
אם עדיין לא הפעלת ממשקי API מבוססי ענן עבור הפרויקט שלך, עשה זאת כעת:
- פתח את הדף Firebase ML APIs של מסוף Firebase.
אם עדיין לא שדרגת את הפרויקט שלך לתוכנית התמחור של Blaze, לחץ על שדרג כדי לעשות זאת. (תתבקש לשדרג רק אם הפרויקט שלך אינו בתוכנית Blaze.)
רק פרויקטים ברמת Blaze יכולים להשתמש בממשקי API מבוססי ענן.
- אם ממשקי API מבוססי ענן עדיין לא מופעלים, לחץ על הפעל ממשקי API מבוססי ענן .
השתמש ב- Swift Package Manager כדי להתקין ולנהל תלות ב-Firebase.
https://github.com/firebase/firebase-ios-sdk
לאחר מכן, בצע כמה הגדרות בתוך האפליקציה:
הגדר את גלאי ציוני הדרך
כברירת מחדל, גלאי הענן משתמש בגרסה היציבה של הדגם ומחזיר עד 10 תוצאות. אם ברצונך לשנות אחת מההגדרות הללו, ציין אותן באמצעות אובייקט VisionCloudDetectorOptions
כמו בדוגמה הבאה:
מָהִיר
let options = VisionCloudDetectorOptions() options.modelType = .latest options.maxResults = 20
Objective-C
FIRVisionCloudDetectorOptions *options = [[FIRVisionCloudDetectorOptions alloc] init]; options.modelType = FIRVisionCloudModelTypeLatest; options.maxResults = 20;
בשלב הבא, העבר את אובייקט VisionCloudDetectorOptions
בעת יצירת אובייקט גלאי הענן.
הפעל את גלאי ציוני הדרך
כדי לזהות ציוני דרך בתמונה, העבר את התמונה כ-UIImage
או CMSampleBufferRef
לשיטת detect(in:)
של VisionCloudLandmarkDetector
:- קבל מופע של
VisionCloudLandmarkDetector
:מָהִיר
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
:מָהִיר
guard let imageData = uiImage.jpegData(compressionQuality: 1.0f) else { return } let base64encodedImage = imageData.base64EncodedString()
Objective-C
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 // ... }
Objective-C
[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 }
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, עליך לנקוט כמה צעדים נוספים כדי למנוע ולהפחית את ההשפעה של גישה לא מורשית ל-API .