-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
111 lines (85 loc) · 3.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
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
# Flask package
from flask import Flask, request, render_template
from flask_restx import Api, Resource
# parsing
import argparse
# Deep learning framework
import torch
from src.models.SSD300.model import SSD300
from src.detect import detect
# Image preprocessing package
from PIL import Image
import json
from io import BytesIO
import base64
print("========== Start detection server ==========")
# torch device state
print("=============== device setup ===============")
print("torch device avaliable : ", torch.cuda.is_available())
print("torch current device : ", torch.cuda.current_device())
print("torch device num : ", torch.cuda.device_count())
# torch cuda initialize and clear cache
torch.cuda.init()
torch.cuda.empty_cache()
def parsing():
parser = argparse.ArgumentParser(description="Start detection server for K-MolOCR")
# tag and result directory
parser.add_argument("--tag", type = str, default = "SSD")
parser.add_argument("--save_dir", type = str, default = "./results")
# detection setup
parser.add_argument("--min_score", type = float, default = 0.2)
parser.add_argument("--max_overlap", type = float, default = 0.5)
parser.add_argument("--top_k", type = int, default = 5)
# gpu allocation
parser.add_argument("--gpu_num", type = int, default = 0)
args = vars(parser.parse_args())
return args
# parsing
args = parsing()
tag = args['tag']
# device allocation
if(torch.cuda.device_count() >= 1):
device = "cuda:" + str(args["gpu_num"])
else:
device = 'cpu'
save_best_dir = "./weights/{}_best.pt".format(tag)
# load
print("=========== Load detection model ===========")
model = SSD300(5)
model.to(device)
model.eval()
model.load_state_dict(torch.load(save_best_dir, map_location = device))
print("=============== Flask API on ===============")
app = Flask(__name__)
# api = Api(app)
@app.route("/index")
def index():
return render_template("index.html")
@app.route("/detect", methods = ["POST"])
def detect_mol():
if request.method == 'POST':
json_data = request.get_json()
dict_data = json.dumps(json_data)
dict_data = json.loads(dict_data)
file = dict_data['image']
if not file:
return render_template("index.html", label = 'No files')
try:
img = base64.b64decode(file)
img = BytesIO(img)
img = Image.open(img)
except:
print("File upload error ")
return render_template("index.html", label = 'File upload error')
try:
annot, is_success = detect(img, model, device, min_score = args['min_score'], max_overlap = args['max_overlap'], top_k = args['top_k'])
annot.save("./results/request_detection.jpg")
print("Detection process complete")
if is_success:
return render_template("index.html", label = 'Detection complete')
else:
return render_template("index.html", label = 'Detection failed')
except:
return render_template("index.html", label = 'Detection process error')
if __name__ == "__main__":
app.run(debug = True, host = "0.0.0.0", port = 8000)