Spaces:
Runtime error
Runtime error
| 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 "" |