11

I'm using Anaconda, and I'm trying to use google cloud vision, but I cannot import google cloud vision. I can import google cloud, but it throws an error below.

from google.cloud import vision
ImportError: cannot import name 'vision'

What module should I import with anaconda? (I've already imported google-api-core, google-auth, google-cloud-bigquery, google-cloud-core, google-cloud-sdk, google-cloud-storage, google-resumable-media, google-resumable-media, googleapis-common-protos)

Could anyone solve this? Thanks in advance.

joey22
  • 221
  • 1
  • 4
  • 11

3 Answers3

14

You may need to add a dependency to google-cloud-vision. To install the latest version, just run:

pip install google-cloud-vision
norbjd
  • 10,166
  • 4
  • 45
  • 80
10

You can install google-cloud-vision by typing:

pip install google-cloud-vision
Shlomo
  • 120
  • 2
  • 8
Shan Ali
  • 564
  • 4
  • 12
0

If you want to convert with OCR from PDF to txt with Google Cloud

import os
from google.cloud import vision
from google.cloud.vision_v1 import types

#pip install google-cloud-vision

# Set up the Google Cloud Vision client
client = vision.ImageAnnotatorClient()

# Directory containing the PDF files
pdf_directory = "d:/doc/doc"

# Output directory for the TXT files
output_directory = "d:/doc/doc"

# Get a list of PDF files in the directory
pdf_files = [file for file in os.listdir(pdf_directory) if file.endswith(".pdf")]

# Process each PDF file
for pdf_file in pdf_files:
    pdf_path = os.path.join(pdf_directory, pdf_file)

    # Create the output TXT file path
    txt_file = os.path.splitext(pdf_file)[0] + ".txt"
    txt_path = os.path.join(output_directory, txt_file)

    # Read the PDF file as bytes
    with open(pdf_path, 'rb') as file:
        content = file.read()

    # Convert PDF to image using Google Cloud Vision API
    input_image = types.Image(content=content)
    response = client.document_text_detection(image=input_image)

    # Extract text from the response and save it as TXT
    text = response.full_text_annotation.text
    with open(txt_path, 'w', encoding='utf-8') as file:
        file.write(text)

    print(f"Converted {pdf_file} to {txt_file}")
Just Me
  • 864
  • 2
  • 18
  • 28