การย้ายข้อมูลจาก 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

อัปเดตทรัพยากร Dependency สำหรับ ML Kit iOS Cocoapods ใน Podfile ของแอปโดยทำดังนี้

APIพ็อด GMVพ็อด ML Kit
การสแกนบาร์โค้ด 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 รายการ ที่แตกต่างกัน ทั้ง 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 คุณเรียกใช้ Inference 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 แบบ ทั้ง 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 คุณเรียกใช้ Inference 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 รายการ ที่แตกต่างกัน ทั้ง 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 คุณเรียกใช้ Inference 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

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

หากพบปัญหา โปรดไปที่หน้าชุมชน ซึ่งเราได้ระบุช่องทางที่ใช้ติดต่อเรา