คุณสามารถใช้ Firebase ML เพื่อจดจำจุดสังเกตที่เป็นที่รู้จักในรูปภาพได้
ก่อนที่คุณจะเริ่ม
- หากคุณยังไม่ได้เพิ่ม Firebase ลงในแอปของคุณ ให้ทำตามขั้นตอนใน คู่มือการเริ่มต้นใช้งาน
- ใน Xcode เมื่อโปรเจ็กต์แอปของคุณเปิดอยู่ ให้ไปที่ File > Add Package
- เมื่อได้รับแจ้ง ให้เพิ่มพื้นที่เก็บข้อมูล SDK แพลตฟอร์ม Firebase Apple:
- เลือกไลบรารี Firebase ML
- เพิ่มแฟล็ก
-ObjC
ไปยังส่วน Other Linker Flags ของการตั้งค่า build ของเป้าหมายของคุณ - เมื่อเสร็จแล้ว Xcode จะเริ่มแก้ไขและดาวน์โหลดการอ้างอิงของคุณโดยอัตโนมัติในเบื้องหลัง
- ในแอปของคุณ ให้นำเข้า Firebase:
สวิฟท์
import FirebaseMLModelDownloader
วัตถุประสงค์-C
@import FirebaseMLModelDownloader;
หากคุณยังไม่ได้เปิดใช้งาน API บนระบบคลาวด์สำหรับโปรเจ็กต์ของคุณ ให้ดำเนินการทันที:
- เปิด หน้า Firebase ML API ของคอนโซล Firebase
หากคุณยังไม่ได้อัปเกรดโปรเจ็กต์เป็นแผนราคา Blaze ให้คลิก อัปเกรด เพื่อดำเนินการดังกล่าว (คุณจะได้รับแจ้งให้อัปเกรดเฉพาะในกรณีที่โปรเจ็กต์ของคุณไม่ได้อยู่ในแผน Blaze)
เฉพาะโปรเจ็กต์ระดับ Blaze เท่านั้นที่ใช้ API บนระบบคลาวด์ได้
- หากยังไม่ได้เปิดใช้งาน API ในระบบคลาวด์ ให้คลิก เปิดใช้งาน API ในระบบคลาวด์
ใช้ Swift Package Manager เพื่อติดตั้งและจัดการการพึ่งพา Firebase
https://github.com/firebase/firebase-ios-sdk.git
จากนั้น ให้ทำการตั้งค่าในแอป:
กำหนดค่าเครื่องตรวจจับจุดสังเกต
ตามค่าเริ่มต้น ตัวตรวจจับคลาวด์จะใช้โมเดลเวอร์ชันเสถียรและส่งกลับผลลัพธ์สูงสุด 10 รายการ หากคุณต้องการเปลี่ยนการตั้งค่าอย่างใดอย่างหนึ่งเหล่านี้ ให้ระบุด้วยออบเจ็กต์ VisionCloudDetectorOptions
ดังตัวอย่างต่อไปนี้:
สวิฟท์
let options = VisionCloudDetectorOptions() options.modelType = .latest options.maxResults = 20
วัตถุประสงค์-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()
วัตถุประสงค์-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.0) else { return } let base64encodedImage = imageData.base64EncodedString()
วัตถุประสงค์-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 // ... }
วัตถุประสงค์-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 }
วัตถุประสงค์-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 ที่ไม่ได้รับอนุญาต