מעבר מ-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

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

GMV

NSDictionary *options = @{
    GMVDetectorFaceMode : @(GMVDetectorFaceAccurateMode),
    GMVDetectorFaceClassificationType : @(GMVDetectorFaceClassificationAll),
    GMVDetectorFaceLandmarkType : @(GMVDetectorFaceLandmarkAll)
};
GMVDetector *faceDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeFace options:options];

ML Kit

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:));

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

GMV ML Kit
GMVFaceFeature MLKFace
GMVFaceContour MLKFaceContour
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary של אפשרויות זיהוי פנים MLKFaceDetectorOptions
GMVDetectorFaceFastMode Set MLKFaceDetectorOptions.performanceMode to MLKFaceDetectorPerformanceModeFast
GMVDetectorFaceAccurateMode Set MLKFaceDetectorOptions.performanceMode to MLKFaceDetectorPerformanceModeAccurate
GMVDetectorFaceSelfieMode Set MLKFaceDetectorOptions.contourMode to MLKFaceDetectorContourModeAll
GMVDetectorFaceLandmarkType MLKFaceDetectorOptions.landmarkMode
GMVDetectorFaceLandmarkNone Set MLKFaceDetectorOptions.landmarkMode to MLKFaceDetectorLandmarkModeNone
GMVDetectorFaceLandmarkAll Set MLKFaceDetectorOptions.landmarkMode to MLKFaceDetectorLandmarkModeAll
GMVDetectorFaceLandmarkContour Set MLKFaceDetectorOptions.contourMode to MLKFaceDetectorContourModeAll
GMVDetectorFaceClassificationType MLKFaceDetectorOptions.classificationMode
GMVDetectorFaceClassificationNone Set MLKFaceDetectorOptions.classificationMode to MLKFaceDetectorClassificationModeNone
GMVDetectorFaceClassificationAll Set MLKFaceDetectorOptions.classificationMode to MLKFaceDetectorClassificationModeAll
GMVDetectorFaceTrackingEnabled MLKFaceDetectorOptions.trackingEnabled
GMVDetectorProminentFaceOnly Set MLKFaceDetectorOptions.contourMode to MLKFaceDetectorContourModeAll
GMVDetectorFaceMinSize MLKFaceDetectorOptions.minFaceSize

BarcodeDetector

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

GMV

NSDictionary *options = @{
    GMVDetectorBarcodeFormats : @(GMVDetectorBarcodeFormatCode128 |
                                  GMVDetectorBarcodeFormatQRCode)
};
GMVDetector *barcodeDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeBarcode options:options];

ML Kit

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:));

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

GMV ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary של אפשרויות לזיהוי ברקודים MLKBarcodeScannerOptions
GMVDetectorBarcodeFormats MLKBarcodeScannerOptions.formats
GMVBarcodeFeature MLKBarcode
GMVBarcodeFeatureAddress MLKBarcodeAddress
GMVBarcodeFeatureCalendarEvent MLKBarcodeCalendarEvent
GMVBarcodeFeatureContactInfo MLKBarcodeContactInfo
GMVBarcodeFeatureDriverLicense MLKBarcodeDriverLicense
GMVBarcodeFeatureEmail MLKBarcodeEmail
GMVBarcodeFeatureGeoPoint MLKBarcodeGeoPoint
GMVBarcodeFeaturePersonName MLKBarcodePersonName
GMVBarcodeFeaturePhone MLKBarcodePhone
GMVBarcodeFeatureSMS MLKBarcodeSMS
GMVBarcodeFeatureURLBookmark MLKBarcodeURLBookmark
GMVBarcodeFeatureWiFi MLKBarcodeWiFi

TextRecognition

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

GMV

GMVDetector *textDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeText options:nil];

ML Kit

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:));

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

GMV ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
GMVTextBlockFeature MLKTextBlock
GMVTextElementFeature MLKTextElement
GMVTextLineFeature MLKTextLine

קבלת עזרה

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