תיוג תמונות באמצעות מודל עם אימון אוטומטי ב-iOS

לאחר שתאמן את הדגם שלך באמצעות AutoML Vision Edge , תוכל להשתמש בו באפליקציה שלך כדי לתייג תמונות.

לפני שאתה מתחיל

  1. אם עדיין לא הוספת את Firebase לאפליקציה שלך, עשה זאת על ידי ביצוע השלבים במדריך לתחילת העבודה .
  2. כלול את ספריות ML Kit ב-Podfile שלך:
    pod 'Firebase/MLVision', '6.25.0'
    pod 'Firebase/MLVisionAutoML', '6.25.0'
    
    לאחר שתתקין או תעדכן את ה-Pods של הפרויקט שלך, הקפד לפתוח את פרויקט Xcode שלך ​​באמצעות .xcworkspace שלו.
  3. באפליקציה שלך, ייבא את Firebase:

    מָהִיר

    import Firebase

    Objective-C

    @import Firebase;

1. טען את הדגם

ML Kit מריץ את הדגמים שנוצרו על ידי AutoML במכשיר. עם זאת, אתה יכול להגדיר את ערכת ML לטעון את הדגם שלך מרחוק מ-Firebase, מאחסון מקומי או שניהם.

על ידי אירוח הדגם ב-Firebase, אתה יכול לעדכן את המודל מבלי לשחרר גרסת אפליקציה חדשה, ותוכל להשתמש ב-Remote Config ו-A/B Testing כדי להגיש באופן דינמי מודלים שונים לקבוצות שונות של משתמשים.

אם תבחר לספק את הדגם רק על ידי אירוח שלו ב-Firebase, ולא לצרף אותו לאפליקציה שלך, תוכל להקטין את גודל ההורדה הראשוני של האפליקציה שלך. עם זאת, זכור שאם הדגם אינו מצורף לאפליקציה שלך, כל פונקציונליות הקשורה לדגם לא תהיה זמינה עד שהאפליקציה שלך תוריד את הדגם בפעם הראשונה.

על ידי צירוף הדגם שלך עם האפליקציה שלך, אתה יכול להבטיח שתכונות ה-ML של האפליקציה שלך עדיין פועלות כאשר המודל המתארח ב-Firebase אינו זמין.

הגדר מקור מודל שמתארח ב-Firebase

כדי להשתמש במודל המתארח מרחוק, צור אובייקט AutoMLRemoteModel , תוך ציון השם שהקצית למודל כשפרסמת אותו:

מָהִיר

let remoteModel = AutoMLRemoteModel(
    name: "your_remote_model"  // The name you assigned in the Firebase console.
)

Objective-C

FIRAutoMLRemoteModel *remoteModel = [[FIRAutoMLRemoteModel alloc]
    initWithName:@"your_remote_model"];  // The name you assigned in the Firebase console.

לאחר מכן, התחל את משימת הורדת הדגם, תוך ציון התנאים שבהם ברצונך לאפשר הורדה. אם הדגם אינו במכשיר, או אם זמינה גרסה חדשה יותר של הדגם, המשימה תוריד את הדגם באופן אסינכרוני מ-Firebase:

מָהִיר

let downloadConditions = ModelDownloadConditions(
  allowsCellularAccess: true,
  allowsBackgroundDownloading: true
)

let downloadProgress = ModelManager.modelManager().download(
  remoteModel,
  conditions: downloadConditions
)

Objective-C

FIRModelDownloadConditions *downloadConditions =
    [[FIRModelDownloadConditions alloc] initWithAllowsCellularAccess:YES
                                         allowsBackgroundDownloading:YES];

NSProgress *downloadProgress =
    [[FIRModelManager modelManager] downloadRemoteModel:remoteModel
                                             conditions:downloadConditions];

אפליקציות רבות מתחילות את משימת ההורדה בקוד האתחול שלהן, אך תוכל לעשות זאת בכל שלב לפני שתצטרך להשתמש במודל.

הגדר מקור דגם מקומי

כדי לאגד את הדגם עם האפליקציה שלך:

  1. חלץ את המודל ואת המטא נתונים שלו מארכיון ה-zip שהורדת ממסוף Firebase לתיקיה:
    your_model_directory
      |____dict.txt
      |____manifest.json
      |____model.tflite
    
    כל שלושת הקבצים חייבים להיות באותה תיקיה. אנו ממליצים להשתמש בקבצים כפי שהורדתם אותם, ללא שינוי (כולל שמות הקבצים).
  2. העתק את התיקיה לפרויקט Xcode שלך, הקפד לבחור באפשרות צור הפניות לתיקיות כאשר אתה עושה זאת. קובץ הדגם והמטא נתונים ייכללו ב-App Bundle וזמינים ל-ML Kit.
  3. צור אובייקט AutoMLLocalModel , תוך ציון הנתיב לקובץ המניפסט של המודל:

    מָהִיר

    guard let manifestPath = Bundle.main.path(
        forResource: "manifest",
        ofType: "json",
        inDirectory: "your_model_directory"
    ) else { return true }
    let localModel = AutoMLLocalModel(manifestPath: manifestPath)
    

    Objective-C

    NSString *manifestPath = [NSBundle.mainBundle pathForResource:@"manifest"
                                                           ofType:@"json"
                                                      inDirectory:@"your_model_directory"];
    FIRAutoMLLocalModel *localModel = [[FIRAutoMLLocalModel alloc] initWithManifestPath:manifestPath];
    

צור תוויות תמונה מהדגם שלך

לאחר שתגדיר את מקורות המודל שלך, צור אובייקט VisionImageLabeler מאחד מהם.

אם יש לך רק מודל מקובץ מקומית, פשוט צור תוויות מאובייקט AutoMLLocalModel שלך והגדר את סף ציון הביטחון שאתה רוצה לדרוש (ראה הערכת המודל שלך ):

מָהִיר

let options = VisionOnDeviceAutoMLImageLabelerOptions(localModel: localModel)
options.confidenceThreshold = 0  // Evaluate your model in the Firebase console
                                 // to determine an appropriate value.
let labeler = Vision.vision().onDeviceAutoMLImageLabeler(options: options)

Objective-C

FIRVisionOnDeviceAutoMLImageLabelerOptions *options =
    [[FIRVisionOnDeviceAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel];
options.confidenceThreshold = 0;  // Evaluate your model in the Firebase console
                                  // to determine an appropriate value.
FIRVisionImageLabeler *labeler =
    [[FIRVision vision] onDeviceAutoMLImageLabelerWithOptions:options];

אם יש לך דגם שמתארח מרחוק, תצטרך לבדוק שהוא הורד לפני שתפעיל אותו. אתה יכול לבדוק את המצב של משימת הורדת המודל באמצעות שיטת isModelDownloaded(remoteModel:) של מנהל המודלים.

למרות שאתה רק צריך לאשר זאת לפני הפעלת התווית, אם יש לך גם דגם שמתארח מרחוק וגם מודל עם חבילה מקומית, ייתכן שיהיה הגיוני לבצע את הבדיקה הזו בעת הפעלת ה- VisionImageLabeler : צור תוויות מהדגם המרוחק אם זה הורד, ומהדגם המקומי אחרת.

מָהִיר

var options: VisionOnDeviceAutoMLImageLabelerOptions?
if (ModelManager.modelManager().isModelDownloaded(remoteModel)) {
  options = VisionOnDeviceAutoMLImageLabelerOptions(remoteModel: remoteModel)
} else {
  options = VisionOnDeviceAutoMLImageLabelerOptions(localModel: localModel)
}
options.confidenceThreshold = 0  // Evaluate your model in the Firebase console
                                 // to determine an appropriate value.
let labeler = Vision.vision().onDeviceAutoMLImageLabeler(options: options)

Objective-C

VisionOnDeviceAutoMLImageLabelerOptions *options;
if ([[FIRModelManager modelManager] isModelDownloaded:remoteModel]) {
  options = [[FIRVisionOnDeviceAutoMLImageLabelerOptions alloc] initWithRemoteModel:remoteModel];
} else {
  options = [[FIRVisionOnDeviceAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel];
}
options.confidenceThreshold = 0.0f;  // Evaluate your model in the Firebase console
                                     // to determine an appropriate value.
FIRVisionImageLabeler *labeler = [[FIRVision vision] onDeviceAutoMLImageLabelerWithOptions:options];

אם יש לך רק דגם שמתארח מרחוק, עליך להשבית את הפונקציונליות הקשורה לדגם - למשל, לאפור או להסתיר חלק מהממשק שלך - עד שתאשר שהדגם הורד.

אתה יכול לקבל את סטטוס הורדת הדגם על ידי צירוף צופים למרכז ההתראות המוגדר כברירת מחדל. הקפד להשתמש בהתייחסות חלשה self בבלוק הצופה, מכיוון שההורדות יכולות להימשך זמן מה, והאובייקט המקור יכול להשתחרר עד לסיום ההורדה. לדוגמה:

מָהִיר

NotificationCenter.default.addObserver(
    forName: .firebaseMLModelDownloadDidSucceed,
    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: .firebaseMLModelDownloadDidFail,
    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:FIRModelDownloadDidSucceedNotification
                object:nil
                 queue:nil
            usingBlock:^(NSNotification *_Nonnull note) {
              if (weakSelf == nil | note.userInfo == nil) {
                return;
              }
              __strong typeof(self) strongSelf = weakSelf;

              FIRRemoteModel *model = note.userInfo[FIRModelDownloadUserInfoKeyRemoteModel];
              if ([model.name isEqualToString:@"your_remote_model"]) {
                // The model was downloaded and is available on the device
              }
            }];

[NSNotificationCenter.defaultCenter
    addObserverForName:FIRModelDownloadDidFailNotification
                object:nil
                 queue:nil
            usingBlock:^(NSNotification *_Nonnull note) {
              if (weakSelf == nil | note.userInfo == nil) {
                return;
              }
              __strong typeof(self) strongSelf = weakSelf;

              NSError *error = note.userInfo[FIRModelDownloadUserInfoKeyError];
            }];

2. הכן את תמונת הקלט

לאחר מכן, עבור כל תמונה שברצונך לסמן, צור אובייקט VisionImage באמצעות אחת מהאפשרויות המתוארות בסעיף זה והעביר אותו למופע של VisionImageLabeler (מתואר בסעיף הבא).

צור אובייקט VisionImage באמצעות UIImage או CMSampleBufferRef .

כדי להשתמש UIImage :

  1. במידת הצורך, סובב את התמונה כך שהמאפיין imageOrientation שלה יהיה .up .
  2. צור אובייקט VisionImage באמצעות UIImage המסובב כהלכה. אל תציין מטא נתונים כלשהם של סיבוב - יש להשתמש בערך ברירת המחדל, .topLeft .

    מָהִיר

    let image = VisionImage(image: uiImage)

    Objective-C

    FIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage];

כדי להשתמש ב- CMSampleBufferRef :

  1. צור אובייקט VisionImageMetadata המציין את הכיוון של נתוני התמונה הכלולים במאגר CMSampleBufferRef .

    כדי לקבל את כיוון התמונה:

    מָהִיר

    func imageOrientation(
        deviceOrientation: UIDeviceOrientation,
        cameraPosition: AVCaptureDevice.Position
        ) -> VisionDetectorImageOrientation {
        switch deviceOrientation {
        case .portrait:
            return cameraPosition == .front ? .leftTop : .rightTop
        case .landscapeLeft:
            return cameraPosition == .front ? .bottomLeft : .topLeft
        case .portraitUpsideDown:
            return cameraPosition == .front ? .rightBottom : .leftBottom
        case .landscapeRight:
            return cameraPosition == .front ? .topRight : .bottomRight
        case .faceDown, .faceUp, .unknown:
            return .leftTop
        }
    }

    Objective-C

    - (FIRVisionDetectorImageOrientation)
        imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation
                               cameraPosition:(AVCaptureDevicePosition)cameraPosition {
      switch (deviceOrientation) {
        case UIDeviceOrientationPortrait:
          if (cameraPosition == AVCaptureDevicePositionFront) {
            return FIRVisionDetectorImageOrientationLeftTop;
          } else {
            return FIRVisionDetectorImageOrientationRightTop;
          }
        case UIDeviceOrientationLandscapeLeft:
          if (cameraPosition == AVCaptureDevicePositionFront) {
            return FIRVisionDetectorImageOrientationBottomLeft;
          } else {
            return FIRVisionDetectorImageOrientationTopLeft;
          }
        case UIDeviceOrientationPortraitUpsideDown:
          if (cameraPosition == AVCaptureDevicePositionFront) {
            return FIRVisionDetectorImageOrientationRightBottom;
          } else {
            return FIRVisionDetectorImageOrientationLeftBottom;
          }
        case UIDeviceOrientationLandscapeRight:
          if (cameraPosition == AVCaptureDevicePositionFront) {
            return FIRVisionDetectorImageOrientationTopRight;
          } else {
            return FIRVisionDetectorImageOrientationBottomRight;
          }
        default:
          return FIRVisionDetectorImageOrientationTopLeft;
      }
    }

    לאחר מכן, צור את אובייקט המטא נתונים:

    מָהִיר

    let cameraPosition = AVCaptureDevice.Position.back  // Set to the capture device you used.
    let metadata = VisionImageMetadata()
    metadata.orientation = imageOrientation(
        deviceOrientation: UIDevice.current.orientation,
        cameraPosition: cameraPosition
    )

    Objective-C

    FIRVisionImageMetadata *metadata = [[FIRVisionImageMetadata alloc] init];
    AVCaptureDevicePosition cameraPosition =
        AVCaptureDevicePositionBack;  // Set to the capture device you used.
    metadata.orientation =
        [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                     cameraPosition:cameraPosition];
  2. צור אובייקט VisionImage באמצעות האובייקט CMSampleBufferRef והמטא נתונים של הסיבוב:

    מָהִיר

    let image = VisionImage(buffer: sampleBuffer)
    image.metadata = metadata

    Objective-C

    FIRVisionImage *image = [[FIRVisionImage alloc] initWithBuffer:sampleBuffer];
    image.metadata = metadata;

3. הפעל את תווית התמונות

כדי לתייג אובייקטים בתמונה, העבר את אובייקט VisionImage לשיטת process() של VisionImageLabeler :

מָהִיר

labeler.process(image) { labels, error in
    guard error == nil, let labels = labels else { return }

    // Task succeeded.
    // ...
}

Objective-C

[labeler
    processImage:image
      completion:^(NSArray<FIRVisionImageLabel *> *_Nullable labels, NSError *_Nullable error) {
        if (error != nil || labels == nil) {
          return;
        }

        // Task succeeded.
        // ...
      }];

אם תיוג תמונה יצליח, מערך של אובייקטים של VisionImageLabel יועבר למטפל ההשלמה. מכל אובייקט ניתן לקבל מידע על תכונה המוכרת בתמונה.

לדוגמה:

מָהִיר

for label in labels {
    let labelText = label.text
    let confidence = label.confidence
}

Objective-C

for (FIRVisionImageLabel *label in labels) {
  NSString *labelText = label.text;
  NSNumber *confidence = label.confidence;
}

טיפים לשיפור הביצועים בזמן אמת

  • מצערת קוראת לגלאי. אם מסגרת וידאו חדשה הופכת לזמינה בזמן שהגלאי פועל, שחרר את המסגרת.
  • אם אתה משתמש בפלט של הגלאי כדי לשכב גרפיקה על תמונת הקלט, תחילה קבל את התוצאה מ-ML Kit, ולאחר מכן עבד את התמונה ואת שכבת העל בצעד אחד. על ידי כך, אתה מעבד למשטח התצוגה רק פעם אחת עבור כל מסגרת קלט. ראה את המחלקות previewOverlayView ו- FIRDetectionOverlayView באפליקציה לדוגמה לראווה לדוגמא.