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

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

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

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

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

עדכון Coapods

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

APIפוד GMVפוד לערכת ML
סריקת ברקודים 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

קבלת עזרה

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