Android Question OCR Scan Specific Number

Discussion in 'Android Questions' started by Kope, May 3, 2019.

  1. Kope

    Kope 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

  2. Erel

    Erel Administrator Staff Member Licensed User

    This is a difficult task. It will be easier to recognize the complete picture and then analyze the text.
  3. emexes

    emexes Well-Known Member Licensed User

    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:
    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, 
    tram yourNIT/
    4 nutmeg 70201008 wits.
    2924951999 111111111111111111111
    from, 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: May 3, 2019
    Erel and Shelby like this.
  4. emexes

    emexes Well-Known Member 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:


    and passed it to and got this:


    which in my books is a win.

  5. emexes

    emexes Well-Known Member Licensed User

    I ran the cleaned-up number through (which uses Tesseract, which I've since discovered is free and open-source) and we possibly have a new winner:

  6. Kope

    Kope Member Licensed User

    Thanks for your response

    My issue is, if I can get an OCR library which can scan only numbers irrespective of where the numbers are displayed eg Credit card numbers, tickets etc
  7. emexes

    emexes Well-Known Member Licensed User

    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.

    You can OCR the entire ticket, but the Wikipedia article about Tesseract mentioned:
    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 which is a good read.

    Running the entire (not just the number) straightened-up version of your sample through Tesseract via 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":

    a en ar
    imxvver VY

    ‘ er - ~<A

    } € 0903065692678) 4
    Sere tomnonerw
    Wendpetatr ates

    rr anETEOS


    292495 1999
    A |
    Last edited: May 4, 2019
  8. Kope

    Kope Member Licensed User

  9. aeric

    aeric Active Member Licensed User

    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.
  10. Kope

    Kope Member Licensed User

    thank for the response but I really thought I could get a library or an alternative
  11. drgottjr

    drgottjr Active Member Licensed 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.
    emexes likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice