-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
144 lines (125 loc) · 5.27 KB
/
app.py
File metadata and controls
144 lines (125 loc) · 5.27 KB
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
136
137
138
139
140
141
142
143
144
from flask import Flask, request, render_template
from extract_bbox_info import yolo_to_dict
from ultralytics import YOLO
from PIL import Image
import numpy as np
import datetime
import pydicom
import shutil
import os
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/documentation")
def documentation():
return render_template('documentation.html')
@app.route("/conversion_tool")
def conversion_tool():
return render_template('dicom_to_png.html')
@app.route("/start_converting", methods=['GET', 'POST'])
def start_converting():
if request.method == 'POST':
print('yes')
remove_files()
file = request.files.get('input-image')
if file:
file.save('static/convert-file.dicom')
dicom_to_png('static/convert-file.dicom', 'static/convert-file.png')
return render_template('dicom_to_png.html', file_path='static/convert-file.png')
@app.route('/predict', methods=['GET', 'POST'])
def predict():
remove_files()
if request.method == 'POST':
patient_name = request.form['Name']
age = request.form['Age']
gender = request.form['Gender']
side = request.form['Side']
projection = request.form['Projection']
file = request.files.get('input-image')
if file and allowed_file(file.filename):
filename = f"static/input-image.png"
file.save(filename)
os.chdir('static/')
model = YOLO('YOLOv8x-best.pt')
model('input-image.png', save=True, save_txt=True)
os.chdir('../')
now = datetime.datetime.now()
date_of_scan = now.strftime("%B %d, %Y")
if os.path.exists('static/runs/detect/predict/labels/input-image.txt'):
annotations = yolo_to_dict('static/runs/detect/predict/labels/input-image.txt', 'static/runs/detect/predict/input-image.png')
else:
annotations = ''
timestamp = now.strftime('%Y%m%d%H%M%S')
return render_template('result.html',
patient_name=patient_name,
age=age,
gender=gender,
side=side,projection=projection,
date_of_scan=date_of_scan,
annotations=annotations,
timestamp=timestamp)
return render_template('index.html')
@app.route('/dicom_to_png_api_index')
def dicom_to_png_api_index():
return render_template('dicom_to_png_api.html')
@app.route("/dicom_to_png_api_start_converting", methods=['GET', 'POST'])
def dicom_to_png_api_start_converting():
if request.method == 'POST':
remove_files()
file = request.files.get('input-image')
if file:
file.save('static/dicom_to_png_api_convert-file.dicom')
dicom_to_png('static/dicom_to_png_api_convert-file.dicom', 'static/dicom_to_png_api_convert-file.png')
print('yes')
return render_template('dicom_to_png_api.html', file_path='static/dicom_to_png_api_convert-file.png')
@app.route('/mobile_detection_api')
def detection_api_index():
return render_template('android_detection_index.html')
@app.route('/mobile_detection_api_predict', methods=['GET', 'POST'])
def detection_api_predict():
remove_files()
if request.method == 'POST':
file = request.files.get('file')
if file:
filename = f"static/detection_api_input-image.png"
file.save(filename)
os.chdir('static/')
if not os.path.exists('API_DIR'):
os.mkdir('API_DIR')
os.chdir('API_DIR/')
model = YOLO('../YOLOv8x-best.pt')
model('../detection_api_input-image.png', save=True)
os.chdir('../../')
return render_template('android_detection_result.html')
return render_template('android_detection_index.html')
def allowed_file(filename):
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def remove_files():
if os.path.isdir('static/runs/'):
shutil.rmtree('static/runs')
if os.path.isdir('static/API_DIR/runs/'):
shutil.rmtree('static/API_DIR/runs')
if os.path.exists('static/input-image.png'):
os.remove('static/input-image.png')
if os.path.exists('static/convert-file.dicom'):
os.remove('static/convert-file.dicom')
if os.path.exists('static/convert-file.png'):
os.remove('static/convert-file.png')
if os.path.exists('static/dicom_to_png_api_convert-file.dicom'):
os.remove('static/dicom_to_png_api_convert-file.dicom')
if os.path.exists('static/dicom_to_png_api_convert-file.png'):
os.remove('static/dicom_to_png_api_convert-file.png')
if os.path.exists('static/detection_api_input-image.png'):
os.remove('static/detection_api_input-image.png')
def dicom_to_png(input_file, output_file):
im = pydicom.dcmread(input_file)
im = im.pixel_array.astype(float)
rescaled_image = (np.maximum(im, 0)/im.max()) * 255
final_image = np.uint8(rescaled_image)
final_image = Image.fromarray(final_image)
final_image.save(output_file)
if __name__ == "__main__":
app.run(debug=True)