[英]Where does API key go in Google Cloud Vision API?
Want to use Google's Cloud Vision API for OCR.想要使用 Google 的 Cloud Vision API 进行 OCR。 Using the python sample code here we have:
在这里使用python示例代码,我们有:
def detect_text(path):
"""Detects text in the file."""
client = vision.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
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)))
Where do I put my API key?我应该把我的 API 密钥放在哪里? I (obviously) can't authenticate without it.
我(显然)没有它就无法进行身份验证。
If you plan to use a service account with client library code, you need to set an environment variable.
如果计划将服务帐户与客户端库代码一起使用,则需要设置环境变量。
Provide the credentials to your application code by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS.
通过设置环境变量GOOGLE_APPLICATION_CREDENTIALS为您的应用程序代码提供凭据。 Replace [PATH] with the location of the JSON file you downloaded in the previous step.
将[PATH]替换为您在上一步中下载的JSON文件的位置。
For example:
例如:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
So it looks like you should create a service account, download a credentials file, and set up an environmental variable to point to it. 因此,您似乎应该创建一个服务帐户,下载一个凭证文件,并设置一个指向它的环境变量。
There are two ways through which you can authenticate您可以通过两种方式进行身份验证
from google.cloud import vision
def get_text_from_image(image_file):
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "./creds/xxxx-xxxxx.json"
try:
# process_image is a method to convert numpy array to bytestream
# (not of interest in this context hence not including it here)
byte_img = process_image_to_bytes(image_file)
client = vision.ImageAnnotatorClient()
image = vision.Image(content=byte_img)
response = client.text_detection(image=image)
texts = response.text_annotations
return texts
except BaseException as e:
print(str(e))
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] is doing the work here os.environ['GOOGLE_APPLICATION_CREDENTIALS'] 正在这里工作
from google.cloud import vision
from google.oauth2 import service_account
def get_text_from_image(image_file):
creds = service_account.Credentials.from_service_account_file("./creds/xxx-xxxxx.json")
try:
# process_image is a method to convert numpy array to bytestream
# (not of interest in this context hence not including it here)
byte_img = process_image_to_bytes(image_file)
client = vision.ImageAnnotatorClient(credentials=creds)
image = vision.Image(content=byte_img)
response = client.text_detection(image=image)
texts = response.text_annotations
return texts
except BaseException as e:
print(str(e))
Here we are using the google-auth library to create a credential file from the JSON credential file and passing that object to ImageAnnotatorClient for authentication.在这里,我们使用 google-auth 库从 JSON 凭据文件创建一个凭据文件,并将该对象传递给 ImageAnnotatorClient 进行身份验证。
Hope these sample snippets helped you希望这些示例片段对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.