【发布时间】:2018-08-01 08:30:18
【问题描述】:
我需要从 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"`;
图像解析正确。此图像包含验证码,如下所示:
我的输出是:
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'(代码中的最后一个字符串)发送。
【问题讨论】:
-
你确定没有?文件“ocr_result”包含什么?
-
看起来你没有在任何地方定义 $txt 所以你有一个空的 ocr_result.txt
-
将
use strict; use warnings;添加到所有脚本的顶部,并使用my(例如my $variable)初始化变量以帮助消除简单的错误。 -
@Chris Turner,“ocr_result”需要包含验证码中的文本。但在我之前的代码中,我可以看到,我没有从 Tesseract 得到任何文本到这个文件
-
@Oysiyl:像程序员一样思考。
标签: perl ocr tesseract captcha