图形验证码的识别

OCR 技术:

(1) 在爬虫过程中,难免会遇到各种各样的验证码,而大多数验证码还是罔形验证码,这时候我们可以直接用 OCR 来识别
(2) OCR ,即 Optical Character Recognition ,光学字符识别, 是指通过扫描字符,然后通过其形状将其翻译成电子文本的过程
(3) tesserocr 是 Python 的一个OCR 识别库,但其实是对 tesseract 做的一层 Python API 封装,所以它的核心是 tesseract。因此,在安装 tesserocr 之前,我们需要先安装 tesseract


Windows 下安装 tessorocr:

1. 先安装 tessoract,下载地址:https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.01.exe
2. 再安装 tessorocr,使用 pip3 安装即可:pip3 install tesserocr pillow


Linux 下安装 tessorocr:

yum install -y tesseract
git clone https://github.com/tesseract-ocr/tessdata.git
sudo mv tessdata/* /usr/share/tesseract/tessdata
pip3 install tesserocr pillow


Python 识别图片验证码:

            

import tesserocr
from PIL import Image

image = Image.open('1.png')                 # Opens and identifies the given image file
result = tesserocr.image_to_text(image)     # Recognize OCR text from an image object
print(result)


Python 识别有干扰的图片验证码:

复制代码
import tesserocr
from PIL import Image

image = Image.open('2.png')

image = image.convert('L')
threshold = 127
table = []
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)

image = image.point(table, '1')
result = tesserocr.image_to_text(image)
print(result)
复制代码

 

 

 

 

 

 

 

 

     

posted @   孔雀东南飞  阅读(937)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示