ocrgoogle-cloud-visionicr

Is it possible to give text format hints in google vision api?


I'm trying to detect handwritten dates isolated in images.

enter image description here

In the cloud vision api, is there a way to give hints about type?

example: the only text present will be dd/mm/yy, d,m and y being digits

The only thing I found is language hints in the documentation.

Sometimes I get results that include letters like O instead of 0.


Solution

  • There is not a way to give hints about type but you can filter the output using client libraries. I downloaded detect.py and requirements.txt from here and modified detect.py (in def detect_text, after line 283):

        response = client.text_detection(image=image)
        texts = response.text_annotations
    
        #Import regular expressions
        import re
    
        print('Date:')
    
        dateStr=texts[0].description
        # Test case for letters replacement
        #dateStr="Z3 OZ/l7"
        #print(dateStr)
    
        dateStr=dateStr.replace("O","0")    
        dateStr=dateStr.replace("Z","2")    
        dateStr=dateStr.replace("l","1")    
    
        dateList=re.split(' |;|,|/|\n',dateStr)
    
        dd=dateList[0]
        mm=dateList[1]
        yy=dateList[2]
        date=dd+'/'+mm+'/'+yy 
        print(date)
        #for text in texts:
            #print('\n"{}"'.format(text.description))
        #print('Hello you!')
            #vertices = (['({},{})'.format(vertex.x, vertex.y)
            #            for vertex in text.bounding_poly.vertices])
    
            #print('bounds: {}'.format(','.join(vertices)))
        # [END migration_text_detection]
    # [END def_detect_text]
    

    Then I launched detect.py inside the virtual environment using this command line:

    python detect_dates.py text qAkiq.png
    

    And I got this:

    23/02/17
    

    There are few letters that can be mistaken for numbers, so using str.replace(“letter”,”number”) should solve the wrong identifications. I added the most common cases for this example.