Issue
I'm using pytesseract to process the following image:
When I use the image_to_string()
function
config = "--oem 3 -l eng --psm 7"
pytesseract.image_to_string(potential_image, config = config)
I get the correct "03" output. However, when I use the image_to_data()
function
predict = pytesseract.image_to_data(potential_image, config = config, output_type="data.frame")
print(predict)
predict = predict[predict["conf"] != -1]
try:
detected = " ".join([str(int(a)) if isinstance(a, float) else str(a) for a in predict["text"].tolist()])
confidence = predict["conf"].iloc[0]
print("Converted detected:", detected)
print("with confidence:", confidence)
except:
pass
I get:
level page_num block_num par_num line_num word_num left top width height conf text
4 5 1 1 1 1 1 4 4 25 16 95.180374 3.0
Converted detected: 3
with confidence: 95.180374
Where the leading 0 is not preserved, and the result is a float that I later have to convert to an int / string. Is there a way to preserve the text output so that it is the same as image_to_string()
?
Solution
Rather than using data.frame
as the output type, use a regular Python dictionary:
pytesseract.image_to_data(image, config = config, output_type = pytesseract.Output.DICT)
Answered By - Andrei
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.