我有一个像这样一个黑白文本的二进制文本图像 - 猫
我想对这样的图像执行 OCR。它们只包含一个词。我已经尝试过 tesseract 和谷歌云视觉,但它们都没有返回任何结果。我正在使用 python 3.6 和 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
这张图片对于两者中的任何一个来说都应该是一项简单的任务,我觉得我的代码中遗漏了一些东西。请帮帮我!
编辑:
感谢F10为我指明了正确的方向。这就是我如何让它与本地图像一起工作。
# 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)