forked from IlyaOvodov/AngelinaReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
65 lines (53 loc) · 2.21 KB
/
server.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
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename
from BrailleToKor import BrailleToKor
import local_config
import model.infer_retinanet as infer_retinanet
model_weights = 'model.t7'
recognizer = infer_retinanet.BrailleInference(
params_fn=os.path.join(local_config.data_path, 'weights', 'param.txt'),
model_weights_fn=os.path.join(local_config.data_path, 'weights', model_weights),
create_script=None)
translator = BrailleToKor()
app = Flask(__name__)
@app.route("/", methods=["GET"])
def home():
return "Server"
@app.route("/loadModel", methods=["POST"])
def loadModel():
if request.method == "POST":
global recognizer, translator
recognizer = infer_retinanet.BrailleInference(
params_fn=os.path.join(local_config.data_path, 'weights', 'param.txt'),
model_weights_fn=os.path.join(local_config.data_path, 'weights', model_weights),
create_script=None)
translator = BrailleToKor()
return "LoadModel"
@app.route("/inference", methods=["POST"])
def inference():
if request.method == "POST":
global recognizer, translator
file = request.files["file"]
filename = secure_filename(file.filename)
file.save("./" + filename)
path = os.getcwd() + "\\" + filename
braille_texts = recognizer.inference(path, lang="EN",
draw_refined=recognizer.DRAW_NONE,
find_orientation=False,
process_2_sides=False,
align_results=True,
repeat_on_aligned=False)
translated_texts = []
if braille_texts is None:
src_text = ""
translated_text = ""
else:
src_text = "\n".join(braille_texts)
for line in braille_texts:
translated_texts.append(translator.translation(line))
translated_text = "\n".join(translated_texts)
return jsonify({"srcText": src_text, "translatedText": translated_text})
if __name__ == "__main__":
app.run("0.0.0.0", port=5000)