Android Question OCR Scan Specific Number

Kope

Active Member
Licensed User
Hi, I need help with regards to OCR. I have a lot of content but I want to scan only specific part (ie Only Numbers) eg. Scan credit card number but not the name and other details on the card, any assistance please?

Attached is a sample image

WhatsApp Image 2019-04-27 at 5.30.12 PM(1).jpeg WhatsApp Image 2019-04-27 at 5.30.12 PM.jpeg

Thanks
 

emexes

Expert
Licensed User
This is a difficult task.
Understatement of the week ;-/

I have a friend who was doing OCR reading of name and number off a credit-card-like card for registering people arriving for appointments at a business, but he used Windows and some library (Tesseract?). The accuracy was 80-90% but 1/ we had a good idea of the names (and sometimes even the numbers) likely to be scanned on a given day, and 2/ the number had a check digit, so when he took the OCR output and then applied those filters to it to clean up stuff like is that a D or an O? then it got to like 98%. The other 2% get handled manually, but they are almost all people who aren't on the list, and thus manual intervention is required anyway, so... no worries.

I tried your sample on some online OCR sites. It wasn't pretty. Best result was:
B4X:
ScotchRow 0403065692( /83
..ti, ...A I ■ Uwt diaby....t414440.n osier. v." taw, 44.'114 .6.44,vor.4 ca Aordotri
14 4.1141L% ro4,11., mats Yes u• mple • IVINerorianrws, to.. WU. IX QV.
rearm a nailialualwrotionegyou..1.04.0 rainaigewirinewbiliamonovit.ty 430 tad
ateckyaobalanat.Dat924. 4ret,50C, • moterdoinwiDonralt MTN Con.c-t(rwr cheAr, 00
tram yourNIT/4 nutmeg 70201008 wits.
Nurnhe 2924951999 111111111111111111111
from https://www.onlineocr.net, which'd be great if it was the bottom number you were after. It was all downhill from there with three other online OCR sites.

But, on the positive side, if the card colors and layout are consistent, then you should be able to:

- use the yellow background to detect the four edges of the ticket
- straighten up and measure the ticket
- calculate the position and size of the number you need to OCR
- it looks like a fixed-pitch font, so you can probably even pick out the digits by position and the white space surrounding them
- you know they're digits, so if your OCR routine comes back with eg letters O, I, Z, S, G or B you can be pretty sure they should be 0, 1, 2, 5, 6, or 8.

And if you're writing your own OCR routine (to discern between 10 digits - how hard could that be??? ;-) and you have heaps of spare time (don't we all??? ;-) then it'd be interesting to incorporate some machine-learning of the digits out in the wild, so that the OCR gets better the more it gets used.

What could possibly go wrong?!?!
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
I thought about this whilst tidying up after dinner, thought, hey, I wonder if that OCR is any better if I do the above-mentioned processing. So I took your image, straightened it up, cut out the number bit:

upload_2019-5-3_22-45-26.png


and passed it to https://www.onlineocr.net/ and got this:

upload_2019-5-3_22-47-38.png


which in my books is a win.

:)
 
Upvote 0

emexes

Expert
Licensed User
an OCR library which can scan only numbers
The first few hits of a Google search for tesseract digits link to discussions on StackOverflow about constraining and retraining Tesseract to recognize a reduced set of characters (and thus correspondingly increase its accuracy) (one hopes).

There are also discussions of using Tesseract with B4A in these B4X forums here too.

I half-expected that some barcode scanning libraries might include the ability to read credit card numbers, but I haven't found any (yet). I did stumble on this OCR competition page (complete with cash prizes) which had 48 competitors (registrants) but was cancelled due to no submissions, which does not bode well for your quest.

irrespective of where the numbers are displayed
You can OCR the entire ticket, but the Wikipedia article about Tesseract mentioned:
Tesseract's output will have very poor quality if the input images are not preprocessed to suit it: Images (especially screenshots) must be scaled up such that the text x-height is at least 20 pixels,[13] any rotation or skew must be corrected or no text will be recognized, low-frequency changes in brightness must be high-pass filtered, or Tesseract's binarization stage will destroy much of the page, and dark borders must be manually removed, or they will be misinterpreted as characters.
and indeed that was the case with your sample image - all of the test OCR's on the skewed sample, failed.

The source of that quote seems to have gone offline, but searching for it led me to https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality which is a good read.

Running the entire (not just the number) straightened-up version of your sample through Tesseract via www.onlineocr.net resulted in this, which would be (borderline) usable if you are expecting a 14-digit number and could thus deduce that the ")" is probably a "1":

B4X:
 a en ar
imxvver VY

‘ er - ~<A

} € 0903065692678) 4
Sere tomnonerw
See
CaywOregmeensiatenestinngyewscmets
aihupreintinnentattygetes
Wendpetatr ates

—
rr anETEOS

}

292495 1999
A |
 
Last edited:
Upvote 0

Kope

Active Member
Licensed User
Upvote 0

Kope

Active Member
Licensed User
T
A few years ago, I was working for a company which provide OCR solutions that utilising the ABBYY recognition engine. If you are willing to pay for their license you can check on it's website for the Mobile SDK or Flexicapture SDK.
https://www.abbyy.com
thank for the response but I really thought I could get a library or an alternative
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i tested 3 credit cards using both tesseract and google's ocr, they both succeeded easily on the same card and failed miserably on the other two. (the photographs were quite sharp, in focus and well-sized, as ocr has been of particular interest to me for some time so i go out of my way to present the best image i can.) the results didn't surprise me; for what it's worth, it is my belief that google's ocr is an optimized version of tesseract since google essentially "owns" tesseract and its output uses the same form as tesseract's.

beyond that, the backgrounds upon which credit card account numbers are printed confuse the ocr machine. as does light glinting off raised account numbers from a heavily used card. a lot of pre-processing of the image of the card is required.

tesseract is known to have issues with numbers. (and credit card numbers use a particular font. that font can be downloaded and tesseract "trained"
with it.)

as is mentioned in one of the replies, text extraction is sensitive to alignment. placing the card in a holder in front of a stationery camera would help. anyone using tesseract would probably be doing her own preprocessing with leptonica (and possibly even opencv) to deal with alignment and background removal, but google ocr users might assume these things were taken care of by google (mainly because google says nothing about the matter, and, in general, does a decent job of correction). but when confused, google's ocr will display correctly extracted text or numbers out of order. in a test on one of the cards, a correctly extracted account number from a slighty misaligned photograph showed the last 4 digits of the account number first. it's an issue i raised on github a couple years ago.
 
Upvote 0
Top