Firebase is back at Google I/O on May 10! Register now

รู้จักสถานที่สำคัญด้วย Firebase ML บน iOS

จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ

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

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

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

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

    1. ใน Xcode เมื่อโปรเจ็กต์แอปของคุณเปิดอยู่ ให้ไปที่ File > Add Packages
    2. เมื่อได้รับแจ้ง ให้เพิ่มที่เก็บ Firebase Apple platforms SDK:
    3.   https://github.com/firebase/firebase-ios-sdk
    4. เลือกไลบรารี Firebase ML
    5. เมื่อเสร็จแล้ว 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 เมื่อคุณสร้างออบเจ็กต์ Cloud detector

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

ในการจดจำจุดสังเกตในรูปภาพ ให้ส่งรูปภาพเป็น UIImage หรือ CMSampleBufferRef ไปยังเมธอด detection 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.0f) 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];
}

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