Search code examples
python-3.xanacondagoogle-cloud-vision

AttributeError: module 'google.cloud.vision_v1' has no attribute 'Feature'


the title. I have copied the code as is from google's website but it is not working:

import io
from google.cloud import vision_v1

def sample_batch_annotate_files(file_path="path/to/your/document.pdf"):
    """Perform batch file annotation."""
    client = vision_v1.ImageAnnotatorClient()
    # Supported mime_type: application/pdf, image/tiff, image/gif
    mime_type = "application/pdf"
    with io.open(file_path, "rb") as f:
        content = f.read()
    input_config = {"mime_type": mime_type, "content": content}
    features = [{"type_": vision_v1.Feature.Type.DOCUMENT_TEXT_DETECTION}]
    # The service can process up to 5 pages per document file. Here we specify
    # the first, second, and last page of the document to be processed.
    pages = [1, 2, -1]
    requests = [{"input_config": input_config, "features": features, "pages": pages}]
    response = client.batch_annotate_files(requests=requests)
    for image_response in response.responses[0].responses:
        print(u"Full text: {}".format(image_response.full_text_annotation.text))
        for page in image_response.full_text_annotation.pages:
            for block in page.blocks:
                print(u"\nBlock confidence: {}".format(block.confidence))
                for par in block.paragraphs:
                    print(u"\tParagraph confidence: {}".format(par.confidence))
                    for word in par.words:
                        print(u"\t\tWord confidence: {}".format(word.confidence))

Yet it is not working and giving me the following error:

File "---", line 16, in sample_batch_annotate_files
features = [{"type_": vision_v1.Feature.Type.DOCUMENT_TEXT_DETECTION}]
AttributeError: module 'google.cloud.vision_v1' has no attribute 'Feature'

I am using a conda environment and this is the .yml - as I read in another posts, I have installed the Google-api-python-client and google-cloud-vision as recommended. Might it be related to the 'google-cloud-vision' version 1.0.1 when it should be 3.5.1 ? how to update it? I installed it with: conda install -c conda-forge google-cloud-vision

channels:
- conda-forge
- defaults
dependencies:
- aiohttp=3.8.1=py39hb82d6ee_1
- aiosignal=1.2.0=pyhd8ed1ab_0
- async-timeout=4.0.2=pyhd8ed1ab_0
- attrs=22.1.0=pyh71513ae_1
- brotlipy=0.7.0=py39hb82d6ee_1004
- ca-certificates=2022.9.24=h5b45459_0
- cachetools=5.2.0=pyhd8ed1ab_0
- certifi=2022.9.24=pyhd8ed1ab_0
- cffi=1.15.1=py39h0878f49_0
- charset-normalizer=2.1.1=pyhd8ed1ab_0
- cryptography=37.0.4=py39h7bc7c5c_0
- frozenlist=1.3.1=py39hb82d6ee_0
- google-api-core=2.10.1=pyhd8ed1ab_0
- google-api-core-grpc=2.10.1=hd8ed1ab_0
- google-api-python-client=2.64.0=pyhd8ed1ab_0
- google-auth=2.12.0=pyh1a96a4e_0
- google-auth-httplib2=0.1.0=pyhd8ed1ab_1
- google-cloud-core=2.3.2=pyhd8ed1ab_0
- google-cloud-storage=2.5.0=pyh6c4a22f_0
- google-cloud-vision=1.0.1=pyhd8ed1ab_0
- google-crc32c=1.1.2=py39h3fc79e4_3
- google-resumable-media=2.4.0=pyhd8ed1ab_0
- googleapis-common-protos=1.56.4=py39h35db3c3_0
- grpcio=1.46.0=py39hb76b349_1
- grpcio-status=1.41.1=pyhd3eb1b0_0
- httplib2=0.20.4=pyhd8ed1ab_0
- idna=3.4=pyhd8ed1ab_0
- libcrc32c=1.1.2=h0e60522_0
- libprotobuf=3.20.1=h7755175_1
- libzlib=1.2.12=h8ffe710_2
- multidict=6.0.2=py39hb82d6ee_1
- openssl=1.1.1q=h8ffe710_0
- pip=22.2.2=py39haa95532_0
- protobuf=3.20.1=py39hcbf5309_0
- pyasn1=0.4.8=py_0
- pyasn1-modules=0.2.7=py_0
- pycparser=2.21=pyhd8ed1ab_0
- pyopenssl=22.0.0=pyhd8ed1ab_1
- pyparsing=3.0.9=pyhd8ed1ab_0
- pysocks=1.7.1=pyh0701188_6
- python=3.9.13=h6244533_1
- python_abi=3.9=2_cp39
- pyu2f=0.1.5=pyhd8ed1ab_0
- requests=2.28.1=pyhd8ed1ab_1
- rsa=4.9=pyhd8ed1ab_0
- setuptools=63.4.1=py39haa95532_0
- six=1.16.0=pyh6c4a22f_0
- sqlite=3.39.3=h2bbff1b_0
- typing-extensions=4.4.0=hd8ed1ab_0
- typing_extensions=4.4.0=pyha770c72_0
- tzdata=2022c=h04d1e81_0
- uritemplate=4.1.1=pyhd8ed1ab_0
- urllib3=1.26.11=pyhd8ed1ab_0
- vc=14.2=h21ff451_1
- vs2015_runtime=14.27.29016=h5e58377_2
- wheel=0.37.1=pyhd3eb1b0_0
- win_inet_pton=1.1.0=py39hcbf5309_4
- wincertstore=0.2=py39haa95532_2
- yarl=1.7.2=py39hb82d6ee_2
- zlib=1.2.12=h8ffe710_2

Solution

  • Manually installed google-cloud-vision downloading the tar.bz2 from Anaconda.

    • activate [env]
    • conda install path-to-tar

    By doing that I had my google-cloud-vision package update to the version I chose, but then I had an issue with PROTO. The error said that could find the module "import proto".

    What I did is to update: conda install -c conda-forge google-api-python-client and that solved the issue.