מעבר מ-Mobile Vision ל-ML Kit ב-iOS

במסמך הזה מוסבר איך להעביר את הפרויקטים מ-Google Mobile Vision‏ (GMV) ל-ML Kit ב-iOS.

דרישות מוקדמות

לפני שמתחילים להעביר את הקוד, חשוב לוודא שאתם עומדים בדרישות הבאות:

  • ב-ML Kit יש תמיכה ב-Xcode בגרסה 13.2.1 ואילך.
  • ML Kit תומך ב-iOS מגרסה 15.5 ואילך.
  • ב-ML Kit אין תמיכה בארכיטקטורות של 32 ביט (i386 ו-armv7). ב-ML Kit יש תמיכה בארכיטקטורות של 64 ביט (x86_64 ו-arm64).

עדכון cocoapods

מעדכנים את יחסי התלות של ML Kit ל-iOS ב-cocoapods ב-Podfile של האפליקציה:

APIGMV PodML Kit Pod
סריקת ברקודים GoogleMobileVision/BarcodeDetector GoogleMLKit/BarcodeScanning
זיהוי פנים GoogleMobileVision/FaceDetector GoogleMLKit/FaceDetection
זיהוי טקסט GoogleMobileVision/TextDetector GoogleMLKit/TextRecognition

שינויים כלליים ב-API

השינויים האלה חלים על כל ממשקי ה-API:

  • ממשקי ה-API של GMV ליצירת אינטרפולציה מקבלים את UIImage או את CMSampleBufferRef כקלט. ‏ML Kit עוטף אותם בתוך MLKVisionImage ומשתמש בהם כקלט.
  • ה-GMV משתמש ב-NSDictionary כדי להעביר אפשרויות שונות של גלאי. לצורך כך, ב-ML Kit נעשה שימוש באפשרויות ייעודיות.
  • GMV מעביר את סוג הגלאי לכיתה היחידה GMVDetector כשיוצרים גלאי. ב-ML Kit נעשה שימוש בקטגוריות ייעודיות כדי ליצור מכונות נפרדות לזיהוי, לסריקה ולזיהוי אובייקטים.
  • ממשקי ה-API של GMV תומכים בזיהוי סינכרוני בלבד. אפשר להפעיל את ממשקי ה-API של ML Kit להסקה באופן סינכרוני ולא-סינכרוני.
  • GMV הוא תוסף ל-AVCaptureVideoDataOutput, והוא מספק מסגרת עם מספר גלאי זיהוי לביצוע מספר זיהויים בו-זמנית. ב-ML Kit אין מנגנונים כאלה, אבל המפתחים יכולים ליישם את אותה פונקציונליות אם הם רוצים.

שינויים ספציפיים ל-API

בקטע הזה מתוארות הכיתות והשיטות התואמות של GMV ו-ML Kit לכל אחד מ-Vision API, ומוסבר איך לאתחל את ה-API.

FaceDetector

מקודדים מחדש את האתחול כמו בדוגמה הבאה:

GMVML Kit
NSDictionary *options = @{
    GMVDetectorFaceMode : @(GMVDetectorFaceAccurateMode),
    GMVDetectorFaceClassificationType : @(GMVDetectorFaceClassificationAll),
    GMVDetectorFaceLandmarkType : @(GMVDetectorFaceLandmarkAll)
};
GMVDetector *faceDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeFace options:options];
MLKFaceDetectorOptions *options = [[MLKFaceDetectorOptions alloc] init];
options.performanceMode = MLKFaceDetectorPerformanceModeAccurate;
options.classificationMode = MLKFaceDetectorClassificationModeAll;
options.landmarkMode = MLKFaceDetectorLandmarkModeAll;
MLKFaceDetector *faceDetector = [MLKFaceDetector faceDetectorWithOptions:options];

ל-GMVDetector יש שני ממשקי API שונים לזיהוי. שתי הפעולות הן פעולות סינכרוניות:

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInImage:(UIImage *)image
            options:(nullable NSDictionary *)options;

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInBuffer:(CMSampleBufferRef)sampleBuffer
             options:(nullable NSDictionary *)options;

מחליפים את GMVDetector ב-MLKFaceDetector. אפשר להפעיל את ממשק ה-API של ההסקה באופן סינכרוני או אסינכררוני.

- (nullable NSArray<MLKFace *> *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKFaceDetectionCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

משנים את השמות, השיטות והכיתות הבאים:

BarcodeDetector

מקודדים מחדש את האתחול כמו בדוגמה הבאה:

GMVML Kit
NSDictionary *options = @{
    GMVDetectorBarcodeFormats : @(GMVDetectorBarcodeFormatCode128 |
                                  GMVDetectorBarcodeFormatQRCode)
};
GMVDetector *barcodeDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeBarcode options:options];
MLKBarcodeScannerOptions *options = [[MLKBarcodeScannerOptions alloc] init];
options.formats = MLKBarcodeFormatCode128 | MLKBarcodeFormatQRCode;
MLKBarcodeScanner *barcodeScanner =
    [MLKBarcodeScanner barcodeScannerWithOptions:options];

ל-GMVDetector יש שני ממשקי API שונים לזיהוי. שתי הפעולות הן פעולות סינכרוניות:

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInImage:(UIImage *)image
            options:(nullable NSDictionary *)options;

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInBuffer:(CMSampleBufferRef)sampleBuffer
             options:(nullable NSDictionary *)options;

מחליפים את GMVDetector ב-MLKBarcodeScanner. אפשר להפעיל את ממשק ה-API של ההסקה באופן סינכרוני או אסינכרוני.

- (nullable NSArray<MLKBarcode *> *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKBarcodeScanningCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

משנים את השמות, השיטות והכיתות הבאים:

TextRecognition

מקודדים מחדש את האתחול כמו בדוגמה הבאה:

GMVML Kit
GMVDetector *textDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeText options:nil];
MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];

ל-GMVDetector יש שני ממשקי API שונים לזיהוי. שתי הפעולות הן פעולות סינכרוניות:

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInImage:(UIImage *)image
            options:(nullable NSDictionary *)options;

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInBuffer:(CMSampleBufferRef)sampleBuffer
             options:(nullable NSDictionary *)options;

מחליפים את GMVDetector ב-MLKTextRecognizer. אפשר להפעיל את ממשק ה-API של ההסקה באופן סינכרוני או אסינכררוני.

- (nullable MLKText *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKTextRecognitionCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

משנים את השמות, השיטות והכיתות הבאים:

קבלת עזרה

אם נתקלתם בבעיות, תוכלו להיכנס לדף הקהילה שלנו, שבו מפורטים הערוצים שבהם אפשר ליצור איתנו קשר.