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

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

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

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

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

โมเดลที่ให้มาด้วย โมเดลที่โฮสต์
โมเดลนี้เป็นส่วนหนึ่งของไฟล์ .ipa ของแอป ซึ่งจะเพิ่มขนาด โมเดลนี้ไม่ได้เป็นส่วนหนึ่งของไฟล์ .ipa ของแอป โดยโฮสต์โดยการอัปโหลดไปยัง Firebase Machine Learning
โมเดลนี้จะพร้อมใช้งานทันทีแม้ว่าอุปกรณ์ 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 ของโปรเจ็กต์ Xcode เวอร์ชัน 13.2.1 ขึ้นไปรองรับ ML Kit

  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 } และคอลเล็กชัน รูปแบบสำหรับฟีเจอร์ที่ขับเคลื่อนโดยแมชชีนเลิร์นนิง

Improving performance

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

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

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