File size: 864 Bytes
460ecbc
 
7965fc0
460ecbc
 
 
7965fc0
 
 
 
460ecbc
 
7965fc0
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from google.cloud import vision
import os
import io

class GoogleVisionOCR:
    def __init__(self):
        # Initialize with either environment credentials or local key file
        creds_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS", "receipt-vision-key.json")
        if os.path.exists(creds_path):
            os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = creds_path
        self.client = vision.ImageAnnotatorClient()

    def extract_text(self, image_content):
        """Extracts text from image using Google Vision API"""
        try:
            image = vision.Image(content=image_content)
            response = self.client.text_detection(image=image)
            texts = response.text_annotations
            return texts[0].description if texts else ""
        except Exception as e:
            print(f"OCR Error: {str(e)}")
            return ""