File size: 1,199 Bytes
a746d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
import os
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

def google_ocr(image_path):
    """
    Use Google Apps Script to extract text from an image using OCR
    """
    SCOPES = ['https://www.googleapis.com/auth/script.external_request']
    SERVICE_ACCOUNT_FILE = 'path/to/service_account_key.json'

    credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, SCOPES)
    service = build('script', 'v1', credentials=credentials)

    with open(image_path, 'rb') as f:
        image_data = f.read()

    request_body = {
        'requests': [
            {
                'image': {
                    'content': image_data
                },
                'features': [
                    {
                        'type': 'TEXT_DETECTION'
                    }
                ]
            }
        ]
    }

    try:
        response = service.scripts().run(body=request_body).execute()
        text = response['responses'][0]['textAnnotations'][0]['description']
        return text
    except HttpError as e:
        print(f'Error: {e}')
        return None