编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Tesseract使用初步(tesseract安装教程)

wxchong 2024-07-21 07:27:14 开源技术 15 ℃ 0 评论

Tesseract 是一个 OCR 库,目前由 Google 赞助。Tesseract是目前公认最优秀,最精确的开源 OCR 系统。除了极高的精确度,Tesseract也具有很高的灵活性。它可以通过训练识别出任何字体,也可以识别出任何 Unicode 字符。

Tesseract 是C++语言开发的二进制软件,使用 CLI 进行交互,也提供 API 接口(C++语言)以便其他语言调用(如Python)。

Tesseract 系统包含:bin(二进制文件),includes(C/C++接口),share(训练数据和配置文件),lib(库文件)。

对于环境的依赖:giflib,jpeg,libpng,libtiff,little-cms2,openjpeg,webp,leptonica

一、安装

由于个人使用Mac Pro,所以这里记录的MacOS下安装(其实就是二进制文件)

MacOS

brew install tesseract
brew info tesseract #查看安装信息

遇到权限问题(/usr/local/lib/pkgconfig),可以设置当前用户为目录属主

sudo chown -R $(whoami) /usr/local/lib/pkgconfig

其他的操作系统安装可以参考链接:

https://tesseract-ocr.github.io/tessdoc/Installation.html

Docker

docker pull tesseractshadow/tesseract4re

docker有re(运行时)和comp(编译时)两种镜像。


下载语言包(训练好的数据),下载文件放在目录 /usr/local/Cellar/tesseract/4.1.1/share/tessdata 中(其中4.1.1是 tesseract 版本)

简体中文
https://raw.githubusercontent.com/tesseract-ocr/tessdata/master/chi_sim.traineddata
简体中文-竖排
https://raw.githubusercontent.com/tesseract-ocr/tessdata/master/chi_sim_vert.traineddata
繁体中文
https://raw.githubusercontent.com/tesseract-ocr/tessdata/master/chi_tra.traineddata
繁体中文-竖排
https://raw.githubusercontent.com/tesseract-ocr/tessdata/master/chi_tra_vert.traineddata

二、使用方法

先上 CLI 支持的所有使用参数

tesseract --help | --help-extra | --help-psm | --help-oem | --version
tesseract --list-langs [--tessdata-dir PATH]
tesseract --print-parameters [options...] [configfile...]
tesseract imagename|imagelist|stdin outputbase|stdout [options...] [configfile...]

1、一般使用(最简模式)

默认使用eng文字库,imgName是图片的地址,result识别结果(自动保存为result.txt,默认eng语言)

tesseract imgName result

2、指定语言

可以指定图片解析语言,比如指定使用简体中文

tesseract -l chi_sim imgName result

3、查看本地存在的语言库

tesseract --list-langs

4、指定多语言

如果图片中可能包含多种语言,需要都指定,多个语言间用+号相连,如下面的例子,图片中包含中英文,需要指定这两个语言训练数据来解析。

tesseract -l chi_sim+eng imgName result

5、其他参数

--oem 选择引擎模式(OCR Engine mode)

0    Legacy engine only.
1    Neural nets LSTM engine only.
2    Legacy + LSTM engines.
3    Default, based on what is available.

默认的引擎是0,也是数据仓库提供训练数据默认的支持格式

--psm 分割模式(page segmentation mode)

0    Orientation and script detection (OSD) only.
1    Automatic page segmentation with OSD.
2    Automatic page segmentation, but no OSD, or OCR. (not implemented)
3    Fully automatic page segmentation, but no OSD. (Default)
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.
13    Raw line. Treat the image as a single text line, bypassing hacks that are Tesseract-specific.

三、输入输出

默认输出为 "文件名.txt" 方式,如上面的例子,会将解析结果保存在当面目录的"result.txt"文档里面,如果需要直接输出到控制台标准输出,可以用关键词 stdout 来表示。

tesseract -l chi_sim+eng imgName stdout

Tesseract 也支持输出为pdf格式文档。

tesseract -l eng+chi_sim test.png test pdf

会生成"test.pdf"文档(文本模式)。

输入除了文件名方式,还支持用管道符结合 stdin 关键词来直接使用标准输入来输入图片数据流。

四、Python使用Tesseract

通过 pip 安装支持Python 版本的 Tesseract库,其实也是对Tesseract的简单封装,在使用的pytesseract前还是要安装Tesseract的。

pip install pytesseract

内部其实还是调用Tesseract进程并捕获输出来获取结果(这点显得有点Low哈)。

通过Python代码可以以更简单直观的方式获取OCR结果:

import pytesseract
from PIL import Image


image = Image.open('/Users/admin/Desktop/test.jpg')
text = pytesseract.image_to_string(image)
print text

五、C++接口

Tesseract默认提供C++接口,可以方便嵌入到系统中使用。

#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>


int main()
{
    char *outText;


    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
    // Initialize tesseract-ocr with English, without specifying tessdata path
    if (api->Init(NULL, "eng")) {
        fprintf(stderr, "Could not initialize tesseract.\n");
        exit(1);
    }


    // Open input image with leptonica library
    Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
    api->SetImage(image);
    // Get OCR result
    outText = api->GetUTF8Text();
    printf("OCR output:\n%s", outText);


    // Destroy used object and release memory
    api->End();
    delete api;
    delete [] outText;
    pixDestroy(&image);


    return 0;
}

更多例子可以参考链接:

https://tesseract-ocr.github.io/tessdoc/Examples_C++.html

六、测试结果

实际测试结果,对于印刷体(包括中文情况)准确率确实不错。



Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表