For my current ocr project I tried using tesserect using the the python cover pytesseract for converting images into text files. Up till now I was only passing well straight oriented images into my module at it was able to properly figure out text in that image. But now as I am passing rotated images it is not able recognize even a single word. So to get good result I need to pass images only with proper orientation. Now I want to know that is there any method to figure out the orientation of an image before passing it in ocr module. Please let me know what methods can I used to do that orientation check.
This is the method which I am using to do conversion:
def images_to_text(testImg):
print('Reading images form the directory..........')
dataFile=[]
for filename in os.listdir(testImg):
os.chdir(testImg)
# Define config parameters.
# '-l eng' for using the English language
# '--oem 1' for using LSTM OCR Engine
config = ('-l eng --oem 1 --psm 3')
# Read image from disk
im = cv2.imread(str(filename), cv2.IMREAD_COLOR)
# Run tesseract OCR on image
text = pytesseract.image_to_string(im, config=config)
#basic preprocessing of the text
text = text.replace('\t',' ')
text= text.rstrip()
text= text.lstrip()
text = text.replace(' +',' ')
text = text.replace('\n+','\n')
text = text.replace('\n+ +',' ')
#writing data to file
os.chdir(imgTxt)
rep=filename[-3:]
name=filename.replace(rep,'txt')
with open(name, 'w') as writeFile:
writeFile.write("%s\n" % text)
text = text.replace('\n',' ')
dataFile.append(text)
print('writing data to file done')
return dataFile
I got the solution to check the orientation of an image. We already have an method in pytesseract to do this work.
imPath='path_to_image'
im = cv2.imread(str(imPath), cv2.IMREAD_COLOR)
newdata=pytesseract.image_to_osd(im)
re.search('(?<=Rotate: )\d+', newdata).group(0)
Output of method pytesseract.image_to_osd(im) is:
Page number: 0
Orientation in degrees: 270
Rotate: 90
Orientation confidence: 4.21
Script: Latin
Script confidence: 1.90
And we need rotation value only for changing the orientation, so using regular expression will do further remaining work.
re.search('(?<=Rotate: )\d+', newdata).group(0)
This would be the final method to rotate an image to bring it to 0` orientation.
def rotate(image, center = None, scale = 1.0):
angle=360-int(re.search('(?<=Rotate: )\d+', pytesseract.image_to_osd(image)).group(0))
(h, w) = image.shape[:2]
if center is None:
center = (w / 2, h / 2)
# Perform the rotation
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated