Issue
I am trying to extract text from the image, however, with the following code that I have tried on other images, it works but not on this image. Is there an issue with the code?
Image trying extract text from:
Here is the code:
import cv2
import pytesseract
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
try:
from PIL import Image
except ImportError:
import Image
# Import image, convert,resize and noise removal
img = cv2.imread("sample01.png", cv2.IMREAD_GRAYSCALE)
print('Dimension of image: {}'.format(img.ndim))
img = cv2.resize(img, None, fx=2, fy=2)
blur = cv2.GaussianBlur(img, (5, 5), 0)
# Apply adaptiveThreshold (Mean)
th2 = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 11, 2)
cv2.imwrite('resize_adaptive_threshmean.png', th2)
# Apply Tesseract to detect words
print(pytesseract.image_to_string(Image.open('resize_adaptive_threshmean.png')))
print("=========================================================")
Is there anything wrong with the code?
Solution
Well, you can use adaptive-thresholding
import cv2
import numpy as np
import pytesseract
img = cv2.imread("ACtBA.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
flt = cv2.adaptiveThreshold(gry,
100, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 15, 16)
txt = pytesseract.image_to_string(flt)
print(txt)
The image will be:
Result:
Parking: You may park anywhere on the campus where there are no signs prohibiting par-
king. Keep in mind the carpool hours and park accordingly so you do not get blocked in the
afternoon
Under Schoo! Age Children.While we love the younger children, it can be disruptive and
inappropriate to have them on campus during school hours. There may be special times
that they may be invited or can accompany a parent volunteer, but otherwise we ask that
you adhere to our —_ policy for the benefit of the students and staff.
I test with different parameters, so I think the most suitable parameters are:
maxValue = 100 # Display pixels greater than maxValue
blockSize=15. # size of neighbourhood area.
C=16 # just a constant which is subtracted from the mean or weighted mean calculated.
Answered By - Ahx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.