การย้ายข้อมูลจาก 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 สําหรับ Cocoapods ของ ML Kit iOS ใน 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

เขียนโค้ดการเริ่มต้นใหม่ตามที่แสดงในตัวอย่างนี้

GMVML Kit
NSDictionary *options = @{
    GMVDetectorFaceMode : @(GMVDetectorFaceAccurateMode),
    GMVDetectorFaceClassificationType : @(GMVDetectorFaceClassificationAll),
    GMVDetectorFaceLandmarkType : @(GMVDetectorFaceLandmarkAll)
};
GMVDetector *faceDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeFace options:options];
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 คุณสามารถเรียกใช้ API การอนุมานแบบซิงค์หรือไม่ซิงค์ก็ได้

- (nullable NSArray<MLKFace *> *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKFaceDetectionCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

เปลี่ยนคลาส เมธอด และชื่อต่อไปนี้

BarcodeDetector

เขียนโค้ดการเริ่มต้นใหม่ตามที่แสดงในตัวอย่างนี้

GMVML Kit
NSDictionary *options = @{
    GMVDetectorBarcodeFormats : @(GMVDetectorBarcodeFormatCode128 |
                                  GMVDetectorBarcodeFormatQRCode)
};
GMVDetector *barcodeDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeBarcode options:options];
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 คุณสามารถเรียกใช้ API การอนุมานแบบซิงค์หรือไม่ซิงค์ก็ได้

- (nullable NSArray<MLKBarcode *> *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKBarcodeScanningCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

เปลี่ยนคลาส เมธอด และชื่อต่อไปนี้

TextRecognition

เขียนโค้ดการเริ่มต้นใหม่ตามที่แสดงในตัวอย่างนี้

GMVML Kit
GMVDetector *textDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeText options:nil];
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 คุณสามารถเรียกใช้ API การอนุมานแบบซิงค์หรือไม่ซิงค์ก็ได้

- (nullable MLKText *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKTextRecognitionCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

เปลี่ยนคลาส เมธอด และชื่อต่อไปนี้

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

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