python-调用谷歌OCR
要使用Python调用谷歌OCR,您需要:
-
安装Python的Google Cloud Vision API库。
-
创建一个Google Cloud账户并启用Google Cloud Vision API。
-
生成一个JSON格式的身份验证密钥文件,以便您的Python应用程序可以使用该API。
-
在Python中编写代码,以便它可以使用Google Cloud Vision API进行OCR。
以下是一个示例代码:
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'resources/wakeupcat.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# Performs label detection on the image file
response = client.text_detection(image=image)
texts = response.text_annotations
print('Texts:')
for text in texts:
print('\n"{}"'.format(text.description))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in text.bounding_poly.vertices])
print('bounds: {}'.format(','.join(vertices)))
请确保替换文件名和路径,以便它们与您的实际文件匹配。您还需要将身份验证密钥文件路径替换为您自己的文件路径。
此代码将打印图像中检测到的文本。
原文地址: https://www.cveoy.top/t/topic/rfL 著作权归作者所有。请勿转载和采集!