How to improve text extraction from image?

I use pytesseract to extract text from images. Before extracting text with pytesseract, I use Pillow and cv2 to reduce noise and improve the image:

import numpy as np
import pytesseract
from PIL import Image, ImageFilter, ImageEnhance
import cv2

img = cv2.imread('ss.png')

img = cv2.resize(img, (0,0), fx=3, fy=3)
cv2.imwrite("new.png", img)

img1 = cv2.imread("new.png", 0)

#Apply dilation and erosion
kernel = np.ones((2, 2), np.uint8)
img1 = cv2.dilate(img1, kernel, iterations=1)
img1 = cv2.erode(img1, kernel, iterations=1)

img1 = cv2.adaptiveThreshold(img1,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,2)

cv2.imwrite("new1.png", img1)
img2 = Image.open("new1.png")

#Enhance the image
img2 = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
img2 = enhancer.enhance(2)
img2.save('new2.png')

result = pytesseract.image_to_string(Image.open("new2.png"))
print(result)

I mostly get good results, but when I use low quality / resolution images, I don't get the expected result. Can I improve this in my code?

Example:

Input: ss

new1.png: new1

new2.png: enter image description here

The line I get from the console is play . What can I change in my algorithm to get the whole row retrieved?

Any help would be greatly appreciated.

0

Source: https://habr.com/ru/post/1678909/


All Articles