Tesseract 不识别 png 文件中的验证码,其中包含数字和英文字母
Tesseract dont recognize captcha in png file, which contains numbers and letters of the English alphabet
我需要从 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 = ;
}
###
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"`;
图片解析正确。此图片包含验证码,看起来像:
我的输出是:
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 中的文本?也许这些附加信息可以帮助您。
看那个:
这就是 $url 在浏览器中的样子。我的目标是使用 perl 在 wim 中为此页面创建查询。为此,我需要在我的 $user、$pass 和 $txt 上方填写表格(来自使用 Tesseract 图像识别)。并使用 POST 'url'(代码中的最后一个字符串)发送它。
这里发生了几件奇怪的事情。其中任何一个都可能导致您的问题。
- 在你的 shebang 行上加入
-X
是个糟糕的主意。它明确关闭警告。我建议您删除它,将 use warnings
添加到您的代码并修复所有揭示的问题(我建议也添加 use strict
,但您需要声明所有变量)。
- 我建议使用 LWP::Simple 而不是炮击到
GET
。
- 请不要使用正则表达式来解析 HTML。请改用真正的 HTML 解析器。 Web::Query 是我目前的最爱。
- 然后您再次 运行
GET
,使用名为 $txt
的没有值的变量。那是行不通的!
$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 = ;
}
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;
我改变了一些东西。
- 已将
$img_url
从 $url . $img
更正为 $home . $img
(这是阻止它获取正确图像的原因)。
- 自始至终都改用 LWP::Simple(更简单)。
chomp
编辑(两次!)tesseract
的输出以删除换行符。
- 使用 File::Basename 获取正确的文件名以传递到最后的
POST
。
- 在
POST
之前从 $txt
中删除了所有非单词字符。
还是不行。它似乎挂起等待服务器的响应。但我恐怕 运行 没有时间帮助你。
我需要从 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 = ;
}
###
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"`;
图片解析正确。此图片包含验证码,看起来像:
我的输出是:
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 中的文本?也许这些附加信息可以帮助您。
看那个:
这就是 $url 在浏览器中的样子。我的目标是使用 perl 在 wim 中为此页面创建查询。为此,我需要在我的 $user、$pass 和 $txt 上方填写表格(来自使用 Tesseract 图像识别)。并使用 POST 'url'(代码中的最后一个字符串)发送它。
这里发生了几件奇怪的事情。其中任何一个都可能导致您的问题。
- 在你的 shebang 行上加入
-X
是个糟糕的主意。它明确关闭警告。我建议您删除它,将use warnings
添加到您的代码并修复所有揭示的问题(我建议也添加use strict
,但您需要声明所有变量)。 - 我建议使用 LWP::Simple 而不是炮击到
GET
。 - 请不要使用正则表达式来解析 HTML。请改用真正的 HTML 解析器。 Web::Query 是我目前的最爱。
- 然后您再次 运行
GET
,使用名为$txt
的没有值的变量。那是行不通的! $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 = ;
}
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;
我改变了一些东西。
- 已将
$img_url
从$url . $img
更正为$home . $img
(这是阻止它获取正确图像的原因)。 - 自始至终都改用 LWP::Simple(更简单)。
chomp
编辑(两次!)tesseract
的输出以删除换行符。- 使用 File::Basename 获取正确的文件名以传递到最后的
POST
。 - 在
POST
之前从$txt
中删除了所有非单词字符。
还是不行。它似乎挂起等待服务器的响应。但我恐怕 运行 没有时间帮助你。