Tesseract不会识别png文件中的验证码,该文件包含英文字母的数字和字母

问题描述 投票:0回答:1

我需要从url中提取验证码并使用Tesseract识别它。我的代码是:

#!/usr/bin/perl -X
###
$user = 'user'; #Enter your username here
$pass = 'pass'; #Enter your password here
###
#Server settings
$home = "http://perltest.adavice.com";
$url = "$home/c/test.cgi?u=$user&p=$pass";
#Get HTML code!
$html = `GET "$url"`
###Add code here!
#Grab img from HTML code
if ($html =~ m%img[^>]*src="(/[^"]*)"%s)
{
    $img = $1;
}
###
die "<img> not found\n" if (!$img);
#Download image to server (save as: ocr_me.img)
print "GET '$home$img' > ocr_me.img\n";
system "GET '$home$img' > ocr_me.img";
###Add code here!
#Run OCR (using shell command tesseract) on img and save text as ocr_result.txt
system("tesseract ocr_me.img ocr_result");
print "GET '$txt' > ocr_result.txt\n";
system "GET '$txt' > ocr_result.txt";
###
die "ocr_result.txt not found\n" if (!-e "ocr_result.txt");
# check OCR results:
$txt = 'cat ocr_result.txt';
$txt =~ s/[^A-Za-z0-9\-_\.]+//sg;
$img =~ s/^.*\///;
print `echo -n "file=$img&text=$txt" | POST "$url"`;

图像正确解析。此图片包含captcha,看起来像:

My image PNG file, which contains a captcha

我的输出是:

GET 'http://perltest.adavice.com/captcha/1533110309.png' > ocr_me.img
Tesseract Open Source OCR Engine v3.02.02 with Leptonica
GET '' > ocr_result.txt
Captcha text not specified

如您所见,脚本正确解析图像。但Tesseract在PNG文件中没有看到任何内容。我试图用shell命令tesseract指定其他参数,如-psm和-l,但这也没有给出任何内容

更新:阅读答案@Dave Cross后,我尝试了他的建议。

在输出中我得到:

http://perltest.adavice.com/captcha/1533141024.png
ocr_me.img
Tesseract Open Source OCR Engine v3.02.02 with Leptonica
[]
200Captcha text not specified
Original image file not specified
Captcha text not specified

为什么我需要来自图像.PNG的文字?也许这些额外的信息可以帮助您。看看:enter image description here

这就是$ url在浏览器中的样子。我的目标是使用perl在wim中为此页面创建查询。为此,我需要填写$ user,$ pass和$ txt之上的表格(来自Tesseract图像的识别)。并使用POST'url'发送它(代码中的最后一个字符串)。

perl ocr tesseract captcha
1个回答
2
投票

这里发生了几件奇怪的事情。他们中的任何一个都可能导致你的问题。

  1. 在你的shebang线上拥有-X是一个糟糕的主意。它明确地关闭了警告。我建议你删除它,将use warnings添加到你的代码中并修复所有显示的问题(我建议也添加use strict,但你需要声明所有的变量)。
  2. 我建议使用LWP::Simple而不是炮轰GET
  3. 请不要使用正则表达式来解析HTML。请改用真正的HTML解析器。 Web::Query是我目前的最爱。
  4. 然后使用名为GET且没有值的变量再次运行$txt。那不行!
  5. $txt = 'cat ocr_result.txt'没有做你认为它做的事情。你想要反引号,而不是单引号。

更新:显然,我无法访问您的用户名或密码,因此我无法重建您的所有代码。但这似乎适用于访问示例中的图像并从中提取文本。

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use LWP::Simple;

my $img_url  = 'http://perltest.adavice.com/captcha/1533110309.png';
my $img_file = 'ocr_me.img';

getstore($img_url, $img_file);

my $txt = `tesseract $img_file stdout`;

say $txt;

这是你的实际错误:

system("tesseract ocr_me.img ocr_result");
print "GET '$txt' > ocr_result.txt\n";
system "GET '$txt' > ocr_result.txt";

你要求tesseract将其输出写入ocr_result.txt,但两行后,你用GET失败调用的输出覆盖该文件。我不确定你认为会发生什么,但它会废弃tesseract已存储在该文件中的任何输出。

更新的更新:

这是我当前版本的代码:

#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use LWP::Simple qw[$ua get getstore];
use File::Basename;
###
my $user = 'xxxx'; #Enter your username here
my $pass = 'xxxx'; #Enter your password here
###
#Server settings
my $home = "http://perltest.adavice.com";
my $url = "$home/c/test.cgi?u=$user&p=$pass";
#Get HTML code!
my $html = get($url);
my $img;
###Add code here!
#Grab img from HTML code
if ($html =~ m%img[^>]*src="(/[^"]*)"%s)
{
    $img = $1;
}
my $img_url = $home . $img;
my $img_file = 'ocr_me.img';

getstore($img_url, $img_file);

say $img_url;
say $img_file;

# Looks like tesseract adds two newlines to its output -
# so chomp() it twice!
chomp(my $txt = `tesseract ocr_me.img stdout`);
chomp($txt);

say "[$txt]";

$txt =~ s/\W+//g;

my $resp = $ua->post($url, {
  u    => $user,
  p    => $pass,
  file => basename($img),
  text => $txt,
});

print $resp->code;
print $resp->content;

我改变了一些东西。

  1. 修正了从$img_url$url . $img$home . $img(这是阻止它获得正确图像的原因)。
  2. 切换到使用LWP :: Simple(它更简单)。
  3. chomped(两次!)来自tesseract的输出删除换行符。
  4. 使用File :: Basename获取正确的文件名以在最终的POST中传递。
  5. $txt之前删除了POST中的任何非单词字符。

它仍然不太有用。它似乎挂起等待服务器的响应。但我担心我没时间帮助你了。

© www.soinside.com 2019 - 2024. All rights reserved.