Aditya G.
Aditya G.

Reputation: 3

OCR on binary image

I have a binary text image like this one black on white text - cat

I want to perform OCR on images like these. They contain no more than one word. I have tried tesseract and Google cloud vision but both of them return no results. I'm using python 3.6 and Windows 10.

# export GOOGLE_APPLICATION_CREDENTIALS=kyourcredentials.json
import io
import cv2
from PIL import Image

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

# Instantiates a client
client = vision.ImageAnnotatorClient()

with io.open("test.png", 'rb') as image_file:
    content = image_file.read()

image = types.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
resp = ''

for text in texts:
    resp+=' ' + text.description

print(resp)

from PIL import Image as im
import pytesseract as ts
print(ts.image_to_string(im.fromarray(canvas.reshape((480,640)),'L'))) # canvas contains the Mat object from which the image is saved to png

This image should be a simple task for either of the two and I feel I'm missing something in my code. Please help me out!

EDIT:

Thanks to F10 for pointing me in the right direction. This is how I got it to work with a local image.

# export GOOGLE_APPLICATION_CREDENTIALS=kyourcredentials.json
import io
import cv2
from PIL import Image

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
from google.cloud.vision import enums

# Instantiates a client
client = vision.ImageAnnotatorClient()

with io.open("test.png", 'rb') as image_file:
    content = image_file.read()

features = [
    types.Feature(type=enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
]


image = types.Image(content=content)

request = types.image_annotator_pb2.AnnotateImageRequest(image=image, features=features)
response = client.annotate_image(request)

print(response)

Upvotes: 0

Views: 893

Answers (1)

F10
F10

Reputation: 2893

Based on this document, I used the following code and I was able to get text: "cat\n" as the output:

from pprint import pprint

# Imports the Google Cloud client library
from google.cloud import vision

# Instantiates a client
client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
response = client.annotate_image({
  'image': {'source': {'image_uri': 'gs://<your_bucket>/ORW90.png'}},
  'features': [{'type': vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION}],
})

pprint(response)

Hope it helps.

Upvotes: 1

Related Questions