在 iOS 上使用 ML Kit 辨識圖片中的文字

您可以使用機器學習套件辨識圖片或影片中的文字,例如路標的文字。這項功能的主要特徵如下:

文字辨識 API
說明辨識圖片或影片中的拉丁字母文字。
SDK 名稱GoogleMLKit/TextRecognition (version 2.2.0)
實作在建構期間,資產會以靜態方式連結至您的應用程式。
應用程式大小影響大約 20 MB
效能在多數裝置上即時執行。

立即體驗

事前準備

  1. 在 Podfile 中加入下列機器學習套件 Pod:
    pod 'GoogleMLKit/TextRecognition','2.2.0'
    
  2. 安裝或更新專案的 Pod 後,使用 .xcworkspace 開啟 Xcode 專案。Xcode 12.4 以上版本支援機器學習套件。

1. 建立 TextRecognizer 的執行個體

呼叫 +textRecognizer 來建立 TextRecognizer 的執行個體:
SwiftObjective-C
let textRecognizer = TextRecognizer.textRecognizer()
     
MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];
     

2. 準備輸入圖片

將圖片做為 UIImageCMSampleBufferRef 傳遞至 TextRecognizerprocess(_:completion:) 方法:

使用 UIImageCMSampleBuffer 建立 VisionImage 物件。

如果您使用 UIImage,請按照下列步驟操作:

  • 使用 UIImage 建立 VisionImage 物件。請務必指定正確的 .orientation
    SwiftObjective-C
    let image = VisionImage(image: UIImage)
    visionImage
    .orientation = image.imageOrientation
    MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
    visionImage
    .orientation = image.imageOrientation;

如果您使用 CMSampleBuffer,請按照下列步驟操作:

  • 指定 CMSampleBuffer 中包含的圖片資料方向。

    如何取得圖片方向:

    SwiftObjective-C
    func imageOrientation(
      deviceOrientation
    : UIDeviceOrientation,
      cameraPosition
    : AVCaptureDevice.Position
    ) -> UIImage.Orientation {
     
    switch deviceOrientation {
     
    case .portrait:
       
    return cameraPosition == .front ? .leftMirrored : .right
     
    case .landscapeLeft:
       
    return cameraPosition == .front ? .downMirrored : .up
     
    case .portraitUpsideDown:
       
    return cameraPosition == .front ? .rightMirrored : .left
     
    case .landscapeRight:
       
    return cameraPosition == .front ? .upMirrored : .down
     
    case .faceDown, .faceUp, .unknown:
       
    return .up
     
    }
    }
         
    - (UIImageOrientation)
      imageOrientationFromDeviceOrientation
    :(UIDeviceOrientation)deviceOrientation
                             cameraPosition
    :(AVCaptureDevicePosition)cameraPosition {
     
    switch (deviceOrientation) {
       
    case UIDeviceOrientationPortrait:
         
    return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored
                                                               
    : UIImageOrientationRight;

       
    case UIDeviceOrientationLandscapeLeft:
         
    return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored
                                                               
    : UIImageOrientationUp;
       
    case UIDeviceOrientationPortraitUpsideDown:
         
    return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored
                                                               
    : UIImageOrientationLeft;
       
    case UIDeviceOrientationLandscapeRight:
         
    return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored
                                                               
    : UIImageOrientationDown;
       
    case UIDeviceOrientationUnknown:
       
    case UIDeviceOrientationFaceUp:
       
    case UIDeviceOrientationFaceDown:
         
    return UIImageOrientationUp;
     
    }
    }
         
  • 使用 CMSampleBuffer 物件和方向建立 VisionImage 物件:
    SwiftObjective-C
    let image = VisionImage(buffer: sampleBuffer)
    image
    .orientation = imageOrientation(
      deviceOrientation
    : UIDevice.current.orientation,
      cameraPosition
    : cameraPosition)
     MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer];
     image
    .orientation =
       
    [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                    cameraPosition
    :cameraPosition];

3. 處理圖片

接著,將圖片傳送至 process(_:completion:) 方法:

SwiftObjective-C
textRecognizer.process(visionImage) { result, error in
  guard error
== nil, let result = result else {
   
// Error handling
   
return
 
}
 
// Recognized text
}
[textRecognizer processImage:image
                  completion
:^(MLKText *_Nullable result,
                               
NSError *_Nullable error) {
 
if (error != nil || result == nil) {
   
// Error handling
   
return;
 
}
 
// Recognized text
}];

4. 從已辨識的文字區塊擷取文字

如果文字辨識作業成功,系統會傳回 Text 物件。Text 物件包含圖像中辨識的完整文字,以及零或多個 TextBlock 物件。

每個 TextBlock 都是矩形文字區塊,其中包含零個或多個 TextLine 物件。每個 TextLine 物件都包含零個或多個 TextElement 物件,這些物件代表字詞和類似實體的實體,例如日期和數字。

針對各個 TextBlockTextLineTextElement 物件,您可以取得系統辨識的區域和邊界座標。

例如:

SwiftObjective-C
let resultText = result.text
for block in result.blocks {
    let blockText
= block.text
    let blockLanguages
= block.recognizedLanguages
    let blockCornerPoints
= block.cornerPoints
    let blockFrame
= block.frame
   
for line in block.lines {
        let lineText
= line.text
        let lineLanguages
= line.recognizedLanguages
        let lineCornerPoints
= line.cornerPoints
        let lineFrame
= line.frame
       
for element in line.elements {
            let elementText
= element.text
            let elementCornerPoints
= element.cornerPoints
            let elementFrame
= element.frame
       
}
   
}
}
NSString *resultText = result.text;
for (MLKTextBlock *block in result.blocks) {
 
NSString *blockText = block.text;
 
NSArray<MLKTextRecognizedLanguage *> *blockLanguages = block.recognizedLanguages;
 
NSArray<NSValue *> *blockCornerPoints = block.cornerPoints;
 
CGRect blockFrame = block.frame;
 
for (MLKTextLine *line in block.lines) {
   
NSString *lineText = line.text;
   
NSArray<MLKTextRecognizedLanguage *> *lineLanguages = line.recognizedLanguages;
   
NSArray<NSValue *> *lineCornerPoints = line.cornerPoints;
   
CGRect lineFrame = line.frame;
   
for (MLKTextElement *element in line.elements) {
     
NSString *elementText = element.text;
     
NSArray<NSValue *> *elementCornerPoints = element.cornerPoints;
     
CGRect elementFrame = element.frame;
   
}
 
}
}

輸入圖片規範

  • 為了讓機器學習套件能正確識別文字,輸入圖片必須包含以足夠像素資料呈現的文字。在理想情況下,每個字元至少應為 16x16 像素。一般來說,如果字元大於 24x24 像素,通常就沒有準確率。

    例如,640x480 圖片可能有助於掃描佔滿圖片寬度的名片。如要掃描以字母大小列印的文件,您可能需要提供 720x1280 像素的圖片。

  • 圖片焦點不佳可能會影響文字辨識的準確度。如果無法收到可接受的結果,請嘗試請使用者重新擷取圖片。

  • 如果您要在即時應用程式中辨識文字,應考慮輸入圖片的整體尺寸。較小型的影像處理速度較快。為了縮短延遲時間,請確保文字會盡可能佔用最多圖片,並以較低解析度擷取圖片 (請注意上述的準確率規定)。如需詳細資訊,請參閱效能改善提示

改善成效的訣竅

  • 如要處理影格,請使用偵測工具的 results(in:) 同步 API。從 AVCaptureVideoDataOutputSampleBufferDelegatecaptureOutput(_, didOutput:from:) 函式呼叫此方法,即可同步取得特定影片影格的結果。將 AVCaptureVideoDataOutputalwaysDiscardsLateVideoFrames 保留為 true,以限制對偵測工具的呼叫。假如在偵測器執行期間有新的視訊畫面可用,系統就會捨棄該影格。
  • 如果您使用偵測工具的輸出內容,為輸入圖片上的圖像重疊,請先透過 ML Kit 取得結果,然後透過單一步驟算繪圖像和疊加層。如此一來,每個處理的輸入影格只會轉譯一次到顯示途徑一次。如需範例,請參閱 ML Kit 快速入門導覽課程範例中的 updatePreviewOverlayViewWithLastFrame
  • 請考慮以較低的解析度拍照。同時也請注意,此 API 的圖片尺寸規定。