คุณสามารถใช้ Firebase ML เพื่อจดจำจุดสังเกตที่มีชื่อเสียงในรูปภาพ
ก่อนเริ่มต้น
-
หากยังไม่ได้เพิ่ม Firebase ลงในแอป ให้ทําตามขั้นตอนในคู่มือการเริ่มต้นใช้งาน
- เปิดโปรเจ็กต์แอปใน Xcode แล้วไปที่ไฟล์ > เพิ่มแพ็กเกจ
- เมื่อได้รับข้อความแจ้ง ให้เพิ่มที่เก็บ Firebase SDK สําหรับแพลตฟอร์ม Apple ดังนี้
- เลือกคลัง Firebase ML
- เพิ่ม Flag
-ObjC
ลงในส่วน Other Linker Flags ของการตั้งค่าบิลด์เป้าหมาย - เมื่อเสร็จแล้ว Xcode จะเริ่มจับคู่ข้อมูลและดาวน์โหลดทรัพยากร Dependency ในเบื้องหลังโดยอัตโนมัติ
- นําเข้า Firebase ในแอป โดยทําดังนี้
Swift
import FirebaseMLModelDownloader
Objective-C
@import FirebaseMLModelDownloader;
-
หากยังไม่ได้เปิดใช้ API ที่อยู่ในระบบคลาวด์สําหรับโปรเจ็กต์ ให้ทําดังนี้
- เปิดFirebase ML หน้า API ในคอนโซล Firebase
-
หากยังไม่ได้อัปเกรดโปรเจ็กต์เป็นแพ็กเกจราคาแบบจ่ายตามการใช้งานของ Blaze ให้คลิกอัปเกรด (ระบบจะแจ้งให้อัปเกรดเฉพาะในกรณีที่โปรเจ็กต์ไม่ได้อยู่ในแพ็กเกจราคาของ Blaze)
เฉพาะโปรเจ็กต์ในแพ็กเกจราคา Blaze เท่านั้นที่ใช้ API ที่อยู่ในระบบคลาวด์ได้
- หากยังไม่ได้เปิดใช้ API บนระบบคลาวด์ ให้คลิกเปิดใช้ API บนระบบคลาวด์
ใช้ Swift Package Manager เพื่อติดตั้งและจัดการทรัพยากร Dependency ของ Firebase
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;
ในขั้นตอนถัดไป ให้ส่งออบเจ็กต์ VisionCloudDetectorOptions
เมื่อสร้างออบเจ็กต์เครื่องตรวจจับของ Cloud
เรียกใช้ตัวตรวจหาจุดสังเกต
หากต้องการจดจำจุดสังเกตในรูปภาพ ให้ส่งรูปภาพเป็นUIImage
หรือ
CMSampleBufferRef
ไปยังdetect(in:)
วิธีของ VisionCloudLandmarkDetector
ดังนี้
- รับอินสแตนซ์ของ
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];
-
รูปภาพต้องอยู่ในรูปแบบสตริงที่เข้ารหัส Base64 จึงจะเรียกใช้ Cloud Vision ได้ วิธีประมวลผล
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 ใช้งานได้จริง คุณควรทําตามขั้นตอนเพิ่มเติมเพื่อป้องกันและลดผลกระทบจากการเข้าถึง API ที่ไม่ได้รับอนุญาต