มีสองวิธีในการผสานรวมโมเดลที่ฝึกจาก 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 ขึ้นไปหากคุณต้องการดาวน์โหลด model อย่าลืม เพิ่ม Firebase ในโครงการ Android ของคุณ หากคุณยังไม่ได้ดำเนินการดังกล่าว สิ่งนี้ไม่จำเป็นเมื่อคุณรวมโมเดล
1. โหลดโมเดล
กำหนดค่าแหล่งที่มาของโมเดลในพื้นที่
วิธีรวมโมเดลกับแอปของคุณ:
แยกโมเดลและข้อมูลเมตาจากไฟล์ zip ที่คุณดาวน์โหลดจากคอนโซล Firebase ลงในโฟลเดอร์:
your_model_directory |____dict.txt |____manifest.json |____model.tflite
ทั้งสามไฟล์ต้องอยู่ในโฟลเดอร์เดียวกัน เราขอแนะนำให้คุณใช้ไฟล์ตามที่ดาวน์โหลดมา โดยไม่ต้องแก้ไข (รวมถึงชื่อไฟล์)
คัดลอกโฟลเดอร์ไปยังโปรเจ็กต์ Xcode ของคุณ โดยเลือก สร้างการอ้างอิงโฟลเดอร์ เมื่อคุณดำเนินการดังกล่าว ไฟล์รุ่นและข้อมูลเมตาจะรวมอยู่ในชุดแอปและพร้อมใช้งานสำหรับ ML Kit
สร้างวัตถุ
LocalModel
โดยระบุพาธไปยังไฟล์รายการโมเดล:Swift
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
โดยระบุชื่อที่คุณกำหนดให้กับโมเดลเมื่อคุณเผยแพร่:
Swift
// 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:
Swift
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
จากหนึ่งในนั้น
หากคุณมีเฉพาะโมเดลที่รวมอยู่ในเครื่อง ให้สร้าง labeler จากวัตถุ LocalModel
ของคุณและกำหนดค่าเกณฑ์คะแนนความเชื่อมั่นที่คุณต้องการ (ดู ประเมินโมเดลของคุณ ):
Swift
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 จากโมเดลระยะไกลหาก ถูกดาวน์โหลดและจากรุ่นท้องถิ่นเป็นอย่างอื่น
Swift
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
ในบล็อกผู้สังเกตการณ์ เนื่องจากการดาวน์โหลดอาจใช้เวลาพอสมควร และวัตถุต้นทางจะเป็นอิสระได้เมื่อการดาวน์โหลดเสร็จสิ้น ตัวอย่างเช่น:
Swift
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
ที่ถูกต้องSwift
let image = VisionImage(image: uiImage) visionImage.orientation = image.imageOrientation
วัตถุประสงค์-C
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image]; visionImage.orientation = image.imageOrientation;
หากคุณใช้ CMSampleBufferRef
ให้ทำตามขั้นตอนเหล่านี้:
ระบุการวางแนวของข้อมูลรูปภาพที่มีอยู่ในบัฟเฟอร์
CMSampleBufferRef
ในการรับการวางแนวของภาพ:
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 } }
วัตถุประสงค์-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
และการวางแนว:Swift
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. เรียกใช้เครื่องติดฉลากรูปภาพ
แบบอะซิงโครนัส:
Swift
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.
}];
พร้อมกัน:
Swift
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) คะแนนความเชื่อมั่น และดัชนี ตัวอย่างเช่น: Swift
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