属性错误:模块'google.cloud.vision'没有属性'types'



我通过Python设置了使用Google Vision API,但它不起作用,我找不到任何好的解决方案。无论我做什么,我总是得到"AttributeError: module 'google.cloud.vision' has no attribute 'types'"。。。

下面是我使用的代码示例(完成身份验证等(。

from __future__ import print_function
from google.cloud import vision
from google.cloud.vision import types
image_uri = 'gs://cloud-samples-data/vision/using_curl/shanghai.jpeg'
client = vision.ImageAnnotatorClient()
image = types.Image(content=IMAGE_CONTENT)
label_results = client.label_detection(image=image)
response = client.label_detection(image=image)
print('Labels (and confidence score):')
print('=' * 30)
for label in label_results.label_annotations:
print(f'{label.description} - {label.score}')

这是我的点子冻结:

appdirs==1.4.3
CacheControl==0.12.6
cachetools==4.1.1
certifi==2019.11.28
chardet==3.0.4
colorama==0.4.3
contextlib2==0.6.0
distlib==0.3.0
distro==1.4.0
google-api-core==1.22.4
google-auth==1.22.1
google-cloud-vision==2.0.0
googleapis-common-protos==1.52.0
grpcio==1.32.0
html5lib==1.0.1
idna==2.8
ipaddr==2.2.0
libcst==0.3.12
lockfile==0.12.2
msgpack==0.6.2
mypy-extensions==0.4.3
packaging==20.3
pep517==0.8.2
pkg-resources==0.0.0
progress==1.5
proto-plus==1.10.0
protobuf==3.13.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pyparsing==2.4.6
pytoml==0.1.21
pytz==2020.1
PyYAML==5.3.1
requests==2.22.0
retrying==1.3.3
rsa==4.6
six==1.14.0
typing-extensions==3.7.4.3
typing-inspect==0.6.0
urllib3==1.25.8
webencodings==0.5.1

有什么想法吗?

我认为您应该遵循官方文档:

视觉客户端库

import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.abspath('resources/wakeupcat.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)

它用于创建视觉模块的图像:

image = vision.Image(content=content) 
not 
image = types.Image(content=IMAGE_CONTENT)

这应该是答案,因为Google Vision更改了Type和Emun方法。我也遇到了这个问题,因为当你需要来自pdf/img文件的文本数据时,愿景-1和愿景-2有一些不同。

import json
import re
from google.cloud import vision
from google.cloud import storage
# Supported mime_types are: 'application/pdf' and 'image/tiff'
mime_type = 'application/pdf'
# How many pages should be grouped into each json output file.
batch_size = 2
client = vision.ImageAnnotatorClient()
feature = vision.Feature(
type_=vision.Feature.Type.DOCUMENT_TEXT_DETECTION)
gcs_source = vision.GcsSource(uri=gcs_source_uri)
input_config = vision.InputConfig(
gcs_source=gcs_source, mime_type=mime_type)
gcs_destination = vision.GcsDestination(uri=gcs_destination_uri)
output_config = vision.OutputConfig(
gcs_destination=gcs_destination, batch_size=batch_size)
async_request = vision.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=420)
# Once the request has completed and the output has been
# written to GCS, we can list all the output files.
storage_client = storage.Client()
match = re.match(r'gs://([^/]+)/(.+)', gcs_destination_uri)
bucket_name = match.group(1)
prefix = match.group(2)
bucket = storage_client.get_bucket(bucket_name)
# List objects with the given prefix.
blob_list = list(bucket.list_blobs(prefix=prefix))
print('Output files:')
for blob in blob_list:
print(blob.name)
# Process the first output file from GCS.
# Since we specified batch_size=2, the first response contains
# the first two pages of the input file.
output = blob_list[0]
json_string = output.download_as_string()
response = json.loads(json_string)
# The actual response for the first page of the input file.
first_page_response = response['responses'][0]
annotation = first_page_response['fullTextAnnotation']
# Here we print the full text from the first page.
# The response contains more information:
# annotation/pages/blocks/paragraphs/words/symbols
# including confidence scores and bounding boxes
print('Full text:n')
print(annotation['text'])

截至22年10月24日,这对我有效

from google.cloud.vision_v1.types import Image

最新更新