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

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

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

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

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

    1. เปิดโปรเจ็กต์แอปใน Xcode แล้วไปที่ไฟล์ > เพิ่มแพ็กเกจ
    2. เมื่อได้รับข้อความแจ้ง ให้เพิ่มที่เก็บ Firebase SDK สําหรับแพลตฟอร์ม Apple ดังนี้
    3.   https://github.com/firebase/firebase-ios-sdk.git
    4. เลือกคลัง Firebase ML
    5. เพิ่ม Flag -ObjC ลงในส่วน Other Linker Flags ของการตั้งค่าบิลด์เป้าหมาย
    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 เมื่อสร้างออบเจ็กต์เครื่องตรวจจับของ Cloud

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

หากต้องการจดจำจุดสังเกตในรูปภาพ ให้ส่งรูปภาพเป็น 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. รูปภาพต้องอยู่ในรูปแบบสตริงที่เข้ารหัส 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];
  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];
}

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