การย้ายข้อมูลจาก 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 การตรวจจับ 2 รายการที่แตกต่างกัน ซึ่งเป็นการดำเนินการแบบซิงโครนัสทั้งคู่

- (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 การตรวจจับ 2 รายการที่แตกต่างกัน ซึ่งเป็นการดำเนินการแบบซิงโครนัสทั้งคู่

- (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 การตรวจจับ 2 รายการที่แตกต่างกัน ซึ่งเป็นการดำเนินการแบบซิงโครนัสทั้งคู่

- (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

การขอความช่วยเหลือ

หากพบปัญหา โปรดดูหน้าชุมชน ที่เราสรุปช่องทางต่างๆ ที่คุณใช้ติดต่อเราได้