从图像中读取文本
Read text from an image
我有这样一张照片:
我试过在Python中用pytesseract
阅读它:
from PIL import Image
import pytesseract
import numpy
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
im = Image.open("11.jpg")
text = pytesseract.image_to_string(im,lang = "eng")
print(text)
但是pytesseract
看不懂。
我也试过 opencv
但找不到解决方案。
This blog 有一个 post,其中博主提到尝试使用 tesseract
、gocr
和 ocrad
来阅读验证码文本。
关键步骤是在尝试阅读之前清理图像。该网站上给出的示例使用了一个简单的阈值过滤器,但由于您的图像是彩色的,因此可能效果不佳。
您应该尝试不同的图像处理技术,看看是否可以充分清理图像以识别文本。
说了这么多,我会附和@SiHa的评论,并指出这个activity是不道德的。试图破坏验证码保护表明对服务器所有者缺乏尊重,无论他们这样做是为了保护他们的带宽还是他们的业务。
在对图像应用OCR之前,您需要对图像进行预处理。一种简单的预处理方法是将图像放大,使用大津阈值得到二值图像,进行形态学操作,然后对图像进行OCR。
放大、高斯模糊和大津阈值
变形开放
变形关闭
反转、应用轻微模糊和 OCR
Pytesseract OCR image_to_string
使用 --psm 6
配置选项将图像视为单个文本块的结果。
xc2kc2
代码
import cv2
import pytesseract
import imutils
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
# Resize, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.jpg')
image = imutils.resize(image, width=400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Perform morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=3)
# Invert, Blur, and perform text extraction
invert = 255 - cv2.GaussianBlur(close, (3,3), 0)
data = pytesseract.image_to_string(invert, lang='eng',config='--psm 6')
print(data)
cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('close', close)
cv2.imshow('invert', invert)
cv2.waitKey()
我有这样一张照片:
我试过在Python中用pytesseract
阅读它:
from PIL import Image
import pytesseract
import numpy
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
im = Image.open("11.jpg")
text = pytesseract.image_to_string(im,lang = "eng")
print(text)
但是pytesseract
看不懂。
我也试过 opencv
但找不到解决方案。
This blog 有一个 post,其中博主提到尝试使用 tesseract
、gocr
和 ocrad
来阅读验证码文本。
关键步骤是在尝试阅读之前清理图像。该网站上给出的示例使用了一个简单的阈值过滤器,但由于您的图像是彩色的,因此可能效果不佳。
您应该尝试不同的图像处理技术,看看是否可以充分清理图像以识别文本。
说了这么多,我会附和@SiHa的评论,并指出这个activity是不道德的。试图破坏验证码保护表明对服务器所有者缺乏尊重,无论他们这样做是为了保护他们的带宽还是他们的业务。
在对图像应用OCR之前,您需要对图像进行预处理。一种简单的预处理方法是将图像放大,使用大津阈值得到二值图像,进行形态学操作,然后对图像进行OCR。
放大、高斯模糊和大津阈值
变形开放
变形关闭
反转、应用轻微模糊和 OCR
Pytesseract OCR image_to_string
使用 --psm 6
配置选项将图像视为单个文本块的结果。
xc2kc2
代码
import cv2
import pytesseract
import imutils
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
# Resize, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.jpg')
image = imutils.resize(image, width=400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Perform morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=3)
# Invert, Blur, and perform text extraction
invert = 255 - cv2.GaussianBlur(close, (3,3), 0)
data = pytesseract.image_to_string(invert, lang='eng',config='--psm 6')
print(data)
cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('close', close)
cv2.imshow('invert', invert)
cv2.waitKey()