intern_assignment / google_cloud_vision.py
Battlecon's picture
Initial clean deployment commit
05cb41b
Raw
History Blame Contribute Delete
1.07 kB
import os
from google.cloud import vision
def extract_raw_text(image_path: str) -> str:
"""
Sends an image to Google Cloud Vision API and returns the raw extracted text.
Requires GOOGLE_APPLICATION_CREDENTIALS environment variable to be set.
"""
# Initialize the Vision API Client
client = vision.ImageAnnotatorClient()
# Load the image into memory
with open(image_path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
# Perform Document Text Detection (optimized for handwriting/dense text)
response = client.document_text_detection(image=image)
# Error handling for the API response
if response.error.message:
raise Exception(f"Google Cloud Vision API Error: {response.error.message}")
# Extract and return the full text annotation
full_text = response.full_text_annotation.text
return full_text
# Example Usage:
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "credentials.json"
raw_text = extract_raw_text("115.jpg")
print(raw_text)