-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
122 lines (95 loc) · 3.58 KB
/
routes.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
import base64
import datetime
import json
import traceback
import urllib
import requests
from bot import BotNotifier
from settings import *
import os
from flask import Flask, request, jsonify
from utils.autostarter import AutoStarter
if not os.path.isdir(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app = Flask(__name__)
ast = AutoStarter(NN_TIMEOUT, ['python', 'xray_processing/main_utils.py'])
bot = BotNotifier()
# CORS(app)
def allowed_file(filename):
return '.' in filename and filename.split('.')[
-1].lower() in ALLOWED_EXTENSIONS
def secure(filename):
for sep in os.path.sep, os.path.altsep:
if sep:
filename = filename.replace(sep, "_")
filename = str("_".join(filename.split())).strip("._")
return filename
@app.route('/process', methods=['POST'])
def process_image():
filename = request.form['filename']
filename = str(datetime.datetime.now()) + '_' + secure(filename)
input_path = os.path.join(UPLOAD_FOLDER, filename)
ret_val = {'error': None}
to_delete = []
try:
if 'image' in request.files:
file = request.files['image'] # File
if not file or not allowed_file(file.filename):
return jsonify({'error': 'not an image'})
file.save(input_path)
elif 'image' in request.form:
with open(input_path, 'wb') as f:
try:
r = requests.get(request.form['image'])
f.write(r.content)
except Exception as e:
r = urllib.request.urlopen(request.form['image'])
f.write(r.file.read())
else:
raise Exception('No image were sent')
to_delete.append(input_path)
reply = ast.send_recv(input_path)
print(reply)
if reply != 'SUCCESS':
raise Exception(reply)
with open(input_path + '_proc.png', 'rb') as file:
ret_val['proc'] = base64.b64encode(file.read()).decode('utf-8')
to_delete.append(input_path + '_proc.png')
with open(input_path + '_norm.png', 'rb') as file:
ret_val['norm'] = base64.b64encode(file.read()).decode('utf-8')
to_delete.append(input_path + '_norm.png')
with open(input_path + '_data.json', 'r') as file:
obj = json.load(file)
ret_val['data'] = obj
to_delete.append(input_path + '_data.json')
with open(input_path + '_feats.json', 'r') as file:
obj = json.load(file)
ret_val['feats'] = obj
to_delete.append(input_path + '_feats.json')
with open(LOG_FILE, 'a') as f:
f.write(f'[{request.remote_addr}] {filename} - SUCCESS {obj}\n')
except Exception as e:
with open(LOG_FILE, 'a') as f:
f.write(f'[{request.remote_addr}] {filename} - {str(e)}\n')
bot.send(f'#XRAY #SERVICE\n {traceback.format_exc()}')
ret_val['error'] = str(e)
return jsonify(ret_val)
finally:
for s in to_delete:
os.remove(s)
return jsonify(ret_val)
@app.route('/examples', methods=['GET'])
def examples():
return jsonify(sorted(os.listdir(EXAMPLES_FOLDER)))
@app.route('/examples/<path:filename>')
def get_preview(filename):
path = os.path.join(EXAMPLES_FOLDER, secure(filename))
try:
with open(path, 'rb') as file:
b64 = base64.b64encode(file.read()).decode('utf-8')
resp = {'name': filename, 'base64': b64}
except Exception as e:
resp = {'error': str(e)}
return jsonify(resp)
if __name__ == "__main__":
app.run(host='0.0.0.0')