-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
128 lines (110 loc) · 4.14 KB
/
app.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from flask import Flask, request, render_template
import requests, os, time
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
def get_text(image_url, computervision_client):
# Open local image file
read_response = computervision_client.read(image_url, raw=True)
# Get the operation location (URL with an ID at the end)
read_operation_location = read_response.headers["Operation-Location"]
# Grab the ID from the URL
operation_id = read_operation_location.split("/")[-1]
# Retrieve the results
while True:
read_result = computervision_client.get_read_result(operation_id)
if read_result.status.lower() not in ["notstarted", "running"]:
break
time.sleep(1)
# Get the detected text
text = ""
if read_result.status == OperationStatusCodes.succeeded:
for page in read_result.analyze_result.read_results:
for line in page.lines:
# Get text in each detected line and do some fixes to the structure
if (not text) or text[-1].strip() == "." or text[-1].strip() == ":":
text = text + "\n" + line.text
else:
text = text + " " + line.text
text = text.replace(" .", ".").replace(" ,", ",").replace(" :", ":")
return text
def detect_language(text, key, region, endpoint):
# Use the Translator detect function
path = "/detect"
url = endpoint + path
# Build the request
params = {
"api-version": "3.0"
}
headers = {
"Ocp-Apim-Subscription-Key": key,
"Ocp-Apim-Subscription-Region": region,
"Content-type": "application/json"
}
body = [{
"text": text
}]
# Send the request and get response
request = requests.post(url, params=params, headers=headers, json=body)
response = request.json()
# Get language
language = response[0]["language"]
# Return the language
return language
def translate(text, source_language, target_language, key, region, endpoint):
# Use the Translator translate function
url = endpoint + "/translate"
# Build the request
params = {
"api-version": "3.0",
"from": source_language,
"to": target_language
}
headers = {
"Ocp-Apim-Subscription-Key": key,
"Ocp-Apim-Subscription-Region": region,
"Content-type": "application/json"
}
body = [{
"text": text
}]
# Send the request and get response
request = requests.post(url, params=params, headers=headers, json=body)
response = request.json()
# Get translation
translation = response[0]["translations"][0]["text"]
# Return the translation
return translation
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/', methods=['POST'])
def index_post():
# Read the values from the form
image_url = request.form['image']
target_language = request.form['language']
# Load the values from .env
key = os.getenv("COG_SERVICE_KEY")
region = os.getenv("COG_SERVICE_REGION")
endpoint = os.getenv("ENDPOINT")
COG_endpoint = os.getenv("COG_SERVICE_ENDPOINT")
# Authenticate Computer Vision client
computervision_client = ComputerVisionClient(COG_endpoint, CognitiveServicesCredentials(key))
# Extract text
text = get_text(image_url, computervision_client)
# Detect language
language = detect_language(text, key, region, endpoint)
# Translate text
translated_text = translate(text, language, target_language, key, region, endpoint)
# Call render template
return render_template(
'results.html',
translated_text=translated_text,
original_text=text,
target_language=target_language
)
if __name__ == '__main__':
app.run(debug=True)