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

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

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

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

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

    1. เปิดโปรเจ็กต์แอปใน Xcode แล้วไปที่ไฟล์ > เพิ่มแพ็กเกจ
    2. เมื่อได้รับข้อความแจ้ง ให้เพิ่มที่เก็บ SDK สำหรับแพลตฟอร์ม Firebase ของ Apple ดังนี้
    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

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

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

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

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

  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];
}

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