Jor*_*les 4 python google-cloud-platform google-cloud-vision
我正在使用我正在使用的小程序上使用Google Cloud Vision API for Python.该功能正在运行,我得到了OCR结果,但我需要格式化这些结果才能使用它们.
这是功能:
# Call to OCR API
def detect_text_uri(uri):
"""Detects text in the file located in Google Cloud Storage or on the Web.
"""
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.text_detection(image=image)
texts = response.text_annotations
for text in texts:
textdescription = (" "+ text.description )
return textdescription
Run Code Online (Sandbox Code Playgroud)
我特别需要通过切片行的文本行,并在开始和结束一个换行符添加四个空格,但在这一刻,这是唯一的工作在第一线,剩下的就是返回一行斑点.
我一直在检查官方文档,但没有真正了解API的响应格式.
你几乎就在那里.由于您希望逐行切片文本,而不是循环文本注释,请尝试从google vision的响应中获取直接" 描述 ",如下所示.
def parse_image(image_path=None):
"""
Parse the image using Google Cloud Vision API, Detects "document" features in an image
:param image_path: path of the image
:return: text content
:rtype: str
"""
client = vision.ImageAnnotatorClient()
response = client.text_detection(image=open(image_path, 'rb'))
text = response.text_annotations
del response # to clean-up the system memory
return text[0].description
Run Code Online (Sandbox Code Playgroud)
上面的函数返回一个包含图像内容的字符串,其中的行以"\n"分隔
现在,您可以根据需要为每一行添加前缀和后缀.
image_content = parse_image(image_path="path\to\image")
my_formatted_text = ""
for line in image_content.split("\n"):
my_formatted_text += " " + line + "\n"
Run Code Online (Sandbox Code Playgroud)
my_formatted_text
是你需要的文字.
归档时间: |
|
查看次数: |
1553 次 |
最近记录: |