Reading text from image

2024/10/13 3:21:51

Any suggestions on converting these images to text? I'm using pytesseract and it's working wonderfully in most cases except this. Ideally I'd read these numbers exactly. Worst case I can just try to use PIL to determine if the number to the left of the '/' is a zero. Start from the left and find the first white pixel, then

enter image description here enter image description here

from PIL import Image
from pytesseract import image_to_stringmyText = image_to_string(Image.open("tmp/test.jpg"),config='-psm 10')
myText = image_to_string(Image.open("tmp/test.jpg"))

The slash in the middle causes issues here. I've also tried using PIL's '.paste' to add lots of extra black around the image. There might be a few other PIL tricks I could try, but i'd rather not go that route unless I have to.

I tried using config='-psm 10' but my 8's were coming through as ":" sometimes, and random characters other times. And my 0's were coming through as nothing.

Reference to: pytesseract don't work with one digit image for the -psm 10

_____________EDIT_______________ Additional samples:

enter image description here 1BJ2I]

enter image description here DIS

enter image description here 10.I'10

enter image description here 20.I20

So I'm doing some voodoo conversions that seem to be working for now. But looks very error prone:

def ConvertPPTextToReadableNumbers(text):text = RemoveNonASCIICharacters(text)text = text.replace("I]", "0")text = text.replace("|]", "0")text = text.replace("l]", "0")text = text.replace("B", "8")text = text.replace("D", "0")text = text.replace("S", "5")text = text.replace(".I'", "/")text = text.replace(".I", "/")text = text.replace("I'", "/")text = text.replace("J", "/")return text

Ultimately generates:

ConvertPPTextToReadableNumbers return text =  18/20
ConvertPPTextToReadableNumbers return text =  0/5
ConvertPPTextToReadableNumbers return text =  10/10
ConvertPPTextToReadableNumbers return text =  20/20
Answer

Generally speaking, most OCR tools (like Tesseract) are tuned for working with high-resolution scans of printed text. They do not perform well on low-resolution or pixellated images.

Two possible approaches here are:

  1. If the font, background, and layout of your images are completely predictable, you don't need Tesseract at all; it's just complicating matters. Build a library of images representing each character you need to recognize, and check whether parts of the image are equal to the reference image.

  2. If that isn't an option, or if it seems too hard, you could upscale the pixellated image using one of the hq*x algorithms. The added detail may be sufficient to get Tesseract to reliably recognize the characters.

https://en.xdnf.cn/q/69579.html

Related Q&A

XGBoost and sparse matrix

I am trying to use xgboost to run -using python - on a classification problem, where I have the data in a numpy matrix X (rows = observations & columns = features) and the labels in a numpy array y…

How to preserve form fields in django after unsuccessful submit?

Code from views.py:def feedback(request):if request.method == "POST":form = CommentForm(request.POST)if form.is_valid():form.save()else:print("form.errors:", form.errors)else:form =…

Idiomatic way to parse POSIX timestamps in pandas?

I have a csv file with a time column representing POSIX timestamps in milliseconds. When I read it in pandas, it correctly reads it as Int64 but I would like to convert it to a DatetimeIndex. Right now…

Apply function on each column in a pandas dataframe

How I can write following function in more pandas way:def calculate_df_columns_mean(self, df):means = {}for column in df.columns.columns.tolist():cleaned_data = self.remove_outliers(df[column].tolist()…

Compare 2 consecutive rows and assign increasing value if different (using Pandas)

I have a dataframe df_in like so:import pandas as pd dic_in = {A:[aa,aa,bb,cc,cc,cc,cc,dd,dd,dd,ee],B:[200,200,200,400,400,500,700,700,900,900,200],C:[da,cs,fr,fs,se,at,yu,j5,31,ds,sz]} df_in = pd.Data…

searching for k nearest points

I have a large set of features that looks like this:id1 28273 20866 29961 27190 31790 19714 8643 14482 5384 .... upto 1000 id2 12343 45634 29961 27130 33790 14714 7633 15483 4484 .... id3 ..... ....…

Why does del (x) with parentheses around the variable name work?

Why does this piece of code work the way it does?x = 3 print(dir()) #output indicates that x is defined in the global scope del (x) print(dir()) #output indicates that x is not defined in the glob…

How to concisely represent if/else to specify CSS classes in Django templates

In a Django template, Id like to add CSS classes to a DIV based on certain "conditions", for example:<div class="pkg-buildinfo {% if v.release.version == pkg.b.release.version %}activ…

LabelEncoder: How to keep a dictionary that shows original and converted variable

When using LabelEncoder to encode categorical variables into numerics, how does one keep a dictionary in which the transformation is tracked?i.e. a dictionary in which I can see which values became wh…

How to find hidden files inside image files (Jpg/Gif/Png) [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…