【问题标题】:Read text in image with PHP使用 PHP 读取图像中的文本
【发布时间】:2012-11-18 14:06:19
【问题描述】:

我正在尝试从这张图片中读取文字:

我想阅读价格,例如"EUR42721.92"

我尝试了这些库:

  1. How to Create a PHP Captcha Decoder with PHP OCR Class: Recognize text & objects in graphical images - PHP Classes
  2. phpOCR: Optical Character Recognizer written in PHP

但它们不起作用。如何阅读文本?

【问题讨论】:

  • 为什么它不起作用?您收到一些错误消息吗?
  • 两个脚本都没有读取文本和数字.. 示例:EUR42450.92 >> 输出类似于:787988.. 我想要这样; free-ocr.com
  • tesseract (code.google.com/p/tesseract-ocr) 是开源 OCR 库/程序。我用它取得了相当不错的结果。
  • 省点力气怎么样? stackoverflow.com/questions/4791189/…
  • goldprice.org 是否可能仅以图形文件的形式提供价格以防止屏幕刮擦?他们可能会反对您以这种方式使用他们的数据。他们在网站上是否有任何条款和条件,您会因为这样做而破坏?他们是否提供了您可以使用的 API?

标签: php curl image-processing ocr gd


【解决方案1】:

试试这个(它对我有用):

$imagick = new Imagick($filePath);

$size = $imagick->getImageGeometry();
$width     = $size['width'];
$height    = $size['height'];
unset($size);

$textBottomPosition = $height-1;
$textRightPosition = $width;

$black = new ImagickPixel('#000000');
$gray  = new ImagickPixel('#C0C0C0');

$textRight  = 0;
$textLeft   = 0;
$textBottom = 0;
$textTop    = $height;

$foundGray = false;

for($x= 0; $x < $width; ++$x) {
    for($y = 0; $y < $height; ++$y) {
        $pixel = $imagick->getImagePixelColor($x, $y);
        $color = $pixel->getColor();
        // remove alpha component
        $pixel->setColor('rgb(' . $color['r'] . ','
                         . $color['g'] . ','
                         . $color['b'] . ')');

        // find the first gray pixel and ignore pixels below the gray
        if( $pixel->isSimilar($gray, .25) ) {
            $foundGray = true;
            break;
        }

        // find the text boundaries 
        if( $foundGray && $pixel->isSimilar($black, .25) ) {
            if( $textLeft === 0 ) {
                $textLeft = $x;
            } else {
                $textRight = $x;
            }

            if( $y < $textTop ) {
                $textTop = $y;
            }

            if( $y > $textBottom ) {
                $textBottom = $y;
            }
        }
    }
}

$textWidth = $textRight - $textLeft;
$textHeight = $textBottom - $textTop;
$imagick->cropImage($textWidth+10, $textHeight+10, $textLeft-5, $textTop-5);
$imagick->scaleImage($textWidth*10, $textHeight*10, true);

$textFilePath = tempnam('/temp', 'text-ocr-') . '.png';
$imagick->writeImage($textFilePath);

$text = str_replace(' ', '', shell_exec('gocr ' . escapeshellarg($textFilePath)));
unlink($textFilePath);
var_dump($text);

您需要安装 ImageMagick 扩展和 GOCR 才能运行它。 如果您不能或不想安装 ImageMagick 扩展,我会给您发送一个带有计算颜色距离函数的 GD 版本(它只是一个扩展的勾股定理)。

不要忘记设置 $filePath 值。

图像显示它寻找一个灰色像素来更改 $foundGray 标志。 之后,它会从左侧和顶部查找第一个和最后一个像素。 它使用一些填充裁剪图像,调整生成的图像大小并将其保存到临时文件中。之后,很容易使用 gocr(或任何其他 OCR 命令或库)。之后可以删除临时文件。

【讨论】:

    【解决方案2】:

    在开始 OCR 之前提高数字图像的质量。使用绘图程序来提高质量(更大的尺寸、直线)。

    您可以修改 PHP 脚本并使模式识别适应您的需要。 https://github.com/ogres/PHP-OCR/blob/master/Image2String.php

    或尝试其他 OCR 工具: https://github.com/thiagoalessio/tesseract-ocr-for-php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多