คุณใช้ ML Kit เพื่อจดจําเอนทิตีในรูปภาพและติดป้ายกํากับได้ API นี้รองรับโมเดลการแยกประเภทรูปภาพที่กําหนดเองที่หลากหลาย โปรดดูโมเดลที่กําหนดเองที่มี ML Kit เพื่อดูคําแนะนําเกี่ยวกับข้อกําหนดความเข้ากันได้ของโมเดล วิธีค้นหาโมเดลที่ฝึกล่วงหน้า และวิธีฝึกโมเดลของคุณเอง
คุณผสานรวมรูปแบบที่กําหนดเองได้ 2 วิธี คุณสามารถรวมโมเดลโดยวางไว้ในโฟลเดอร์ชิ้นงานของแอป หรือจะดาวน์โหลดจาก Firebase แบบไดนามิกก็ได้ ตารางต่อไปนี้จะเปรียบเทียบ 2 ตัวเลือก
รุ่นที่รวมอยู่ในแพ็กเกจ | โมเดลที่โฮสต์ |
---|---|
โมเดลนี้เป็นส่วนหนึ่งของ APK ของแอปซึ่งจะเพิ่มขนาด | โมเดลไม่ได้เป็นส่วนหนึ่งของ APK ซึ่งโฮสต์โดยการอัปโหลดไปยัง Firebase Machine Learning |
โมเดลจะพร้อมใช้งานทันทีแม้อุปกรณ์ Android ออฟไลน์อยู่ | ดาวน์โหลดโมเดลตามคําขอแล้ว |
ไม่จําเป็นต้องมีโปรเจ็กต์ Firebase | ต้องใช้โปรเจ็กต์ Firebase |
คุณต้องเผยแพร่แอปอีกครั้งเพื่ออัปเดตรูปแบบ | ติดตั้งการอัปเดตโมเดลโดยไม่ต้องเผยแพร่แอปอีกครั้ง |
ไม่มีการทดสอบ A/B ในตัว | ทดสอบ A/B ได้ง่ายๆ ด้วยการกําหนดค่าระยะไกลของ Firebase |
ลองใช้งาน
- ดูตัวอย่างการใช้งานโมเดลแบบกลุ่มได้ในแอปคู่มือเริ่มใช้งานฉบับย่อ และแอปคู่มือเริ่มใช้งานฉบับย่ออัตโนมัติสําหรับการใช้งานตัวอย่าง
ข้อควรทราบก่อนที่จะเริ่มต้น
รวมไลบรารี ML Kit ใน Podfile ดังนี้
สําหรับกลุ่มแพ็กเกจกับแอปของคุณ ให้ทําดังนี้
pod 'GoogleMLKit/ImageLabelingCustom', '3.2.0'
สําหรับการดาวน์โหลดโมเดลแบบไดนามิกจาก Firebase ให้เพิ่มการอ้างอิง
LinkFirebase
pod 'GoogleMLKit/ImageLabelingCustom', '3.2.0' pod 'GoogleMLKit/LinkFirebase', '3.2.0'
หลังจากติดตั้งหรืออัปเดตพ็อดของโปรเจ็กต์แล้ว ให้เปิดโปรเจ็กต์ Xcode โดยใช้
.xcworkspace
XKit เวอร์ชัน 13.2.1 ขึ้นไปรองรับ ML Kitหากต้องการดาวน์โหลดโมเดล โปรดตรวจสอบว่าได้เพิ่ม Firebase ลงในโปรเจ็กต์ iOS หากยังไม่ได้ทํา ซึ่งไม่จําเป็นเมื่อคุณรวมโมเดล
1. โหลดโมเดล
กําหนดค่าแหล่งที่มาของโมเดลในเครื่อง
วิธีรวมโมเดลกับแอป
คัดลอกไฟล์โมเดล (โดยปกติจะลงท้ายด้วย
.tflite
หรือ.lite
) ไปยังโปรเจ็กต์ Xcode โดยระมัดระวังเพื่อเลือกCopy bundle resources
เมื่อคุณดําเนินการดังกล่าว ไฟล์โมเดลจะรวมอยู่ใน App Bundle และพร้อมใช้งานสําหรับ ML Kitสร้างออบเจ็กต์
LocalModel
โดยระบุเส้นทางไปยังไฟล์โมเดลSwift
let localModel = LocalModel(path: localModelFilePath)
Objective-C
MLKLocalModel *localModel = [[MLKLocalModel alloc] initWithPath:localModelFilePath];
กําหนดค่าแหล่งที่มาของโมเดลที่โฮสต์ใน Firebase
หากต้องการใช้โมเดลที่โฮสต์จากระยะไกล ให้สร้างออบเจ็กต์ RemoteModel
โดยระบุชื่อที่คุณกําหนดโมเดลเมื่อเผยแพร่
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];
แอปจํานวนมากเริ่มงานดาวน์โหลดในโค้ดเริ่มต้น แต่คุณสามารถทําได้ทุกเมื่อก่อนที่จะต้องใช้โมเดล
กําหนดค่าผู้ติดป้ายกํากับรูปภาพ
หลังจากที่กําหนดค่าแหล่งที่มาของโมเดลแล้ว ให้สร้างออบเจ็กต์ ImageLabeler
จากหนึ่งในออบเจ็กต์เหล่านั้น
โดยมีตัวเลือกดังต่อไปนี้
ตัวเลือก | |
---|---|
confidenceThreshold
|
คะแนนความเชื่อมั่นขั้นต่ําสําหรับป้ายกํากับที่ตรวจพบ หากไม่ได้ตั้งค่า ระบบจะใช้เกณฑ์ตัวแยกประเภทที่ระบุโดยข้อมูลเมตาของโมเดล หากโมเดลไม่มีข้อมูลเมตาหรือข้อมูลเมตาไม่ได้ระบุเกณฑ์ของตัวแยกประเภท ระบบจะใช้เกณฑ์เริ่มต้นซึ่งก็คือ 0.0 |
maxResultCount
|
จํานวนป้ายกํากับสูงสุดที่จะแสดง หากไม่ได้ตั้งค่า ระบบจะใช้ค่าเริ่มต้นของ 10 |
หากมีเฉพาะโมเดลที่จัดกลุ่มไว้ในเครื่อง เพียงสร้างผู้ติดป้ายกํากับจากออบเจ็กต์ LocalModel
ดังนี้
Swift
let options = CustomImageLabelerOptions(localModel: localModel) options.confidenceThreshold = NSNumber(value: 0.0) let imageLabeler = ImageLabeler.imageLabeler(options: options)
Objective-C
MLKCustomImageLabelerOptions *options = [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel]; options.confidenceThreshold = @(0.0); MLKImageLabeler *imageLabeler = [MLKImageLabeler imageLabelerWithOptions:options];
หากมีโมเดลที่โฮสต์จากระยะไกล คุณจะต้องตรวจสอบว่าโมเดลนั้นดาวน์โหลดแล้วก่อนที่จะเรียกใช้ คุณตรวจสอบสถานะของงานดาวน์โหลดโมเดลได้โดยใช้เมธอด isModelDownloaded(remoteModel:)
ของผู้จัดการโมเดล
แม้ว่าคุณต้องยืนยันนี้เฉพาะเมื่อเรียกใช้ผู้ติดป้ายกํากับ แต่หากคุณมีทั้งโมเดลที่โฮสต์จากระยะไกลและโมเดลแบบกลุ่มในพื้นที่ การตรวจสอบนี้อาจทําได้ทันทีเมื่อสร้างอินสแตนซ์ ImageLabeler
โดยสร้างผู้ติดป้ายกํากับจากโมเดลระยะไกลหากได้ดาวน์โหลดมาจากโมเดลในเครื่อง
Swift
var options: CustomImageLabelerOptions! if (ModelManager.modelManager().isModelDownloaded(remoteModel)) { options = CustomImageLabelerOptions(remoteModel: remoteModel) } else { options = CustomImageLabelerOptions(localModel: localModel) } options.confidenceThreshold = NSNumber(value: 0.0) let imageLabeler = ImageLabeler.imageLabeler(options: options)
Objective-C
MLKCustomImageLabelerOptions *options; if ([[MLKModelManager modelManager] isModelDownloaded:remoteModel]) { options = [[MLKCustomImageLabelerOptions alloc] initWithRemoteModel:remoteModel]; } else { options = [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel]; } options.confidenceThreshold = @(0.0); MLKImageLabeler *imageLabeler = [MLKImageLabeler imageLabelerWithOptions:options];
หากคุณมีเฉพาะโมเดลที่โฮสต์จากระยะไกล คุณควรปิดใช้ฟังก์ชันที่เกี่ยวข้องกับโมเดล เช่น เป็นสีเทาหรือซ่อนส่วนของ 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]; }];
2. เตรียมรูปภาพอินพุต
สร้างออบเจ็กต์ 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];
3. เรียกใช้ผู้ติดป้ายกํากับรูปภาพ
หากต้องการติดป้ายกํากับออบเจ็กต์ในรูปภาพ ให้ส่งออบเจ็กต์ image
ไปยังเมธอด process()
ของ ImageLabeler
ไม่พร้อมกัน:
Swift
imageLabeler.process(image) { labels, error in guard error == nil, let labels = labels, !labels.isEmpty else { // Handle the error. return } // Show results. }
Objective-C
[imageLabeler processImage:image completion:^(NSArray*_Nullable labels, NSError *_Nullable error) { if (label.count == 0) { // Handle the error. return; } // Show results. }];
พร้อมกัน:
Swift
var labels: [ImageLabel] do { labels = try imageLabeler.results(in: image) } catch let error { // Handle the error. return } // Show results.
Objective-C
NSError *error; NSArray*labels = [imageLabeler resultsInImage:image error:&error]; // Show results or handle the error.
4. ดูข้อมูลเกี่ยวกับเอนทิตีที่ติดป้ายกํากับ
หากการติดป้ายกํากับรูปภาพสําเร็จ ระบบจะแสดงอาร์เรย์ของImageLabel
ImageLabel
แต่ละรายการแสดงถึงสิ่งที่ติดป้ายกํากับไว้ในรูปภาพ คุณจะดูคําอธิบายข้อความของป้ายกํากับแต่ละรายการได้ (หากมีในข้อมูลเมตาของไฟล์โมเดล TensorFlow Lite) คะแนนความเชื่อมั่น และดัชนี
เช่น
Swift
for label in labels { let labelText = label.text let confidence = label.confidence let index = label.index }
Objective-C
for (MLKImageLabel *label in labels) { NSString *labelText = label.text; float confidence = label.confidence; NSInteger index = label.index; }
เคล็ดลับในการปรับปรุงประสิทธิภาพแบบเรียลไทม์
หากต้องการติดป้ายกํากับรูปภาพในแอปพลิเคชันแบบเรียลไทม์ ให้ทําตามหลักเกณฑ์ต่อไปนี้เพื่อให้อัตราเฟรมดีที่สุด
- สําหรับการประมวลผลเฟรมวิดีโอ ให้ใช้
results(in:)
API แบบพร้อมกันของตัวตรวจจับ เรียกวิธีนี้จากฟังก์ชันcaptureOutput(_, didOutput:from:)
ของAVCaptureVideoDataOutputSampleBufferDelegate
เพื่อให้ได้ผลลัพธ์พร้อมกันจากเฟรมวิดีโอที่ระบุ เก็บalwaysDiscardsLateVideoFrames
ของAVCaptureVideoDataOutput
ไว้เป็นtrue
เพื่อควบคุมการโทรไปยังตัวตรวจจับ หาก เฟรมวิดีโอใหม่พร้อมใช้งานระหว่างตัวตรวจจับจะทํางาน เฟรมจะหายไป - หากใช้เอาต์พุตของตัวตรวจจับเพื่อวางซ้อนกราฟิกบนรูปภาพอินพุต ก่อนอื่นให้ดูผลลัพธ์จาก ML Kit จากนั้นแสดงผลรูปภาพและวางซ้อนในขั้นตอนเดียว เมื่อทําเช่นนั้น คุณจะแสดงผลบนพื้นที่แสดงผลเพียงครั้งเดียวสําหรับเฟรมอินพุตแต่ละรายการที่ประมวลผลแล้ว ดูตัวอย่าง update PreviewOverlayViewWithLastFrame ในตัวอย่างคู่มือเริ่มใช้งานฉบับย่อสําหรับ ML Kit