我正在尝试使用GCP Vision从我拥有的某些文件中提取文本。我已经将我的GCP项目配置为使用视觉API,并为我的Gservice帐户提供了权限。我正在调用以下代码:
# scan deductions file using Google Cloud Vision API
import os
from google.cloud import vision
from google.cloud import storage
from google.protobuf import json_format
bucket_name='cloud-vision-alpha'
path = 'Deductions/Target Deduction.pdf'
# Supported mime_types are: 'application/pdf' and 'image/tiff'
mime_type = 'application/pdf'
path_dir, filename = os.path.split(path)
result_blob_basename = filename.replace('.pdf', '').replace('.PDF', '')
result_blob_name = result_blob_basename + '/output-1-to-1.json'
result_blob_uri = 'gs://{}/{}/'.format(bucket_name, result_blob_basename)
input_blob_uri = 'gs://{}/{}'.format(bucket_name, filename)
# Upload file to gcloud if it doesn't exist yet
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
if bucket.get_blob(filename) is None:
blob = bucket.blob(filename)
blob.upload_from_filename(path)
# See if result already exists
# TODO: upload as hash, not filename
result_blob = bucket.get_blob(result_blob_name)
if result_blob is None:
# How many pages should be grouped into each json output file.
batch_size = 10
client = vision.ImageAnnotatorClient()
feature = vision.types.Feature(type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
gcs_source = vision.types.GcsSource(uri=input_blob_uri)
input_config = vision.types.InputConfig(gcs_source=gcs_source, mime_type=mime_type)
gcs_destination = vision.types.GcsDestination(uri=result_blob_uri)
output_config = vision.types.OutputConfig(
gcs_destination=gcs_destination, batch_size=batch_size
)
async_request = vision.types.AsyncAnnotateFileRequest(
features=[feature], input_config=input_config, output_config=output_config
)
operation = client.async_batch_annotate_files(requests=[async_request])
print('Waiting for the operation to finish.')
operation.result(timeout=180)
# Get result after OCR is completed
result_blob = bucket.get_blob(result_blob_name)
json_string = result_blob.download_as_string()
response = json_format.Parse(json_string, vision.types.AnnotateFileResponse())
# The actual response for the first page of the input file.
first_page_response = response.responses[0]
annotation = first_page_response.full_text_annotation
print(annotation.text.encode('utf-8'))
但是,此代码失败并显示错误消息:
_Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.PERMISSION_DENIED
details = "Cloud Vision API has not been used in project 563584335869 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/vision.googleapis.com/overview?project=563584335869 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."
但是,自从我激活API至少已有24小时,并且已经使用curl命令对其进行了多次调用。我已经附上了以下视觉API控制台的屏幕截图:
为什么我无法调用API?任何帮助,将不胜感激,谢谢。