หลังจากที่คุณ ฝึกโมเดลของคุณเองโดยใช้ AutoML Vision Edge แล้ว คุณจะสามารถใช้โมเดลนั้นในแอปของคุณเพื่อติดป้ายกำกับรูปภาพได้
มีสองวิธีในการผสานรวมโมเดลที่ได้รับการฝึกจาก AutoML Vision Edge คุณสามารถรวมโมเดลเข้าด้วยกันโดยการคัดลอกไฟล์ของโมเดลลงในโปรเจ็กต์ Xcode ของคุณ หรือคุณสามารถดาวน์โหลดแบบไดนามิกได้จาก Firebase
ตัวเลือกการรวมโมเดล | |
---|---|
รวมอยู่ในแอปของคุณ |
|
โฮสต์ด้วย Firebase |
|
ก่อนที่คุณจะเริ่ม
รวมไลบรารี ML Kit ไว้ใน Podfile ของคุณ:
หากต้องการรวมโมเดลเข้ากับแอปของคุณ:
pod 'GoogleMLKit/ImageLabelingCustom'
สำหรับการดาวน์โหลดโมเดลแบบไดนามิกจาก Firebase ให้เพิ่มการพึ่งพา
LinkFirebase
:pod 'GoogleMLKit/ImageLabelingCustom' pod 'GoogleMLKit/LinkFirebase'
หลังจากที่คุณติดตั้งหรืออัปเดต Pod ของโปรเจ็กต์แล้ว ให้เปิดโปรเจ็กต์ Xcode โดยใช้
.xcworkspace
ML Kit รองรับ Xcode เวอร์ชัน 12.2 ขึ้นไปหากคุณต้องการดาวน์โหลดโมเดล ตรวจสอบ ให้แน่ใจว่าคุณ ได้เพิ่ม Firebase ลงในโปรเจ็กต์ Android ของคุณ หากยังไม่ได้ดำเนินการ สิ่งนี้ไม่จำเป็นเมื่อคุณรวมโมเดลเข้าด้วยกัน
1. โหลดโมเดล
กำหนดค่าแหล่งที่มาของโมเดลในเครื่อง
หากต้องการรวมโมเดลเข้ากับแอปของคุณ:
แยกโมเดลและข้อมูลเมตาออกจากไฟล์ zip ที่คุณดาวน์โหลดจากคอนโซล Firebase ลงในโฟลเดอร์:
your_model_directory |____dict.txt |____manifest.json |____model.tflite
ทั้งสามไฟล์จะต้องอยู่ในโฟลเดอร์เดียวกัน เราขอแนะนำให้คุณใช้ไฟล์ในขณะที่คุณดาวน์โหลดโดยไม่มีการแก้ไข (รวมถึงชื่อไฟล์ด้วย)
คัดลอกโฟลเดอร์ไปยังโปรเจ็กต์ Xcode ของคุณ โดยเลือก สร้างการอ้างอิงโฟลเดอร์ เมื่อคุณทำเช่นนั้น ไฟล์โมเดลและข้อมูลเมตาจะรวมอยู่ใน App Bundle และพร้อมใช้งานสำหรับ ML Kit
สร้างวัตถุ
LocalModel
โดยระบุเส้นทางไปยังไฟล์รายการโมเดล:สวิฟท์
guard let manifestPath = Bundle.main.path( forResource: "manifest", ofType: "json", inDirectory: "your_model_directory" ) else { return true } let localModel = LocalModel(manifestPath: manifestPath)
วัตถุประสงค์-C
NSString *manifestPath = [NSBundle.mainBundle pathForResource:@"manifest" ofType:@"json" inDirectory:@"your_model_directory"]; MLKLocalModel *localModel = [[MLKLocalModel alloc] initWithManifestPath:manifestPath];
กำหนดค่าแหล่งที่มาของโมเดลที่โฮสต์โดย Firebase
หากต้องการใช้โมเดลที่โฮสต์ระยะไกล ให้สร้างออบเจ็กต์ CustomRemoteModel
โดยระบุชื่อที่คุณกำหนดให้กับโมเดลเมื่อคุณเผยแพร่:
สวิฟท์
// Initialize the model source with the name you assigned in
// the Firebase console.
let remoteModelSource = FirebaseModelSource(name: "your_remote_model")
let remoteModel = CustomRemoteModel(remoteModelSource: remoteModelSource)
วัตถุประสงค์-C
// Initialize the model source with the name you assigned in
// the Firebase console.
MLKFirebaseModelSource *firebaseModelSource =
[[MLKFirebaseModelSource alloc] initWithName:@"your_remote_model"];
MLKCustomRemoteModel *remoteModel =
[[MLKCustomRemoteModel alloc] initWithRemoteModelSource:firebaseModelSource];
จากนั้น เริ่มต้นงานการดาวน์โหลดโมเดล โดยระบุเงื่อนไขที่คุณต้องการอนุญาตให้ดาวน์โหลด หากไม่มีโมเดลดังกล่าวอยู่ในอุปกรณ์ หรือมีเวอร์ชันที่ใหม่กว่า งานจะดาวน์โหลดโมเดลจาก Firebase แบบอะซิงโครนัส:
สวิฟท์
let downloadConditions = ModelDownloadConditions(
allowsCellularAccess: true,
allowsBackgroundDownloading: true
)
let downloadProgress = ModelManager.modelManager().download(
remoteModel,
conditions: downloadConditions
)
วัตถุประสงค์-C
MLKModelDownloadConditions *downloadConditions =
[[MLKModelDownloadConditions alloc] initWithAllowsCellularAccess:YES
allowsBackgroundDownloading:YES];
NSProgress *downloadProgress =
[[MLKModelManager modelManager] downloadRemoteModel:remoteModel
conditions:downloadConditions];
แอพจำนวนมากเริ่มงานดาวน์โหลดด้วยโค้ดเริ่มต้น แต่คุณสามารถทำได้ทุกเมื่อก่อนจำเป็นต้องใช้โมเดล
สร้างเครื่องติดป้ายกำกับรูปภาพจากโมเดลของคุณ
หลังจากที่คุณกำหนดค่าแหล่งที่มาของโมเดลแล้ว ให้สร้างออบเจ็กต์ ImageLabeler
จากหนึ่งในนั้น
หากคุณมีเฉพาะโมเดลที่รวมอยู่ในเครื่อง เพียงสร้างผู้ติดป้ายกำกับจากออบเจ็กต์ LocalModel
ของคุณและกำหนดค่าเกณฑ์คะแนนความเชื่อมั่นที่คุณต้องการ (ดู ประเมินโมเดลของคุณ ):
สวิฟท์
let options = CustomImageLabelerOptions(localModel: localModel)
options.confidenceThreshold = NSNumber(value: 0.0) // Evaluate your model in the Cloud console
// to determine an appropriate value.
let imageLabeler = ImageLabeler.imageLabeler(options)
วัตถุประสงค์-C
CustomImageLabelerOptions *options =
[[CustomImageLabelerOptions alloc] initWithLocalModel:localModel];
options.confidenceThreshold = @(0.0f); // Evaluate your model in the Cloud console
// to determine an appropriate value.
MLKImageLabeler *imageLabeler =
[MLKImageLabeler imageLabelerWithOptions:options];
หากคุณมีโมเดลที่โฮสต์ระยะไกล คุณจะต้องตรวจสอบว่ามีการดาวน์โหลดก่อนที่จะรัน คุณสามารถตรวจสอบสถานะของงานดาวน์โหลดโมเดลได้โดยใช้เมธอด isModelDownloaded(remoteModel:)
ของตัวจัดการโมเดล
แม้ว่าคุณจะต้องยืนยันสิ่งนี้ก่อนเรียกใช้ Labeler เท่านั้น หากคุณมีทั้งโมเดลที่โฮสต์จากระยะไกลและโมเดลที่รวมไว้ในเครื่อง ก็อาจเหมาะสมที่จะดำเนินการตรวจสอบนี้เมื่อสร้างอินสแตนซ์ ImageLabeler
: สร้าง labeler จากโมเดลระยะไกลหาก ได้รับการดาวน์โหลดและจากรุ่นท้องถิ่นเป็นอย่างอื่น
สวิฟท์
var options: CustomImageLabelerOptions
if (ModelManager.modelManager().isModelDownloaded(remoteModel)) {
options = CustomImageLabelerOptions(remoteModel: remoteModel)
} else {
options = CustomImageLabelerOptions(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)
วัตถุประสงค์-C
MLKCustomImageLabelerOptions *options;
if ([[MLKModelManager modelManager] isModelDownloaded:remoteModel]) {
options = [[MLKCustomImageLabelerOptions alloc] initWithRemoteModel:remoteModel];
} else {
options = [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel];
}
options.confidenceThreshold = @(0.0f); // Evaluate your model in the Firebase console
// to determine an appropriate value.
MLKImageLabeler *imageLabeler =
[MLKImageLabeler imageLabelerWithOptions:options];
หากคุณมีเฉพาะโมเดลที่โฮสต์จากระยะไกล คุณควรปิดใช้งานฟังก์ชันที่เกี่ยวข้องกับโมเดล เช่น ทำให้เป็นสีเทาหรือซ่อนบางส่วนของ UI จนกว่าคุณจะยืนยันว่าดาวน์โหลดโมเดลแล้ว
คุณสามารถรับสถานะการดาวน์โหลดโมเดลได้โดยแนบผู้สังเกตการณ์เข้ากับศูนย์การแจ้งเตือนเริ่มต้น ตรวจสอบให้แน่ใจว่าใช้การอ้างอิงถึง self
ที่ไม่ชัดเจนในบล็อกผู้สังเกตการณ์ เนื่องจากการดาวน์โหลดอาจใช้เวลาสักครู่ และวัตถุต้นทางสามารถปล่อยออกได้เมื่อการดาวน์โหลดเสร็จสิ้น ตัวอย่างเช่น:
สวิฟท์
NotificationCenter.default.addObserver(
forName: .mlkitMLModelDownloadDidSucceed,
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: .mlkitMLModelDownloadDidFail,
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]
// ...
}
วัตถุประสงค์-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
หรือ CMSampleBufferRef
หากคุณใช้ UIImage
ให้ทำตามขั้นตอนเหล่านี้:
- สร้างวัตถุ
VisionImage
ด้วยUIImage
ตรวจสอบให้แน่ใจว่าได้ระบุ.orientation
ที่ถูกต้องสวิฟท์
let image = VisionImage(image: uiImage) visionImage.orientation = image.imageOrientation
วัตถุประสงค์-C
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image]; visionImage.orientation = image.imageOrientation;
ถ้าคุณใช้ CMSampleBufferRef
ให้ทำตามขั้นตอนเหล่านี้:
ระบุการวางแนวของข้อมูลรูปภาพที่มีอยู่ในบัฟเฟอร์
CMSampleBufferRef
เพื่อให้ได้การวางแนวของภาพ:
สวิฟท์
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 } }
วัตถุประสงค์-C
- (UIImageOrientation) imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation cameraPosition:(AVCaptureDevicePosition)cameraPosition { switch (deviceOrientation) { case UIDeviceOrientationPortrait: return position == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored : UIImageOrientationRight; case UIDeviceOrientationLandscapeLeft: return position == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored : UIImageOrientationUp; case UIDeviceOrientationPortraitUpsideDown: return position == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored : UIImageOrientationLeft; case UIDeviceOrientationLandscapeRight: return position == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored : UIImageOrientationDown; case UIDeviceOrientationUnknown: case UIDeviceOrientationFaceUp: case UIDeviceOrientationFaceDown: return UIImageOrientationUp; } }
- สร้างวัตถุ
VisionImage
โดยใช้วัตถุCMSampleBufferRef
และการวางแนว:สวิฟท์
let image = VisionImage(buffer: sampleBuffer) image.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition)
วัตถุประสงค์-C
MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer]; image.orientation = [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation cameraPosition:cameraPosition];
3. เรียกใช้ตัวติดป้ายกำกับรูปภาพ
แบบอะซิงโครนัส:
สวิฟท์
imageLabeler.process(image) { labels, error in
guard error == nil, let labels = labels, !labels.isEmpty else {
// Handle the error.
return
}
// Show results.
}
วัตถุประสงค์-C
[imageLabeler
processImage:image
completion:^(NSArray<MLKImageLabel *> *_Nullable labels,
NSError *_Nullable error) {
if (label.count == 0) {
// Handle the error.
return;
}
// Show results.
}];
พร้อมกัน:
สวิฟท์
var labels: [ImageLabel]
do {
labels = try imageLabeler.results(in: image)
} catch let error {
// Handle the error.
return
}
// Show results.
วัตถุประสงค์-C
NSError *error;
NSArray<MLKImageLabel *> *labels =
[imageLabeler resultsInImage:image error:&error];
// Show results or handle the error.
4. รับข้อมูลเกี่ยวกับวัตถุที่มีป้ายกำกับ
หากการดำเนินการติดป้ายกำกับรูปภาพสำเร็จ ระบบจะส่งกลับอาร์เรย์ของ ImageLabel
ImageLabel
แต่ละอันแสดงถึงสิ่งที่ถูกติดป้ายกำกับไว้ในรูปภาพ คุณสามารถรับคำอธิบายข้อความของป้ายกำกับแต่ละรายการ (หากมีอยู่ในข้อมูลเมตาของไฟล์โมเดล TensorFlow Lite) คะแนนความเชื่อมั่น และดัชนี ตัวอย่างเช่น:
สวิฟท์
for label in labels {
let labelText = label.text
let confidence = label.confidence
let index = label.index
}
วัตถุประสงค์-C
for (MLKImageLabel *label in labels) {
NSString *labelText = label.text;
float confidence = label.confidence;
NSInteger index = label.index;
}
เคล็ดลับในการปรับปรุงประสิทธิภาพแบบเรียลไทม์
หากคุณต้องการติดป้ายกำกับรูปภาพในแอปพลิเคชันแบบเรียลไทม์ ให้ปฏิบัติตามหลักเกณฑ์เหล่านี้เพื่อให้ได้อัตราเฟรมที่ดีที่สุด:
- คันเร่งเรียกไปที่เครื่องตรวจจับ หากมีเฟรมวิดีโอใหม่ในขณะที่ตัวตรวจจับกำลังทำงานอยู่ ให้ปล่อยเฟรมนั้น
- หากคุณใช้เอาต์พุตของตัวตรวจจับเพื่อวางซ้อนกราฟิกบนรูปภาพอินพุต ขั้นแรกให้รับผลลัพธ์ จากนั้นจึงเรนเดอร์รูปภาพและโอเวอร์เลย์ในขั้นตอนเดียว การทำเช่นนี้ คุณจะเรนเดอร์ไปยังพื้นผิวจอแสดงผลเพียงครั้งเดียวสำหรับแต่ละเฟรมอินพุต ดูตัวอย่างคลาส PreviewOverlayView และ FIRDetectionOverlayView ในแอปตัวอย่าง Showcase