lecodeurdudimanche / document-data-extractor
一个简单的库,用于从具有已知结构的文档中提取数据
0.1.1
2019-11-12 12:51 UTC
Requires
- thiagoalessio/tesseract_ocr: >=2.8.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-17 07:46:54 UTC
README
一个简单的PHP库,用于自动从具有已知格式的文档中提取数据。
需求
此库使用Tesseract从文档中读取文本,并使用Imagick操作图像。
它依赖于GhostScript (gs
) 将PDF文件转换为图像。
安装
安装所需的PHP库:php-imagick
。在Ubuntu上
apt install php7-imagick
然后通过composer安装包
composer require lecodeurdudimanche/document-data-extractor
用法
首先,您需要定义您想要提取的数据以及它在图像上的位置
$extractor = new Extractor(); $regionsOfInterest = [ // The name of the company is in the rectangle with the top left corner (700, 180) and a size of (1080, 160) new ROI('Name of the company')->setRect(700, 180, 1080, 160), new ROI('Total', 'integer')->setRect(1980, 1572, 58, 52); ];
接下来,您可以添加一些传递给tesseract的选项,以获得更精确的结果
$tesseractConfiguration = [ 'psm' => 8, // Page segmentation method is set to 8 (single word) 'tessdataDir' => '/usr/share/tessdata' // Other tesseract options ... ]; $config = Configuration::fromArray(compact('regionsOfInterest', 'tesseractConfiguration')); $extractor->setConfig($config);
然后,您设置要从其中提取数据的文档
$extractor->loadImage('/path/to/image.png'); // or $extractor->loadPDF('/path/to/document.pdf'); // or $extractor->setImage($imageData); // could be an Imagick or GD image or raw image data
最后,您调用run()
方法来提取数据
$data = $extractor->run(); /* * $data = [ * ['label' => 'Name of the company', 'type' => 'text', 'data' => 'Company Limited'], * ['label' => 'Total', 'type' => 'integer', 'data' => '55'] * ]; */
您可以使用toFile
和fromFile
方法保存和加载Configuration
对象。文件格式是格式化的JSON。