Google Cloud Vision_v1无Feature属性报错及conda版本升级咨询
问题描述
批量文件标注代码运行报错
从谷歌官网复制的批量文件标注代码如下:
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))
运行后出现如下错误:
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'
当前conda环境配置
环境配置yml内容:
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
google-cloud-vision 1.0.1版本确实过低,与谷歌官网提供的新版代码不兼容,导致vision_v1.Feature属性找不到。
解决方案
升级google-cloud-vision包
- 首先激活你的conda环境:
conda activate 你的环境名称
- 执行以下命令升级到指定版本(3.5.1):
conda install -c conda-forge google-cloud-vision=3.5.1
- 如果指定版本安装失败,可直接升级到最新兼容版本:
conda install -c conda-forge google-cloud-vision --update-all
验证升级结果
升级完成后,可在环境中执行以下命令确认版本:
conda list google-cloud-vision
确认版本更新后,重新运行代码即可解决属性找不到的错误。
内容的提问来源于stack exchange,提问作者VonViru
相关产品推荐
相关产品推荐

