ติดป้ายกำกับรูปภาพด้วยโมเดลที่ฝึกด้วย AutoML ใน iOS
หลังจากที่คุณฝึกโมเดลของคุณเองโดยใช้ AutoML Vision Edge คุณจะใช้โมเดลดังกล่าวในแอปเพื่อติดป้ายกำกับรูปภาพได้
การผสานรวมโมเดลที่ฝึกจาก AutoML Vision Edge ทำได้ 2 วิธี คุณสามารถรวมโมเดลโดยคัดลอกไฟล์ของโมเดลลงในโปรเจ็กต์ Xcode หรือดาวน์โหลดโมเดลแบบไดนามิกจาก Firebase ก็ได้
| ตัวเลือกการรวมโมเดล | |
|---|---|
| รวมไว้ในแอป |
|
| โฮสต์ด้วย Firebase |
|
ลองเลย
- ลองใช้แอปตัวอย่างเพื่อ ดูตัวอย่างการใช้งาน API นี้
ก่อนเริ่มต้น
1. รวมไลบรารี ML Kit ไว้ใน Podfile โดยทำดังนี้:สำหรับการรวมโมเดลกับแอป
pod 'GoogleMLKit/ImageLabelingAutoML'
LinkFirebase
โดยทำดังนี้
pod 'GoogleMLKit/ImageLabelingAutoML'
pod 'GoogleMLKit/LinkFirebase'
.xcworkspacecode> ML Kit รองรับใน Xcode
เวอร์ชัน 13.2.1 ขึ้นไป
3. หากต้องการดาวน์โหลดโมเดล ให้ตรวจสอบว่าคุณได้
เพิ่ม Firebase ลงในโปรเจ็กต์ iOS แล้ว
(หากยังไม่ได้ดำเนินการ) ซึ่งไม่จำเป็นเมื่อคุณรวม
โมเดล
1. โหลดโมเดล
กำหนดค่าแหล่งที่มาของโมเดลในเครื่อง
หากต้องการรวมโมเดลกับแอป ให้ทำดังนี้:1. แยกโมเดลและข้อมูลเมตาออกจากไฟล์เก็บถาวร zip ที่คุณดาวน์โหลด จากคอนโซล Firebase ลงในโฟลเดอร์โดยทำดังนี้
your_model_directory
|____dict.txt
|____manifest.json
|____model.tflite
2. คัดลอกโฟลเดอร์ไปยังโปรเจ็กต์ Xcode โดยตรวจสอบว่าได้เลือก สร้างการอ้างอิงโฟลเดอร์ เมื่อดำเนินการ ไฟล์โมเดลและข้อมูลเมตา จะรวมอยู่ใน App Bundle และพร้อมใช้งานสำหรับ ML Kit
3. สร้างออบเจ็กต์
AutoMLImageLabelerLocalModel โดยระบุเส้นทางไปยังไฟล์ Manifest ของโมเดลโดยทำดังนี้
Swift
guard let manifestPath = Bundle.main.path( forResource: "manifest", ofType: "json", inDirectory: "your_model_directory" ) else { return } let localModel = AutoMLImageLabelerLocalModel(manifestPath: manifestPath)
Objective-C
NSString *manifestPath = [NSBundle.mainBundle pathForResource:@"manifest" ofType:@"json" inDirectory:@"your_model_directory"]; MLKAutoMLImageLabelerLocalModel *localModel = [[MLKAutoMLImageLabelerLocalModel alloc] initWithManifestPath:manifestPath];
กำหนดค่าแหล่งที่มาของโมเดลที่โฮสต์ด้วย Firebase
หากต้องการใช้โมเดลที่โฮสต์จากระยะไกล ให้สร้างออบเจ็กต์ AutoMLImageLabelerRemoteModel โดยระบุชื่อที่คุณกำหนดให้กับโมเดลเมื่อเผยแพร่
Swift
let remoteModel = AutoMLImageLabelerRemoteModel( name: "your_remote_model" // The name you assigned in // the Firebase console. )
Objective-C
MLKAutoMLImageLabelerRemoteModel *remoteModel = [[MLKAutoMLImageLabelerRemoteModel alloc] initWithName:@"your_remote_model"]; // The name you assigned in // the Firebase console.
จากนั้นเริ่มงานดาวน์โหลดโมเดลโดยระบุเงื่อนไขที่คุณต้องการอนุญาตให้ดาวน์โหลด หากโมเดลไม่อยู่ในอุปกรณ์หรืองานดาวน์โหลดโมเดลเวอร์ชันใหม่กว่าพร้อมใช้งาน งานจะดาวน์โหลดโมเดลจาก 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 จากแหล่งที่มาใดแหล่งที่มาหนึ่ง
หากคุณมีเพียงโมเดลที่รวมไว้ในเครื่อง ให้สร้างเครื่องมือติดป้ายกำกับจากออบเจ็กต์
AutoMLImageLabelerLocalModel และกำหนดค่าเกณฑ์คะแนนความเชื่อมั่น
ที่คุณต้องการ (ดู ประเมินโหมด)
Swift
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)
Objective-C
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 อาจสมเหตุสมผลกว่า โดยสร้างเครื่องมือติดป้ายกำกับจากโมเดลระยะไกลหากดาวน์โหลดแล้ว และจากโมเดลในเครื่องหากยังไม่ได้ดาวน์โหลด
Swift
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)
Objective-C
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 เป็นสีเทาหรือซ่อนไว้ จนกว่าจะยืนยันว่าได้ดาวน์โหลดโมเดลแล้ว
คุณสามารถดูสถานะการดาวน์โหลดโมเดลได้โดยแนบผู้สังเกตการณ์ไปยัง Notification Center เริ่มต้น โปรดใช้การอ้างอิงแบบอ่อนไปยัง 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. เรียกใช้เครื่องมือติดป้ายกำกับรูปภาพ
แบบไม่พร้อมกัน
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 (labels.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; }
เคล็ดลับในการปรับปรุงประสิทธิภาพแบบเรียลไทม์
หากต้องการติดป้ายกำกับรูปภาพในแอปพลิเคชันแบบเรียลไทม์ ให้ทำตามหลักเกณฑ์ต่อไปนี้ เพื่อให้ได้อัตราเฟรมที่ดีที่สุด
- หากต้องการประมวลผลเฟรมวิดีโอ ให้ใช้ API แบบพร้อมกัน
results(in:)ของเครื่องมือตรวจจับ เรียกใช้เมธอดนี้จากฟังก์ชันAVCaptureVideoDataOutputSampleBufferDelegateของcaptureOutput(_, didOutput:from:)เพื่อรับผลลัพธ์จากเฟรมวิดีโอที่ระบุแบบพร้อมกัน ตั้งค่าAVCaptureVideoDataOutputของalwaysDiscardsLateVideoFramesเป็นtrueเพื่อควบคุมการเรียกไปยังเครื่องมือตรวจจับ หากเฟรมวิดีโอใหม่ พร้อมใช้งานขณะที่เครื่องมือตรวจจับกำลังทำงาน ระบบจะทิ้งเฟรมนั้น - หากคุณใช้เอาต์พุตของเครื่องมือตรวจจับเพื่อซ้อนทับกราฟิกบน รูปภาพอินพุต ให้รับผลลัพธ์จาก ML Kit ก่อน จากนั้นแสดงรูปภาพ และซ้อนทับในขั้นตอนเดียว การทำเช่นนี้จะแสดงผลไปยังพื้นผิวการแสดงผล เพียงครั้งเดียวสำหรับเฟรมอินพุตที่ประมวลผลแต่ละเฟรม ดูตัวอย่างได้ที่ updatePreviewOverlayViewWithLastFrame ในตัวอย่างการเริ่มต้นใช้งานอย่างรวดเร็วของ ML Kit