ตรวจหา ติดตาม และจัดประเภทออบเจ็กต์ด้วยโมเดลการแยกประเภทที่กำหนดเองใน iOS

คุณใช้ ML Kit เพื่อตรวจหาและติดตามวัตถุในเฟรมวิดีโอต่อเนื่องได้

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

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

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

โมเดลแบบกลุ่ม โมเดลที่โฮสต์
โมเดลนี้เป็นส่วนหนึ่งของไฟล์ .ipa ของแอป ทำให้ใหญ่ขึ้น โมเดลนี้ไม่ได้เป็นส่วนหนึ่งของไฟล์ .ipa ของแอป ใช่เลย โฮสต์โดยอัปโหลดไปยัง แมชชีนเลิร์นนิงของ Firebase
โมเดลดังกล่าวจะพร้อมใช้งานทันที แม้ว่าอุปกรณ์ Android จะออฟไลน์อยู่ โมเดลจะดาวน์โหลดแบบออนดีมานด์
ไม่จำเป็นต้องมีโปรเจ็กต์ Firebase ต้องมีโปรเจ็กต์ Firebase
คุณต้องเผยแพร่แอปอีกครั้งเพื่ออัปเดตโมเดล พุชการอัปเดตโมเดลโดยไม่เผยแพร่แอปซ้ำ
ไม่มีการทดสอบ A/B ในตัว การทดสอบ A/B ที่ง่ายดายด้วยการกำหนดค่าระยะไกลของ Firebase

ลองเลย

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

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

    สำหรับการรวมโมเดลกับแอป ให้ทำดังนี้

    pod 'GoogleMLKit/ObjectDetectionCustom', '3.2.0'
    

    สำหรับการดาวน์โหลดโมเดลแบบไดนามิกจาก Firebase ให้เพิ่ม LinkFirebase การพึ่งพา:

    pod 'GoogleMLKit/ObjectDetectionCustom', '3.2.0'
    pod 'GoogleMLKit/LinkFirebase', '3.2.0'
    
  2. หลังจากติดตั้งหรืออัปเดตพ็อดของโปรเจ็กต์แล้ว ให้เปิดโปรเจ็กต์ Xcode โดยใช้ .xcworkspace รองรับ ML Kit ใน Xcode เวอร์ชัน 13.2.1 หรือสูงกว่า

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

1. โหลดโมเดล

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

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

  1. คัดลอกไฟล์โมเดล (โดยปกติจะลงท้ายด้วย .tflite หรือ .lite) ไปยัง Xcode ให้เลือก Copy bundle resources เมื่อคุณดำเนินการดังกล่าว ไฟล์โมเดลจะรวมอยู่ใน App Bundle และพร้อมใช้งานสำหรับ ML Kit

  2. สร้างออบเจ็กต์ LocalModel โดยระบุเส้นทางไปยังไฟล์โมเดล

    Swift

    let localModel = LocalModel(path: localModelFilePath)

    Objective-C

    MLKLocalModel *localModel =
        [[MLKLocalModel alloc] initWithPath:localModelFilePath];

กำหนดค่าแหล่งที่มาของโมเดลที่โฮสต์กับ Firebase

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

Swift

let firebaseModelSource = FirebaseModelSource(
    name: "your_remote_model") // The name you assigned in
                               // the Firebase console.
let remoteModel = CustomRemoteModel(remoteModelSource: firebaseModelSource)

Objective-C

MLKFirebaseModelSource *firebaseModelSource =
    [[MLKFirebaseModelSource alloc]
        initWithName:@"your_remote_model"]; // The name you assigned in
                                            // the Firebase console.
MLKCustomRemoteModel *remoteModel =
    [[MLKCustomRemoteModel alloc]
        initWithRemoteModelSource:firebaseModelSource];

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

Swift

let downloadConditions = ModelDownloadConditions(
  allowsCellularAccess: true,
  allowsBackgroundDownloading: true
)

let downloadProgress = ModelManager.modelManager().download(
  remoteModel,
  conditions: downloadConditions
)

Objective-C

MLKModelDownloadConditions *downloadConditions =
    [[MLKModelDownloadConditions alloc] initWithAllowsCellularAccess:YES
                                         allowsBackgroundDownloading:YES];

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

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

2. กำหนดค่าตัวตรวจจับออบเจ็กต์

หลังจากคุณกำหนดค่าแหล่งที่มาของโมเดล ให้กำหนดค่าตัวตรวจจับออบเจ็กต์สำหรับ Use Case กับออบเจ็กต์ CustomObjectDetectorOptions คุณสามารถเปลี่ยน การตั้งค่าต่อไปนี้:

การตั้งค่าตัวตรวจจับวัตถุ
โหมดการตรวจจับ STREAM_MODE (ค่าเริ่มต้น) | วันที่ SINGLE_IMAGE_MODE

ใน STREAM_MODE (ค่าเริ่มต้น) ตัวตรวจจับวัตถุจะทำงาน โดยมีเวลาในการตอบสนองต่ำ แต่อาจให้ผลลัพธ์ที่ไม่สมบูรณ์ (เช่น กรอบล้อมรอบหรือป้ายกำกับหมวดหมู่ที่ไม่ระบุ) ใน 2-3 รายการแรก การเรียกใช้ตัวตรวจจับ และในอีก STREAM_MODE ตัวตรวจจับจะกำหนดรหัสติดตามให้กับออบเจ็กต์ ซึ่งใช้เพื่อ ติดตามออบเจ็กต์ในเฟรมต่างๆ ใช้โหมดนี้เมื่อคุณต้องการติดตาม หรือเมื่อเวลาในการตอบสนองต่ำมีความสำคัญ เช่น เมื่อประมวลผล สตรีมวิดีโอแบบเรียลไทม์

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

ตรวจหาและติดตามวัตถุหลายรายการ false (ค่าเริ่มต้น) | วันที่ true

สามารถตรวจจับและติดตามวัตถุได้สูงสุด 5 รายการ หรือเฉพาะวัตถุที่พบมากที่สุด ออบเจ็กต์ที่โดดเด่น (ค่าเริ่มต้น)

จำแนกประเภทวัตถุ false (ค่าเริ่มต้น) | วันที่ true

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

เกณฑ์ความเชื่อมั่นในการจัดประเภท

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

ป้ายกำกับสูงสุดต่อออบเจ็กต์

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

หากคุณมีเฉพาะโมเดลที่รวมในเครื่อง ให้สร้างตัวตรวจจับวัตถุจาก ออบเจ็กต์ LocalModel ของคุณ:

Swift

let options = CustomObjectDetectorOptions(localModel: localModel)
options.detectorMode = .singleImage
options.shouldEnableClassification = true
options.shouldEnableMultipleObjects = true
options.classificationConfidenceThreshold = NSNumber(value: 0.5)
options.maxPerObjectLabelCount = 3

Objective-C

MLKCustomObjectDetectorOptions *options =
    [[MLKCustomObjectDetectorOptions alloc] initWithLocalModel:localModel];
options.detectorMode = MLKObjectDetectorModeSingleImage;
options.shouldEnableClassification = YES;
options.shouldEnableMultipleObjects = YES;
options.classificationConfidenceThreshold = @(0.5);
options.maxPerObjectLabelCount = 3;

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

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

Swift

var options: CustomObjectDetectorOptions!
if (ModelManager.modelManager().isModelDownloaded(remoteModel)) {
  options = CustomObjectDetectorOptions(remoteModel: remoteModel)
} else {
  options = CustomObjectDetectorOptions(localModel: localModel)
}
options.detectorMode = .singleImage
options.shouldEnableClassification = true
options.shouldEnableMultipleObjects = true
options.classificationConfidenceThreshold = NSNumber(value: 0.5)
options.maxPerObjectLabelCount = 3

Objective-C

MLKCustomObjectDetectorOptions *options;
if ([[MLKModelManager modelManager] isModelDownloaded:remoteModel]) {
  options = [[MLKCustomObjectDetectorOptions alloc] initWithRemoteModel:remoteModel];
} else {
  options = [[MLKCustomObjectDetectorOptions alloc] initWithLocalModel:localModel];
}
options.detectorMode = MLKObjectDetectorModeSingleImage;
options.shouldEnableClassification = YES;
options.shouldEnableMultipleObjects = YES;
options.classificationConfidenceThreshold = @(0.5);
options.maxPerObjectLabelCount = 3;

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

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

Swift

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]
    // ...
}

Objective-C

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

API การติดตามและตรวจจับออบเจ็กต์ได้รับการเพิ่มประสิทธิภาพสำหรับการใช้งานหลัก 2 รายการนี้ กรณี:

  • การตรวจจับแบบเรียลไทม์และการติดตามวัตถุที่โดดเด่นที่สุดในกล้อง ช่องมองภาพ
  • การตรวจจับวัตถุหลายรายการจากภาพนิ่ง

วิธีกำหนดค่า API สำหรับกรณีการใช้งานเหล่านี้

Swift

// Live detection and tracking
let options = CustomObjectDetectorOptions(localModel: localModel)
options.shouldEnableClassification = true
options.maxPerObjectLabelCount = 3

// Multiple object detection in static images
let options = CustomObjectDetectorOptions(localModel: localModel)
options.detectorMode = .singleImage
options.shouldEnableMultipleObjects = true
options.shouldEnableClassification = true
options.maxPerObjectLabelCount = 3

Objective-C

// Live detection and tracking
MLKCustomObjectDetectorOptions *options =
    [[MLKCustomObjectDetectorOptions alloc] initWithLocalModel:localModel];
options.shouldEnableClassification = YES;
options.maxPerObjectLabelCount = 3;

// Multiple object detection in static images
MLKCustomObjectDetectorOptions *options =
    [[MLKCustomObjectDetectorOptions alloc] initWithLocalModel:localModel];
options.detectorMode = MLKObjectDetectorModeSingleImage;
options.shouldEnableMultipleObjects = YES;
options.shouldEnableClassification = YES;
options.maxPerObjectLabelCount = 3;

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

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

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

  • สร้างออบเจ็กต์ VisionImage ด้วย UIImage ตรวจสอบว่าได้ระบุ .orientation ที่ถูกต้อง

    Swift

    let image = VisionImage(image: UIImage)
    visionImage.orientation = image.imageOrientation

    Objective-C

    MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
    visionImage.orientation = image.imageOrientation;

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

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

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

    Swift

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

    Objective-C

    - (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 วัตถุและการวางแนว:

    Swift

    let image = VisionImage(buffer: sampleBuffer)
    image.orientation = imageOrientation(
      deviceOrientation: UIDevice.current.orientation,
      cameraPosition: cameraPosition)

    Objective-C

     MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer];
     image.orientation =
       [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                    cameraPosition:cameraPosition];

4. สร้างและเรียกใช้ตัวตรวจจับวัตถุ

  1. สร้างตัวตรวจจับวัตถุใหม่ โดยทำดังนี้

    Swift

    let objectDetector = ObjectDetector.objectDetector(options: options)

    Objective-C

    MLKObjectDetector *objectDetector = [MLKObjectDetector objectDetectorWithOptions:options];
  2. จากนั้นใช้ตัวตรวจจับโดยทำดังนี้

    ไม่พร้อมกัน:

    Swift

    objectDetector.process(image) { objects, error in
        guard error == nil, let objects = objects, !objects.isEmpty else {
            // Handle the error.
            return
        }
        // Show results.
    }

    Objective-C

    [objectDetector
        processImage:image
          completion:^(NSArray *_Nullable objects,
                       NSError *_Nullable error) {
            if (objects.count == 0) {
                // Handle the error.
                return;
            }
            // Show results.
         }];

    พร้อมกัน:

    Swift

    var objects: [Object]
    do {
        objects = try objectDetector.results(in: image)
    } catch let error {
        // Handle the error.
        return
    }
    // Show results.

    Objective-C

    NSError *error;
    NSArray *objects =
        [objectDetector resultsInImage:image error:&error];
    // Show results or handle the error.

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

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

Object แต่ละรายการจะมีพร็อพเพอร์ตี้ต่อไปนี้

frame CGRect ที่ระบุตำแหน่งของออบเจ็กต์ในส่วน รูปภาพ
trackingID จำนวนเต็มที่ระบุออบเจ็กต์ในรูปภาพ หรือ "nil" ใน SINGLE_IMAGE_mode
labels
label.text คำอธิบายข้อความของป้ายกำกับ แสดงผลเฉพาะเมื่อ TensorFlow ข้อมูลเมตาของโมเดล Lite มีคำอธิบายป้ายกำกับ
label.index ดัชนีของป้ายกำกับจากป้ายกำกับทั้งหมดที่ระบบรองรับ ตัวแยกประเภท
label.confidence ค่าความเชื่อมั่นของการจัดประเภทออบเจ็กต์

Swift

// objects contains one item if multiple object detection wasn't enabled.
for object in objects {
  let frame = object.frame
  let trackingID = object.trackingID
  let description = object.labels.enumerated().map { (index, label) in
    "Label \(index): \(label.text), \(label.confidence), \(label.index)"
  }.joined(separator: "\n")
}

Objective-C

// The list of detected objects contains one item if multiple object detection
// wasn't enabled.
for (MLKObject *object in objects) {
  CGRect frame = object.frame;
  NSNumber *trackingID = object.trackingID;
  for (MLKObjectLabel *label in object.labels) {
    NSString *labelString =
        [NSString stringWithFormat:@"%@, %f, %lu",
                                   label.text,
                                   label.confidence,
                                   (unsigned long)label.index];
  }
}

สร้างประสบการณ์ของผู้ใช้ที่ดี

โปรดปฏิบัติตามหลักเกณฑ์ต่อไปนี้ในแอปเพื่อให้ผู้ใช้ได้รับประสบการณ์ที่ดีที่สุด

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

นอกจากนี้ คุณยังดู [แอปแสดงดีไซน์ Material ของ ML Kit][showcase-link]{: .external} และ ดีไซน์ Material คอลเล็กชันรูปแบบของฟีเจอร์ที่ขับเคลื่อนด้วยแมชชีนเลิร์นนิง

Improving performance

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

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

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