thiagoalessio/tesseract_ocr

PHP中使用Tesseract OCR的包装器。

2.13.0 2023-10-05 21:14 UTC

README

Tesseract OCR for PHP

Tesseract OCR for PHP

PHP中使用Tesseract OCR的包装器。

CI AppVeyor Codacy Test Coverage
Latest Stable Version Total Downloads Monthly Downloads

安装

通过Composer

$ composer require thiagoalessio/tesseract_ocr

‼️ 此库依赖于Tesseract OCR,版本3.02或更高。


Windows用户注意

在您的系统上安装Tesseract OCR有多种方式,但如果您只想快速启动,我建议使用Capture2Text包与Chocolatey

choco install capture2text --version 3.9

⚠️ Capture2Text的最近版本停止提供tesseract二进制文件。


macOS用户注意

使用MacPorts,您可以这样安装对个别语言的支持

$ sudo port install tesseract-<langcode>

但是,使用Homebrew则不可行。它默认只提供对英语的支持,因此如果您打算使用其他语言,最快速的方法是安装所有语言

$ brew install tesseract tesseract-lang

用法

基本用法

use thiagoalessio\TesseractOCR\TesseractOCR;
echo (new TesseractOCR('text.png'))
    ->run();
The quick brown fox
jumps over
the lazy dog.

其他语言

use thiagoalessio\TesseractOCR\TesseractOCR;
echo (new TesseractOCR('german.png'))
    ->lang('deu')
    ->run();
Bülowstraße

多语言

use thiagoalessio\TesseractOCR\TesseractOCR;
echo (new TesseractOCR('mixed-languages.png'))
    ->lang('eng', 'jpn', 'spa')
    ->run();
I eat すし y Pollo

诱导识别

use thiagoalessio\TesseractOCR\TesseractOCR;
echo (new TesseractOCR('8055.png'))
    ->allowlist(range('A', 'Z'))
    ->run();
BOSS

破解CAPTCHA

是的,我知道有些人可能想用这个库来破解CAPTCHA的高尚目的,所以请看看这个评论

#91 (评论)

API

run

执行一个tesseract命令,可选地接收一个整数作为timeout,以防您遇到停滞的tesseract进程。

$ocr = new TesseractOCR();
$ocr->run();
$ocr = new TesseractOCR();
$timeout = 500;
$ocr->run($timeout);

image

定义由tesseract识别的图像的路径。

$ocr = new TesseractOCR();
$ocr->image('/path/to/image.png');
$ocr->run();

imageData

将图像设置为由tesseract识别,从一个字符串中设置,并指定其大小。这在处理已经加载到内存中的文件时非常有用。您可以轻松检索图像对象的数据和大小

//Using Imagick
$data = $img->getImageBlob();
$size = $img->getImageLength();
//Using GD
ob_start();
// Note that you can use any format supported by tesseract
imagepng($img, null, 0);
$size = ob_get_length();
$data = ob_get_clean();

$ocr = new TesseractOCR();
$ocr->imageData($data, $size);
$ocr->run();

executable

如果由于任何原因,tesseract可执行文件不在$PATH中,则定义一个自定义的tesseract可执行文件位置。

echo (new TesseractOCR('img.png'))
    ->executable('/path/to/tesseract')
    ->run();

version

返回当前tesseract的版本。

echo (new TesseractOCR())->version();

availableLanguages

返回可用语言/脚本列表。

foreach((new TesseractOCR())->availableLanguages() as $lang) echo $lang;

更多信息: https://github.com/tesseract-ocr/tesseract/blob/master/doc/tesseract.1.asc#languages-and-scripts

tessdataDir

指定tessdata目录的自定义位置。

echo (new TesseractOCR('img.png'))
    ->tessdataDir('/path')
    ->run();

userWords

指定用户词文件的位置。

这是一个纯文本文件,包含你想要tesseract将其视为正常字典单词的单词列表。

当处理包含技术术语、行话等内容时非常有用。

$ cat /path/to/user-words.txt
foo
bar
echo (new TesseractOCR('img.png'))
    ->userWords('/path/to/user-words.txt')
    ->run();

userPatterns

指定用户模式文件的位置。

如果你处理的内容有已知模式,此选项可以大大提高tesseract的识别准确度。

$ cat /path/to/user-patterns.txt'
1-\d\d\d-GOOG-441
www.\n\\\*.com
echo (new TesseractOCR('img.png'))
    ->userPatterns('/path/to/user-patterns.txt')
    ->run();

lang

定义在识别过程中要使用的一个或多个语言。可用语言的完整列表可以在以下位置找到:https://github.com/tesseract-ocr/tesseract/blob/master/doc/tesseract.1.asc#languages

提示来自@daijiale 使用组合 ->lang('chi_sim', 'chi_tra') 正确识别中文。

 echo (new TesseractOCR('img.png'))
     ->lang('lang1', 'lang2', 'lang3')
     ->run();

psm

指定页面分割方法,它指导tesseract如何解释给定的图像。

更多信息: https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality#page-segmentation-method

echo (new TesseractOCR('img.png'))
    ->psm(6)
    ->run();

oem

指定OCR引擎模式。(见tesseract --help-oem

echo (new TesseractOCR('img.png'))
    ->oem(2)
    ->run();

dpi

指定图像DPI。如果您的图像在其元数据中不包含此信息,则很有用。

echo (new TesseractOCR('img.png'))
    ->dpi(300)
    ->run();

allowlist

这是->config('tessedit_char_whitelist', 'abcdef....')的快捷方式。

echo (new TesseractOCR('img.png'))
    ->allowlist(range('a', 'z'), range(0, 9), '-_@')
    ->run();

configFile

指定要使用的配置文件。它可以是您自己的配置文件路径,也可以是预定义配置文件之一:https://github.com/tesseract-ocr/tesseract/tree/master/tessdata/configs

echo (new TesseractOCR('img.png'))
    ->configFile('hocr')
    ->run();

setOutputFile

指定要使用的输出文件。请注意:如果您设置了输出文件,则忽略withoutTempFiles选项。即使withoutTempFiles = true,也会写入(并删除)临时文件。

configFile结合使用,您可以获得hocrtsvpdf文件。

echo (new TesseractOCR('img.png'))
    ->configFile('pdf')
    ->setOutputFile('/PATH_TO_MY_OUTPUTFILE/searchable.pdf')
    ->run();

digits

->configFile('digits')的快捷方式。

echo (new TesseractOCR('img.png'))
    ->digits()
    ->run();

hocr

->configFile('hocr')的快捷方式。

echo (new TesseractOCR('img.png'))
    ->hocr()
    ->run();

pdf

->configFile('pdf')的快捷方式。

echo (new TesseractOCR('img.png'))
    ->pdf()
    ->run();

quiet

->configFile('quiet')的快捷方式。

echo (new TesseractOCR('img.png'))
    ->quiet()
    ->run();

tsv

->configFile('tsv')的快捷方式。

echo (new TesseractOCR('img.png'))
    ->tsv()
    ->run();

txt

->configFile('txt')的快捷方式。

echo (new TesseractOCR('img.png'))
    ->txt()
    ->run();

tempDir

定义一个自定义目录以存储tesseract生成的临时文件。请确保该目录确实存在,并且运行php的用户允许写入其中。

echo (new TesseractOCR('img.png'))
    ->tempDir('./my/custom/temp/dir')
    ->run();

withoutTempFiles

指定tesseract应输出识别的文本而不写入临时文件。数据是从tesseract的标准输出中收集的。

echo (new TesseractOCR('img.png'))
    ->withoutTempFiles()
    ->run();

其他选项

任何Tesseract提供的配置选项都可以这样使用

echo (new TesseractOCR('img.png'))
    ->config('config_var', 'value')
    ->config('other_config_var', 'other value')
    ->run();

或者这样

echo (new TesseractOCR('img.png'))
    ->configVar('value')
    ->otherConfigVar('other value')
    ->run();

更多信息: https://github.com/tesseract-ocr/tesseract/wiki/ControlParams

Thread-limit

有时,限制tesseract可以使用的线程数可能很有用(例如,在此情况下)。将最大线程数作为run函数的参数设置。

echo (new TesseractOCR('img.png'))
    ->threadLimit(1)
    ->run();

如何贡献

您可以通过以下方式为此项目做出贡献

  • 如果您发现错误或希望提出新功能,请打开一个问题
  • 提交一个包含修复bug、缺失/错误文档或实现新功能的Pull Request

请确保您查看我们的行为准则贡献指南

许可证

tesseract-ocr-for-php遵循MIT许可证发布。

在柏林用love打造