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')
发布了25 篇原创文章 · 获赞 37 · 访问量 2924

猜你喜欢

转载自blog.csdn.net/qq_41297934/article/details/105302006
今日推荐