จดจำจุดสังเกตด้วย Firebase ML บน iOS

คุณใช้ Firebase ML เพื่อจดจำสถานที่สำคัญที่มีชื่อเสียงในรูปภาพได้

ก่อนเริ่มต้น

    หากยังไม่ได้เพิ่ม Firebase ลงในแอป ให้เพิ่มโดยทำตาม ขั้นตอนในคู่มือเริ่มต้นใช้งาน

    ใช้ Swift Package Manager เพื่อติดตั้งและจัดการทรัพยากร Dependency ของ Firebase

    1. เปิดโปรเจ็กต์แอปใน Xcode แล้วไปที่File > Add Packages
    2. เมื่อได้รับข้อความแจ้ง ให้เพิ่มที่เก็บ SDK ของแพลตฟอร์ม Apple ของ Firebase ดังนี้
    3.   https://github.com/firebase/firebase-ios-sdk.git
    4. เลือกFirebase ML คลัง
    5. เพิ่มแฟล็ก -ObjC ลงในส่วนแฟล็ก Linker อื่นๆ ของการตั้งค่าบิลด์ของเป้าหมาย
    6. เมื่อเสร็จแล้ว Xcode จะเริ่มจับคู่ข้อมูลและดาวน์โหลดทรัพยากร Dependency ในเบื้องหลังโดยอัตโนมัติ

    จากนั้นทำการตั้งค่าในแอปดังนี้

    1. ในแอป ให้นำเข้า Firebase ดังนี้

      Swift

      import FirebaseMLModelDownloader

      Objective-C

      @import FirebaseMLModelDownloader;
  1. หากยังไม่ได้เปิดใช้ API บนระบบคลาวด์สำหรับโปรเจ็กต์ ให้ทำดังนี้ ตอนนี้

    1. เปิดFirebase ML หน้า API ในคอนโซล Firebase
    2. หากยังไม่ได้อัปเกรดโปรเจ็กต์เป็นแพ็กเกจราคา Blaze แบบจ่ายตามการใช้งาน ให้คลิกอัปเกรดเพื่อดำเนินการ (ระบบจะแจ้งให้คุณอัปเกรดก็ต่อเมื่อโปรเจ็กต์ไม่ได้ใช้แพ็กเกจราคา Blaze)

      เฉพาะโปรเจ็กต์ในแพ็กเกจการเรียกเก็บเงิน Blaze เท่านั้นที่ใช้ API บนระบบคลาวด์ได้

    3. หากยังไม่ได้เปิดใช้ API บนระบบคลาวด์ ให้คลิก เปิดใช้ API บนระบบคลาวด์

กำหนดค่าเครื่องตรวจจับจุดสังเกต

โดยค่าเริ่มต้น ตัวตรวจจับระบบคลาวด์จะใช้โมเดลเวอร์ชันเสถียรและ แสดงผลลัพธ์ได้สูงสุด 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 เมื่อสร้างออบเจ็กต์เครื่องตรวจจับในระบบคลาวด์

เรียกใช้เครื่องตรวจจับจุดสังเกต

หากต้องการจดจำสถานที่สำคัญในรูปภาพ ให้ส่งรูปภาพเป็น UIImage หรือ CMSampleBufferRef ไปยังเมธอด VisionCloudLandmarkDetector ของ detect(in:)

  1. รับอินสแตนซ์ของ 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];
  2. หากต้องการเรียกใช้ Cloud Vision รูปภาพต้องจัดรูปแบบเป็นสตริงที่เข้ารหัสฐาน 64 วิธีประมวลผล 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];
  3. จากนั้นส่งรูปภาพไปยังเมธอด 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];
}

ขั้นตอนถัดไป