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

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

ก่อนที่คุณจะเริ่ม

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

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

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

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

    1. ในแอปของคุณ ให้นำเข้า Firebase:

      สวิฟท์

      import FirebaseMLModelDownloader

      วัตถุประสงค์-C

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

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

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

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

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

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

  1. รับอินสแตนซ์ของ 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];
    
  2. หากต้องการเรียกใช้ 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];
  3. จากนั้นส่งภาพไปยังวิธี 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];
}

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