-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
135 lines (98 loc) · 4.63 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import csv
import json
import os
from os.path import isdir
from flask import Flask, request, jsonify, send_file
from core.MultiTemplateMatching_smartphone import extract_slides
from core.test_md_nets import run_md_nets
from utils import generate_slides_txt_phone
from flask_cors import CORS
from werkzeug.utils import secure_filename
app = Flask(__name__)
cors = CORS(app)
root_dir = "/var/www/html/Sperm_Morphology_App/data/"
ALLOWED_EXTENSIONS = set([ 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'tif', 'tiff'])
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
@app.route('/run_sperm_morph', methods=['POST'])
def run_sm_model(): # e2e run
input_data = request.json
run_id = input_data['run_id']
print("run_id", run_id)
# run_id = "2"
# folder_name = os.listdir(root_dir + run_id + "/")
folder_name = sorted([fdr for fdr in os.listdir(root_dir + run_id + "/") if isdir(root_dir + run_id + "/" + fdr)],
key=lambda x: len(root_dir + run_id + "/"+x))[0]
print("folder_name", folder_name)
extract_slides(root_dir, run_id,folder_name)
txt_img_files_path = generate_slides_txt_phone(root_dir,run_id)
# output_csv_path = run_md_nets(project_root=root_dir, run_id=run_id, img_paths_file='data/2/2.txt')
run_md_nets(project_root=root_dir,run_id=run_id,img_paths_file=txt_img_files_path)
return jsonify({"status": "success"})
def read_csv(csvFilePath):
# create a dictionary
data = []
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
# Assuming a column named 'No' to
# be the primary key
# key = rows['Slide_name']
data.append(rows)
return data
@app.route("/get_all_run_ids", methods=['POST']) # Just to check the server is running
def get_all_run_ids():
# input_data = request.json
# run_id = input_data['run_id']
print("index", "=" * 50)
# slides = os.listdir(root_dir + run_id + "/" + "slides_heads")
run_ids = os.listdir(root_dir)
# csv_file_path = root_dir + run_id + "/logs/sperm/Xception/1_test_a_sd4_to_a_sd1_f/slide_prediction_sperm.csv"
run_ids = list(filter(lambda x: os.path.exists(root_dir + x + "/logs/sperm/Xception/1_test_a_sd4_to_a_sd1_f/slide_prediction_sperm.csv"), run_ids))
run_ids = sorted(run_ids,key= lambda x: x.split("___")[0].replace("Run-",""))
if len(run_ids) == 0:
return jsonify({"run_ids": ""})
user_data = ""
for user in run_ids:
user_data += user + ";"
return jsonify({"run_ids": user_data[:-1]})
@app.route("/get_run_results", methods=['POST']) # Just to check the server is running
def get_run_results():
input_data = request.json
run_id = input_data['run_id']
csv_file_path = root_dir + run_id + "/logs/sperm/Xception/1_test_a_sd4_to_a_sd1_f/slide_prediction_sperm.csv"
data = read_csv(csv_file_path)
print("index", "=" * 50)
# slides = os.listdir(root_dir + run_id + "/" + "slides_heads")
return jsonify({"run_id_results": data})
@app.route("/single_file_upload", methods=['POST']) # Just to check the server is running
def single_file_upload():
print(' * received form with', list(request.form.items()))
print(' * received files with', list(request.files.items()))
# print(request.files)
run_id = request.form['run_id']
# check if the post request has the file part
for file_name,file in request.files.items():
if file and file.filename.split('.')[-1].lower() in ALLOWED_EXTENSIONS:
# filename = secure_filename(file.filename)
os.makedirs(os.path.dirname(os.path.join(root_dir,run_id, file_name)), exist_ok=True)
# print(file_name,file.filename,filename)
file.save(os.path.join(root_dir.replace(" ","_"),run_id.replace(" ","_"), file_name.replace(" ","_")))
# file.save(os.path.join(root_dir+"/"+run_id+], filename))
# print(' * file uploaded', filename)
return 'uploaded successfully'
@app.route("/download_run_results", methods=['POST']) # Just to check the server is running
def download_run_results():
input_data = request.json
run_id = input_data['run_id']
csv_file_path = root_dir + run_id + "/logs/sperm/Xception/1_test_a_sd4_to_a_sd1_f/slide_prediction_sperm.csv"
print("index", "=" * 50)
# slides = os.listdir(root_dir + run_id + "/" + "slides_heads")
return send_file(csv_file_path, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)
# run_sm_model()