|
from flask import Flask, request, jsonify |
|
import pytesseract |
|
from google.oauth2 import service_account |
|
from googleapiclient.discovery import build |
|
from googleapiclient.errors import HttpError |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
SCOPES = ['https://www.googleapis.com/auth/script.projects'] |
|
SERVICE_ACCOUNT_FILE = 'service_account.json' |
|
|
|
|
|
creds = service_account.Credentials.from_service_account_file( |
|
SERVICE_ACCOUNT_FILE, SCOPES) |
|
|
|
|
|
script_service = build('script', 'v1', credentials=creds) |
|
|
|
@app.route('/ocr', methods=['POST']) |
|
def ocr(): |
|
|
|
img = request.get_json()['image'] |
|
|
|
|
|
text = pytesseract.image_to_string(img) |
|
|
|
|
|
print(f'OCR result: {text}') |
|
|
|
|
|
insert_into_google_chat(text) |
|
|
|
return jsonify({'message': 'OCR successful'}) |
|
|
|
def insert_into_google_chat(text): |
|
|
|
message = { |
|
'text': f'////////⭐️⭐️⭐️⭐️ Bot_gas_main_dev 個人情報の確認 {text}' |
|
} |
|
|
|
|
|
try: |
|
script_service.scripts().run(body={'function': 'insertMessage', 'parameters': [message]}).execute() |
|
except HttpError as e: |
|
print(f'Error inserting message into Google Chat: {e}') |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |