Python「pytesseract」:中文識別模塊

在處理 .ttf 文件時,遇到了識別圖片中中文的情況,常見的方式是調用百度的語言識別接口,但是這裏爲了大批量的識別,首先試了試 python 自帶的語言識別模塊 pytesseract ,這裏簡單做一下記錄。

目錄

一、介紹

(一)image_to_string

(二)config參數 

二、代碼


一、介紹

(一)image_to_string

pytesseract.image_to_string

def image_to_string(
    image, lang=None, config='', nice=0, output_type=Output.STRING, timeout=0,
):
    """
    Returns the result of a Tesseract OCR run on the provided image to string
    將在提供的圖像上運行的Tesseract OCR的結果返回給string
    """

參數含義: 

  • image Object or String - PIL Image/NumPy array or file path of the image to be processed by Tesseract. If you pass object instead of file path, pytesseract will implicitly convert the image to RGB mode.

對象或者字符串。PIL Image/NumPy 數組或者是Tesseract處理圖像的文件路徑。如果傳來的是一個對象的話,會把圖像轉換成RGB格式。

  • lang String - Tesseract language code string. Defaults to eng if not specified! Example for multiple languages: lang='eng+fra'

語言碼。默認自然是英語(eng),這裏中文簡體爲chi_sim,當然也可以採用多種語言。

  • config String - Any additional custom configuration flags that are not available via the pytesseract function. For example: config='--psm 6'

任何通過pytesseract函數不可用的額外自定義配置標誌。

  • nice Integer - modifies the processor priority for the Tesseract run. Not supported on Windows. Nice adjusts the niceness of unix-like processes.

修改Tesseract運行的處理器優先級。不支持windows系統。Nice調整類unix系統進程的良好性。

  • output_type Class attribute - specifies the type of the output, defaults to string. For the full list of all supported types, please check the definition of pytesseract.Output class.

輸出的特定方式,默認是string。

  • timeout Integer or Float - duration in seconds for the OCR processing, after which, pytesseract will terminate and raise RuntimeError.

OCR處理的持續時間(以秒爲單位),在此之後,pytesseract將終止並引發RuntimeError。

(二)config參數 

 這裏詳細介紹一下 config 參數,通過命令行執行 tesseract --help-psm 命令,我們可以看到

$ tesseract --help-psm
Page segmentation modes:
  0    Orientation and script detection (OSD) only.

方向和腳本檢測(OSD)。
  1    Automatic page segmentation with OSD.

自動頁面分割與OSD。
  2    Automatic page segmentation, but no OSD, or OCR.

自動頁面分割,但沒有OSD,或OCR。
  3    Fully automatic page segmentation, but no OSD. (Default)

全自動頁面分割,但沒有OSD。(默認)
  4    Assume a single column of text of variable sizes.

假設有一列大小不同的文本。
  5    Assume a single uniform block of vertically aligned text.

假設有一個垂直對齊的文本塊。
  6    Assume a single uniform block of text.

包含一個統一的文本塊。
  7    Treat the image as a single text line.

將圖像重新定義爲單個文本行。
  8    Treat the image as a single word.

將圖像視爲一個單詞。
  9    Treat the image as a single word in a circle.

將圖像視爲一個圓圈中的單個單詞。
 10    Treat the image as a single character.

將圖像視爲單個字符。
 11    Sparse text. Find as much text as possible in no particular order.

稀疏的文本。在沒有特定順序的情況下,儘可能多地查找文本。
 12    Sparse text with OSD.

稀疏文本與OSD。
 13    Raw line. Treat the image as a single text line,
       bypassing hacks that are Tesseract-specific.

將圖像視爲一個單獨的文本行,繞過特定於tesseract的技巧。

二、代碼

使用如下所示:

import pytesseract
from PIL import Image


# 識別
text = pytesseract.image_to_string(Image.open('圖片路徑'), lang='chi_sim', config='-psm 6')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章