enter image description here我使用了不同的方法来将图像转换为文本,但是在任何情况下,图像中的文本都无法正确转换。
第一个代码:
import cv2
import numpy as np
from PIL import Image
from pytesseract import image_to_string
import pytesseract
src_path = "C:/vineet/ImgToText/"
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
TESSDATA_PREFIX = 'C:/Program Files (x86)/Tesseract-OCR'
img = cv2.imread(src_path+"vine.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#to get image with only black and white pixels
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
cv2.imwrite(src_path + "thres.png", img)
result = pytesseract.image_to_string(Image.open(src_path + "thres.png"),config='-psm 10')
#result = pytesseract.image_to_string(img,config='-psm 10')
print("start")
print(result)
print("done")
第二个代码:
import cv2
import numpy as np
from PIL import Image
from pytesseract import image_to_string
import pytesseract
path = "C:\\vineet\\ImgToText\\"
img = Image.open(path+'vine.jpg')
img = img.convert('RGB')
pix = img.load()
for y in range(img.size[1]):
for x in range(img.size[0]):
if(pix[x, y][0] < 102 or pix[x, y][1] < 102 or pix[x, y][2] < 102):
pix[x, y] = (0, 0, 0, 255)
else:
pix[x, y] = (255, 255, 255, 255)
img.save(path+'temp.jpg')
text = pytesseract.image_to_string(Image.open(path+'temp.jpg'),config='digits')
print(text)
第三个代码:
from PIL import Image
import PIL.Image
from pytesseract import image_to_string
import pytesseract
src_path = "C:/vineet/ImgToText/"
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
TESSDATA_PREFIX = 'C:/Program Files (x86)/Tesseract-OCR'
output = pytesseract.image_to_string(PIL.Image.open(src_path+"vine1.png"))
print(output)
第四次验证码:
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
src_path = "C:/vineet/ImgToText/"
im = Image.open(src_path+"vine.png") # the second one
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
im.save(src_path+'temp2.png')
text = pytesseract.image_to_string(Image.open(src_path+'temp2.png'))
print(text)
我知道所有4个代码都使用相同的功能pytesseract.image_to_string()
进行图像到文本的转换,但是上述方法均不能提供正确的数字文本(在Image中给出)。请为字母数字文本提供替代形式。