-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
118 lines (90 loc) · 3.69 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
from flask import Flask, jsonify, render_template, request
import pytesseract
from PIL import Image
from googletrans import Translator #, exceptions
from flask_cors import CORS
from gevent import monkey
from gevent.pywsgi import WSGIServer
from multiprocessing import Process, cpu_count
import traceback
import os
monkey.patch_all(ssl=False)
app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
PORT = 8000
@app.route('/')
def index():
print('Request for index page received')
return render_template('translateimagetext.html')
@app.route('/api/translateimagetext', methods=['GET', 'POST'])
def upload():
try:
if request.method == 'GET':
# return render_template('translateimagetext.html')
return jsonify({'text':0, 'result':0})
image = request.files.get('image')
image.save('image.png')
origin_language = request.form.get('origin_language')
target_language = request.form.get('target_language')
try:
# origin_text = pytesseract.image_to_string(Image.open('image.png'),lang='chi_sim')
if origin_language is None or origin_language == '':
origin_text = pytesseract.image_to_string(Image.open('image.png'))
else:
origin_language_map = {
'Chinese': 'chi_sim',
'English': 'eng',
'Spanish': 'spa',
'French': 'fra',
'Russian': 'rus',
'Arabic': 'ara',
'German': 'deu',
'Japanese': 'jpn',
'Korean': 'kor'
}
origin_language_code = origin_language_map.get(origin_language)
origin_text = pytesseract.image_to_string(Image.open('image.png'),lang=origin_language_code)
except pytesseract.pytesseract.TesseractError as e:
return jsonify({'error_pytesseract': str(e)})
try:
translator = Translator()
if target_language is None or origin_language == '':
target_text = translator.translate(origin_text,dest='zh_CN')
else:
target_language_map = {
'Chinese': 'zh-CN',
'English': 'en',
'Spanish': 'es',
'French': 'fr',
'Russian': 'ru',
'Arabic': 'ar',
'German': 'de',
'Japanese': 'ja',
'Korean': 'ko'
}
target_language_code = target_language_map.get(target_language)
target_text = translator.translate(origin_text,dest=target_language_code)
# return render_template('translateimagetext.html', text = origin_text, result=target_text.text)
return jsonify({'origin_language':origin_language, 'origin_text':origin_text, 'target_language':target_language, 'target_text':target_text.text})
except Translator.TranslatorError as e:
return jsonify({'error_translator': str(e)})
except Exception as e:
print(traceback.format_exc())
print(e)
print(os.path.abspath(os.getcwd()))
return traceback.format_exc()
def run(MULTI_PROCESS):
if MULTI_PROCESS == False:
WSGIServer(('0.0.0.0', int(PORT)), app).serve_forever()
else:
mulserver = WSGIServer(('0.0.0.0', int(PORT)), app)
mulserver.start()
def server_forever():
mulserver.start_accepting()
mulserver._stop_event.wait()
for i in range(cpu_count()):
p = Process(target=server_forever)
p.start()
if __name__ == '__main__':
# app.run(port=int(PORT), debug=True)
run(False)