问题
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
回答1:
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
回答2:
@MousamSingh, You can't check orientation of an image directly as that would be impossible as whenever you try to pass an image through tesseract it would detect text and give you back string which may have noise or unnecessary text in result.
Answer -> Before passing an image directly to tesseract instead you should first try to detect texts in that image then bound that text with the border that would end up creating rectangle around the text and then crop those texts and pass it to tesseract and it would give you much better result and as you are concerned with the orientation of an image. what you should do is get those coordinates of boxes and using those coordinates, You will be able to find angle and you can rotate that image to particular angle if needed.
I think it might help you. Give it a vote if you find your answer. Thanks
And yes i forgot to give suggesting you way to detect texts...
This the repository for python which will be useful for you to detect texts.
github link to python code for text detection
Let me know if you need anything else. Thanks
来源:https://stackoverflow.com/questions/55119504/is-it-possible-to-check-orientation-of-an-image-before-passing-it-through-pytess