ติดป้ายกำกับรูปภาพด้วยโมเดลที่ AutoML ฝึกบน iOS

หลังจากฝึกโมเดลของคุณเองโดยใช้ AutoML Vision Edge แล้ว คุณจะใช้โมเดลดังกล่าวในแอปเพื่อติดป้ายกำกับรูปภาพได้

การผสานรวมโมเดลที่ฝึกจาก AutoML Vision Edge ทำได้ 2 วิธี คุณสามารถรวมโมเดลโดยคัดลอกไฟล์ของโมเดลไปยังโปรเจ็กต์ Xcode หรือจะดาวน์โหลดแบบไดนามิกจาก Firebase ก็ได้

ตัวเลือกการรวมโมเดล
รวมอยู่ในแอปของคุณ
  • โมเดลเป็นส่วนหนึ่งของแพ็กเกจ
  • โมเดลจะพร้อมใช้งานทันที แม้ว่าอุปกรณ์ iOS จะออฟไลน์อยู่ก็ตาม
  • ไม่ต้องใช้โปรเจ็กต์ Firebase
โฮสต์ด้วย Firebase
  • โฮสต์โมเดลโดยการอัปโหลดไปยัง Firebase Machine Learning
  • ลดขนาด App Bundle
  • ระบบจะดาวน์โหลดโมเดลตามคําขอ
  • พุชการอัปเดตโมเดลโดยไม่ต้องเผยแพร่แอปอีกครั้ง
  • การทดสอบ A/B ที่ง่ายดายด้วยการกำหนดค่าระยะไกลของ Firebase
  • ต้องมีโปรเจ็กต์ Firebase

ลองเลย

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

1. รวมไลบรารี ML Kit ไว้ใน Podfile

สำหรับการรวมโมเดลกับแอป ให้ทำดังนี้
    pod 'GoogleMLKit/ImageLabelingAutoML'
    
หากต้องการดาวน์โหลดโมเดลจาก Firebase แบบไดนามิก ให้เพิ่ม LinkFirebase ดังนี้ dependency
    pod 'GoogleMLKit/ImageLabelingAutoML'
    pod 'GoogleMLKit/LinkFirebase'
    
2. หลังจากติดตั้งหรืออัปเดต Pods ของโปรเจ็กต์แล้ว ให้เปิดโปรเจ็กต์ Xcode โดยใช้ .xcworkspacecode> ของโปรเจ็กต์ Xcode รองรับ ML Kit เวอร์ชัน 13.2.1 ขึ้นไป 3. หากต้องการดาวน์โหลดโมเดล ให้ตรวจสอบว่าคุณได้เพิ่ม Firebase ลงในโปรเจ็กต์ iOS แล้ว หากยังไม่ได้ดำเนินการดังกล่าว ซึ่งไม่จําเป็นเมื่อคุณรวมโมเดล

1. โหลดโมเดล

กำหนดค่าแหล่งข้อมูลโมเดลในเครื่อง

วิธีรวมโมเดลกับแอป

1. แตกไฟล์โมเดลและข้อมูลเมตาออกจากไฟล์ ZIP ที่คุณดาวน์โหลดจากคอนโซล Firebase ลงในโฟลเดอร์
    your_model_directory
      |____dict.txt
      |____manifest.json
      |____model.tflite
    
ไฟล์ทั้ง 3 ไฟล์ต้องอยู่ในโฟลเดอร์เดียวกัน เราขอแนะนำให้คุณใช้ไฟล์ตามที่คุณดาวน์โหลดมาโดยไม่มีการแก้ไข (รวมถึงชื่อไฟล์)

2. คัดลอกโฟลเดอร์ไปยังโปรเจ็กต์ Xcode โดยอย่าลืมเลือกสร้างการอ้างอิงโฟลเดอร์เมื่อทำเช่นนั้น ไฟล์โมเดลและข้อมูลเมตาจะรวมอยู่ใน App Bundle และพร้อมใช้งานสำหรับ ML Kit

3. สร้างออบเจ็กต์ AutoMLImageLabelerLocalModel โดยระบุเส้นทางไปยังไฟล์ Manifest ของรูปแบบ
SwiftObjective-C
guard let manifestPath = Bundle.main.path(
    forResource: "manifest",
    ofType: "json",
    inDirectory: "your_model_directory"
) else { return }
let localModel = AutoMLImageLabelerLocalModel(manifestPath: manifestPath)
NSString *manifestPath =
    [NSBundle.mainBundle pathForResource:@"manifest"
                                  ofType:@"json"
                             inDirectory:@"your_model_directory"];
MLKAutoMLImageLabelerLocalModel *localModel =
    [[MLKAutoMLImageLabelerLocalModel alloc] initWithManifestPath:manifestPath];

กำหนดค่าแหล่งข้อมูลรูปแบบที่โฮสต์ใน Firebase

หากต้องการใช้รูปแบบที่โฮสต์จากระยะไกล ให้สร้างออบเจ็กต์ AutoMLImageLabelerRemoteModel โดยระบุชื่อที่คุณกำหนดให้กับรูปแบบเมื่อเผยแพร่

SwiftObjective-C
let remoteModel = AutoMLImageLabelerRemoteModel(
    name: "your_remote_model"  // The name you assigned in
                               // the Firebase console.
)
MLKAutoMLImageLabelerRemoteModel *remoteModel =
    [[MLKAutoMLImageLabelerRemoteModel alloc]
        initWithName:@"your_remote_model"];  // The name you assigned in
                                             // the Firebase console.

จากนั้นเริ่มงานการดาวน์โหลดโมเดลโดยระบุเงื่อนไขที่คุณต้องการอนุญาตให้ดาวน์โหลด หากโมเดลไม่อยู่ในอุปกรณ์ หรือหากมีโมเดลเวอร์ชันใหม่กว่า แท็บจะดาวน์โหลดโมเดลจาก Firebase แบบไม่พร้อมกัน โดยทำดังนี้

SwiftObjective-C
let downloadConditions = ModelDownloadConditions(
  allowsCellularAccess: true,
  allowsBackgroundDownloading: true
)

let downloadProgress = ModelManager.modelManager().download(
  remoteModel,
  conditions: downloadConditions
)
MLKModelDownloadConditions *downloadConditions =
    [[MLKModelDownloadConditions alloc] initWithAllowsCellularAccess:YES
                                         allowsBackgroundDownloading:YES];

NSProgress *downloadProgress =
    [[MLKModelManager modelManager] downloadModel:remoteModel
                                       conditions:downloadConditions];

แอปจํานวนมากจะเริ่มการดาวน์โหลดในโค้ดเริ่มต้น แต่คุณก็เริ่มได้ทุกเมื่อก่อนที่จะต้องใช้โมเดล

สร้างเครื่องมือติดป้ายกำกับรูปภาพจากโมเดล

หลังจากกําหนดค่าแหล่งที่มาของโมเดลแล้ว ให้สร้างออบเจ็กต์ ImageLabeler จากแหล่งที่มาแหล่งใดแหล่งหนึ่ง

หากมีเพียงโมเดลที่รวมอยู่ในเครื่อง ให้สร้างเครื่องติดป้ายกำกับจากออบเจ็กต์ AutoMLImageLabelerLocalModel แล้วกำหนดค่าเกณฑ์คะแนนความเชื่อมั่นที่ต้องการ (ดูประเมินโมเดล

SwiftObjective-C
let options = AutoMLImageLabelerOptions(localModel: localModel)
options.confidenceThreshold = NSNumber(value: 0.0)  // Evaluate your model in the Firebase console
                                                    // to determine an appropriate value.
let imageLabeler = ImageLabeler.imageLabeler(options: options)
MLKAutoMLImageLabelerOptions *options =
    [[MLKAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel];
options.confidenceThreshold = @(0.0);  // Evaluate your model in the Firebase console
                                       // to determine an appropriate value.
MLKImageLabeler *imageLabeler =
    [MLKImageLabeler imageLabelerWithOptions:options];

หากมีโมเดลที่โฮสต์จากระยะไกล คุณจะต้องตรวจสอบว่าได้ดาวน์โหลดโมเดลแล้วก่อนที่จะเรียกใช้ คุณสามารถตรวจสอบสถานะของงานดาวน์โหลดโมเดลได้โดยใช้isModelDownloaded(remoteModel:) ของเครื่องมือจัดการโมเดล

แม้ว่าคุณจะต้องยืนยันข้อมูลนี้ก่อนเรียกใช้โปรแกรมติดป้ายกำกับ แต่หากมีทั้งโมเดลที่โฮสต์จากระยะไกลและโมเดลที่รวมอยู่ในเครื่อง ก็อาจต้องทำการตรวจสอบนี้เมื่อสร้างอินสแตนซ์ ImageLabeler: สร้างโปรแกรมติดป้ายกำกับจากโมเดลระยะไกลหากมีการดาวน์โหลดไว้ และจากโมเดลในเครื่องหากไม่ได้ดาวน์โหลด

SwiftObjective-C
var options: AutoMLImageLabelerOptions!
if (ModelManager.modelManager().isModelDownloaded(remoteModel)) {
  options = AutoMLImageLabelerOptions(remoteModel: remoteModel)
} else {
  options = AutoMLImageLabelerOptions(localModel: localModel)
}
options.confidenceThreshold = NSNumber(value: 0.0)  // Evaluate your model in the Firebase console
                                                    // to determine an appropriate value.
let imageLabeler = ImageLabeler.imageLabeler(options: options)
MLKAutoMLImageLabelerOptions *options;
if ([[MLKModelManager modelManager] isModelDownloaded:remoteModel]) {
  options = [[MLKAutoMLImageLabelerOptions alloc] initWithRemoteModel:remoteModel];
} else {
  options = [[MLKAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel];
}
options.confidenceThreshold = @(0.0);  // Evaluate your model in the Firebase console
                                       // to determine an appropriate value.
MLKImageLabeler *imageLabeler =
    [MLKImageLabeler imageLabelerWithOptions:options];

หากมีเพียงโมเดลที่โฮสต์ระยะไกล คุณควรปิดใช้ฟังก์ชันการทำงานที่เกี่ยวข้องกับโมเดล เช่น ปิดใช้หรือซ่อน UI บางส่วน จนกว่าคุณจะยืนยันว่าดาวน์โหลดโมเดลแล้ว

คุณดูสถานะการดาวน์โหลดโมเดลได้โดยแนบผู้สังเกตการณ์กับศูนย์การแจ้งเตือนเริ่มต้น โปรดใช้การอ้างอิงแบบอ่อนกับ self ในบล็อกการสังเกตการณ์ เนื่องจากการดาวน์โหลดอาจใช้เวลาสักครู่ และระบบอาจเพิ่มพื้นที่ว่างสำหรับวัตถุต้นทางได้เมื่อการดาวน์โหลดเสร็จสิ้น เช่น

SwiftObjective-C
NotificationCenter.default.addObserver(
    forName: .mlkitModelDownloadDidSucceed,
    object: nil,
    queue: nil
) { [weak self] notification in
    guard let strongSelf = self,
        let userInfo = notification.userInfo,
        let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue]
            as? RemoteModel,
        model.name == "your_remote_model"
        else { return }
    // The model was downloaded and is available on the device
}

NotificationCenter.default.addObserver(
    forName: .mlkitModelDownloadDidFail,
    object: nil,
    queue: nil
) { [weak self] notification in
    guard let strongSelf = self,
        let userInfo = notification.userInfo,
        let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue]
            as? RemoteModel
        else { return }
    let error = userInfo[ModelDownloadUserInfoKey.error.rawValue]
    // ...
}
__weak typeof(self) weakSelf = self;

[NSNotificationCenter.defaultCenter
    addObserverForName:MLKModelDownloadDidSucceedNotification
                object:nil
                 queue:nil
            usingBlock:^(NSNotification *_Nonnull note) {
              if (weakSelf == nil | note.userInfo == nil) {
                return;
              }
              __strong typeof(self) strongSelf = weakSelf;

              MLKRemoteModel *model = note.userInfo[MLKModelDownloadUserInfoKeyRemoteModel];
              if ([model.name isEqualToString:@"your_remote_model"]) {
                // The model was downloaded and is available on the device
              }
            }];

[NSNotificationCenter.defaultCenter
    addObserverForName:MLKModelDownloadDidFailNotification
                object:nil
                 queue:nil
            usingBlock:^(NSNotification *_Nonnull note) {
              if (weakSelf == nil | note.userInfo == nil) {
                return;
              }
              __strong typeof(self) strongSelf = weakSelf;

              NSError *error = note.userInfo[MLKModelDownloadUserInfoKeyError];
            }];

2. เตรียมรูปภาพอินพุต

สร้างออบเจ็กต์ VisionImage โดยใช้ UIImage หรือ CMSampleBuffer

หากคุณใช้ UIImage ให้ทำตามขั้นตอนต่อไปนี้

  • สร้างออบเจ็กต์ VisionImage ด้วย UIImage ตรวจสอบว่าได้ระบุ .orientation ที่ถูกต้อง
    SwiftObjective-C
    let image = VisionImage(image: UIImage)
    visionImage.orientation = image.imageOrientation
    MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
    visionImage.orientation = image.imageOrientation;

หากคุณใช้ CMSampleBuffer ให้ทำตามขั้นตอนต่อไปนี้

  • ระบุการวางแนวของข้อมูลรูปภาพที่อยู่ใน CMSampleBuffer

    วิธีดูการวางแนวรูปภาพ

    SwiftObjective-C
    func imageOrientation(
      deviceOrientation: UIDeviceOrientation,
      cameraPosition: AVCaptureDevice.Position
    ) -> UIImage.Orientation {
      switch deviceOrientation {
      case .portrait:
        return cameraPosition == .front ? .leftMirrored : .right
      case .landscapeLeft:
        return cameraPosition == .front ? .downMirrored : .up
      case .portraitUpsideDown:
        return cameraPosition == .front ? .rightMirrored : .left
      case .landscapeRight:
        return cameraPosition == .front ? .upMirrored : .down
      case .faceDown, .faceUp, .unknown:
        return .up
      }
    }
          
    - (UIImageOrientation)
      imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation
                             cameraPosition:(AVCaptureDevicePosition)cameraPosition {
      switch (deviceOrientation) {
        case UIDeviceOrientationPortrait:
          return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored
                                                                : UIImageOrientationRight;
    
        case UIDeviceOrientationLandscapeLeft:
          return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored
                                                                : UIImageOrientationUp;
        case UIDeviceOrientationPortraitUpsideDown:
          return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored
                                                                : UIImageOrientationLeft;
        case UIDeviceOrientationLandscapeRight:
          return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored
                                                                : UIImageOrientationDown;
        case UIDeviceOrientationUnknown:
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationFaceDown:
          return UIImageOrientationUp;
      }
    }
          
  • สร้างออบเจ็กต์ VisionImage โดยใช้ออบเจ็กต์ CMSampleBuffer และการวางแนว
    SwiftObjective-C
    let image = VisionImage(buffer: sampleBuffer)
    image.orientation = imageOrientation(
      deviceOrientation: UIDevice.current.orientation,
      cameraPosition: cameraPosition)
     MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer];
     image.orientation =
       [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                    cameraPosition:cameraPosition];

3. เรียกใช้โปรแกรมติดป้ายกำกับรูปภาพ

แบบไม่พร้อมกัน

SwiftObjective-C
imageLabeler.process(image) { labels, error in
    guard error == nil, let labels = labels, !labels.isEmpty else {
        // Handle the error.
        return
    }
    // Show results.
}
[imageLabeler
    processImage:image
      completion:^(NSArray *_Nullable labels,
                   NSError *_Nullable error) {
        if (labels.count == 0) {
            // Handle the error.
            return;
        }
        // Show results.
     }];

แบบซิงโครนัส

SwiftObjective-C
var labels: [ImageLabel]
do {
    labels = try imageLabeler.results(in: image)
} catch let error {
    // Handle the error.
    return
}
// Show results.
NSError *error;
NSArray *labels =
    [imageLabeler resultsInImage:image error:&error];
// Show results or handle the error.

4. ดูข้อมูลเกี่ยวกับวัตถุที่ติดป้ายกำกับ

หากการดำเนินการติดป้ายกำกับรูปภาพสำเร็จ ระบบจะแสดงผลอาร์เรย์ของ ImageLabel แต่ละ ImageLabel แสดงถึงสิ่งที่ติดป้ายกำกับในรูปภาพ คุณสามารถดูคำอธิบายข้อความของป้ายกำกับแต่ละรายการ (หากมีในข้อมูลเมตาของไฟล์โมเดล TensorFlow Lite) คะแนนความเชื่อมั่น และดัชนี เช่น
SwiftObjective-C
for label in labels {
  let labelText = label.text
  let confidence = label.confidence
  let index = label.index
}
for (MLKImageLabel *label in labels) {
  NSString *labelText = label.text;
  float confidence = label.confidence;
  NSInteger index = label.index;
}

เคล็ดลับในการปรับปรุงประสิทธิภาพแบบเรียลไทม์

หากต้องการติดป้ายกำกับรูปภาพในแอปพลิเคชันแบบเรียลไทม์ ให้ทำตามหลักเกณฑ์ต่อไปนี้เพื่อให้ได้อัตราเฟรมที่ดีที่สุด

  • สําหรับการประมวลผลเฟรมวิดีโอ ให้ใช้ results(in:) synchronous API ของตัวตรวจจับ เรียกใช้วิธีนี้จากฟังก์ชัน AVCaptureVideoDataOutputSampleBufferDelegate's captureOutput(_, didOutput:from:) เพื่อรับผลลัพธ์จากเฟรมวิดีโอที่ระบุแบบซิงค์ คง alwaysDiscardsLateVideoFrames ของ AVCaptureVideoDataOutput เป็น true เพื่อจำกัดการเรียกใช้เครื่องตรวจจับ หากเฟรมวิดีโอใหม่พร้อมใช้งานขณะที่ตัวตรวจจับทำงานอยู่ ระบบจะทิ้งเฟรมนั้น
  • หากคุณใช้เอาต์พุตของตัวตรวจจับเพื่อวางกราฟิกซ้อนทับบนรูปภาพอินพุต ให้รับผลลัพธ์จาก ML Kit ก่อน จากนั้นจึงแสดงผลรูปภาพและวางซ้อนในขั้นตอนเดียว ซึ่งจะทำให้คุณแสดงผลไปยังพื้นผิวการแสดงผลเพียงครั้งเดียวสำหรับเฟรมอินพุตที่ประมวลผลแต่ละเฟรม ดูตัวอย่างได้ที่ updatePreviewOverlayViewWithLastFrame ในตัวอย่างการเริ่มต้นใช้งานอย่างรวดเร็วของ ML Kit