File size: 1,065 Bytes
05cb41b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)