AttributeError: module 'google.cloud.vision' has no attribute 'types'

Question:

I did the setup for using the Google Vision API via Python, but it doesn’t work and I don’t find any good solutions. No matter what I do, I always get "AttributeError: module 'google.cloud.vision' has no attribute 'types'"

Here is an example Code I use (Authentication etc. is done).

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}')

Here is my pip freeze:

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

Any Idea?

Asked By: Chrissi

||

Answers:

I think you should follow the official documentation:

Vision Client Libraries

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)


It is used to create the image the vision module:

image = vision.Image(content=content) 

not 

image = types.Image(content=IMAGE_CONTENT)
Answered By: marian.vladoi

This should be the answer since Google Vision changed Type and Emun methods. I also faced the issue since vision-1 and vision-2 have some differences when you need text data from pdf/img file.

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'])
Answered By: Mahendra Singh

This worked for me as of 10/24/22

from google.cloud.vision_v1.types import Image
Answered By: jf2017
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.