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

קבלת עזרה

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