-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserving.py
40 lines (32 loc) · 1.12 KB
/
serving.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
import cv2
import uuid
import os
from flask import Flask, request, jsonify
#from src.classifier import Classifier
from src.antigener_detection.antigener_detector import antigener_classification_update_1, Searcher
app = Flask(__name__)
#classifier = Classifier.from_pretrained('')
cache_dir = '.cache_image'
os.makedirs(cache_dir, exist_ok=True)
searcher, id_map = Searcher()
def map_result(result):
new_result = {
"positive": [],
"negative": []
}
for item in result.get('positive', []):
new_result['positive'].append(float(item[-1]))
for item in result.get('negative', []):
new_result['negative'].append(float(item[-1]))
return new_result
@app.route('/antigen-images', methods=['POST'])
def hello():
image = request.files['antigen']
file = os.path.join(cache_dir, f'{str(uuid.uuid4())}-{image.filename}')
image.save(file)
#result = classifier.predict(file)
img = cv2.imread(file)
result = antigener_classification_update_1(img, searcher, id_map)
result = map_result(result)
return jsonify(dict(code=200, data=result))
app.run(host='0.0.0.0', port=8089)