使用 tesseract 读取验证码给出了错误的读数
Reading CAPTCHA using tesseract is giving wrong readings
from urllib import urlopen,urlretrieve
from PIL import Image,ImageOps
from bs4 import BeautifulSoup
import requests
import subprocess
def cleanImage(imagePath):
image=Image.open(imagePath)
image=image.point(lambda x:0 if x<143 else 255)
borederImage=ImageOps.expand(image,border=20,fill="white")
borederImage.save(imagePath)
html=urlopen("http://www.pythonscraping.com/humans-only")
soup=BeautifulSoup(html,'html.parser')
imageLocation=soup.find('img',{'title':'Image CAPTCHA'})['src']
formBuildID=soup.find('input',{'name':'form_build_id'})['value']
captchaSID=soup.find('input',{'name':'captcha_sid'})['value']
captchaToken=soup.find('input',{'name':'captcha_token'})['value']
captchaURL="http://pythonscraping.com"+imageLocation
urlretrieve(captchaURL,"captcha.jpg")
cleanImage("captcha.jpg")
p=subprocess.Popen(['tesseract','captcha.jpg',"captcha"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p.wait()
f=open('captcha.txt','r')
captchaResponce=f.read().replace(" ","").replace("\n","")
print "captcha responce attempt "+ captchaResponce+"\n\n"
try:
print captchaResponce
print len(captchaResponce)
print type(captchaResponce)
except:
print "No way"
你好
这是我用于测试站点的代码,用于下载验证码图像(每次打开站点时都会获得不同的验证码),然后使用 python 中的 tesseract 读取它。
我尝试直接下载图像并使用 tesseract 直接读取它没有得到正确的验证码读数,所以我添加了函数 cleanImage
来帮助但它也没有正确读取.
在网上搜索后,我的问题似乎是 tesseract 没有 "trained" 正确处理图像。
非常感谢任何帮助。
**此代码来自网络抓取书,此示例的目的也是读取验证码并提交表单。这绝不是使网站过载或损害网站的攻击或进攻工具。
Tesseract
被训练做更传统的 OCR,而 CAPTCHA
对它来说非常具有挑战性,因为字符没有对齐,可能有旋转、重叠以及大小和字体不同。您应该尝试使用不同的页面分段模式(--psm
选项)调用 tesseract
。以下是所有可能值的列表:
Page segmentation modes:
0 Orientation and script detection (OSD) only.
1 Automatic page segmentation with OSD.
2 Automatic page segmentation, but no OSD, or OCR.
3 Fully automatic page segmentation, but no OSD. (Default)
4 Assume a single column of text of variable sizes.
5 Assume a single uniform block of vertically aligned text.
6 Assume a single uniform block of text.
7 Treat the image as a single text line.
8 Treat the image as a single word.
9 Treat the image as a single word in a circle.
10 Treat the image as a single character.
11 Sparse text. Find as much text as possible in no particular order.
12 Sparse text with OSD.
13 Raw line. Treat the image as a single text line,
bypassing hacks that are Tesseract-specific.
尝试 OSD 模式(如 1、7、11、12、13)。这将提高您的识别率。但是为了真正提高,你将不得不编写一个程序来找到单独的字母(分割图像)并将它们一个接一个地发送到 tesseract
(使用 --psm 10
)。 opencv
是一个很棒的图像处理库。 This post 可能是一个好的开始。
关于 CAPTCHA
认可的合法性问题:这是道德问题,超出了 SO 的范围。 Pythonscraping
是一个经典的测试站点,我认为没有任何问题可以帮助解决问题。这种担心与教自卫可能被用来攻击人是一样的。
无论如何,CAPTCHA
是一个非常弱的人工确认挑战,现在没有网站应该使用它,而 reCAPTCHA
更强大、更友好和免费。
我用 tesseract 用 nodejs 解决了验证码问题。要获得它 运行 你需要先做一些图像处理(取决于你尝试解决的验证码)。
如果你以这种类型的验证码为例,我做了:
- 移除 "white noise"
- 去除灰线
- 去除灰点
- 填补空白
- 更改为灰度图像
- 现在用 tesseract 做 OCR
您可以在此处查看代码、它是如何完成的以及更多文档:https://github.com/cracker0dks/CaptchaSolver
from urllib import urlopen,urlretrieve
from PIL import Image,ImageOps
from bs4 import BeautifulSoup
import requests
import subprocess
def cleanImage(imagePath):
image=Image.open(imagePath)
image=image.point(lambda x:0 if x<143 else 255)
borederImage=ImageOps.expand(image,border=20,fill="white")
borederImage.save(imagePath)
html=urlopen("http://www.pythonscraping.com/humans-only")
soup=BeautifulSoup(html,'html.parser')
imageLocation=soup.find('img',{'title':'Image CAPTCHA'})['src']
formBuildID=soup.find('input',{'name':'form_build_id'})['value']
captchaSID=soup.find('input',{'name':'captcha_sid'})['value']
captchaToken=soup.find('input',{'name':'captcha_token'})['value']
captchaURL="http://pythonscraping.com"+imageLocation
urlretrieve(captchaURL,"captcha.jpg")
cleanImage("captcha.jpg")
p=subprocess.Popen(['tesseract','captcha.jpg',"captcha"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p.wait()
f=open('captcha.txt','r')
captchaResponce=f.read().replace(" ","").replace("\n","")
print "captcha responce attempt "+ captchaResponce+"\n\n"
try:
print captchaResponce
print len(captchaResponce)
print type(captchaResponce)
except:
print "No way"
你好
这是我用于测试站点的代码,用于下载验证码图像(每次打开站点时都会获得不同的验证码),然后使用 python 中的 tesseract 读取它。
我尝试直接下载图像并使用 tesseract 直接读取它没有得到正确的验证码读数,所以我添加了函数 cleanImage
来帮助但它也没有正确读取.
在网上搜索后,我的问题似乎是 tesseract 没有 "trained" 正确处理图像。
非常感谢任何帮助。 **此代码来自网络抓取书,此示例的目的也是读取验证码并提交表单。这绝不是使网站过载或损害网站的攻击或进攻工具。
Tesseract
被训练做更传统的 OCR,而 CAPTCHA
对它来说非常具有挑战性,因为字符没有对齐,可能有旋转、重叠以及大小和字体不同。您应该尝试使用不同的页面分段模式(--psm
选项)调用 tesseract
。以下是所有可能值的列表:
Page segmentation modes:
0 Orientation and script detection (OSD) only.
1 Automatic page segmentation with OSD.
2 Automatic page segmentation, but no OSD, or OCR.
3 Fully automatic page segmentation, but no OSD. (Default)
4 Assume a single column of text of variable sizes.
5 Assume a single uniform block of vertically aligned text.
6 Assume a single uniform block of text.
7 Treat the image as a single text line.
8 Treat the image as a single word.
9 Treat the image as a single word in a circle.
10 Treat the image as a single character.
11 Sparse text. Find as much text as possible in no particular order.
12 Sparse text with OSD.
13 Raw line. Treat the image as a single text line,
bypassing hacks that are Tesseract-specific.
尝试 OSD 模式(如 1、7、11、12、13)。这将提高您的识别率。但是为了真正提高,你将不得不编写一个程序来找到单独的字母(分割图像)并将它们一个接一个地发送到 tesseract
(使用 --psm 10
)。 opencv
是一个很棒的图像处理库。 This post 可能是一个好的开始。
关于 CAPTCHA
认可的合法性问题:这是道德问题,超出了 SO 的范围。 Pythonscraping
是一个经典的测试站点,我认为没有任何问题可以帮助解决问题。这种担心与教自卫可能被用来攻击人是一样的。
无论如何,CAPTCHA
是一个非常弱的人工确认挑战,现在没有网站应该使用它,而 reCAPTCHA
更强大、更友好和免费。
我用 tesseract 用 nodejs 解决了验证码问题。要获得它 运行 你需要先做一些图像处理(取决于你尝试解决的验证码)。
如果你以这种类型的验证码为例,我做了:
- 移除 "white noise"
- 去除灰线
- 去除灰点
- 填补空白
- 更改为灰度图像
- 现在用 tesseract 做 OCR
您可以在此处查看代码、它是如何完成的以及更多文档:https://github.com/cracker0dks/CaptchaSolver