|
a |
|
b/streamlit_app.py |
|
|
1 |
import streamlit as st |
|
|
2 |
from PIL import Image |
|
|
3 |
import numpy as np |
|
|
4 |
from zipfile import ZipFile |
|
|
5 |
import os |
|
|
6 |
import cv2 |
|
|
7 |
from src.production import read_files, get_setup, make_masks, create_folder, make_legend |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
@st.cache |
|
|
11 |
def cached_get_setup(): |
|
|
12 |
return get_setup() |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
def main(): |
|
|
16 |
models, transforms = cached_get_setup() |
|
|
17 |
st.markdown( |
|
|
18 |
f""" |
|
|
19 |
<style> |
|
|
20 |
.sidebar .sidebar-content {{ |
|
|
21 |
background: url("https://i.ibb.co/BL3qFQW/background.png"); |
|
|
22 |
background-repeat: repeat; |
|
|
23 |
background-size: 100% auto; |
|
|
24 |
}} |
|
|
25 |
.reportview-container {{ |
|
|
26 |
background: url("https://i.ibb.co/BL3qFQW/background.png"); |
|
|
27 |
background-repeat: repeat; |
|
|
28 |
background-size: 100% auto; |
|
|
29 |
}} |
|
|
30 |
.reportview-container .main .block-container{{ |
|
|
31 |
max-width: 850px; |
|
|
32 |
padding-top: 0rem; |
|
|
33 |
padding-right: 0rem; |
|
|
34 |
padding-left: 0rem; |
|
|
35 |
padding-bottom: 0rem; |
|
|
36 |
}} |
|
|
37 |
</style> |
|
|
38 |
""", |
|
|
39 |
unsafe_allow_html=True, |
|
|
40 |
) |
|
|
41 |
for folder in ['segmentations/', 'images/']: |
|
|
42 |
create_folder(folder) |
|
|
43 |
|
|
|
44 |
st.title('Сегментация поражения легких коронавирусной пневмонией') |
|
|
45 |
|
|
|
46 |
st.subheader("Загрузка файлов") |
|
|
47 |
filenames = st.file_uploader('Выберите или ператащите сюда снимки', type=['png', 'jpeg', 'jpg', '.nii', '.nii.gz'], |
|
|
48 |
accept_multiple_files=True) |
|
|
49 |
|
|
|
50 |
multi_class = st.checkbox(label='Мульти-классовая сегментация', value=False) |
|
|
51 |
show_legend = st.checkbox(label='Легенда на картинке', value=False) |
|
|
52 |
|
|
|
53 |
if st.button('Загрузить') and filenames: |
|
|
54 |
paths, folder_name = read_files(filenames) |
|
|
55 |
if not paths: |
|
|
56 |
st.error('Неправильный формат или название файла') |
|
|
57 |
else: |
|
|
58 |
user_dir = "segmentations/" + folder_name |
|
|
59 |
|
|
|
60 |
# creating folders |
|
|
61 |
create_folder(user_dir) |
|
|
62 |
create_folder(os.path.join(user_dir, 'segmentations')) |
|
|
63 |
create_folder(os.path.join(user_dir, 'annotations')) |
|
|
64 |
|
|
|
65 |
zip_obj = ZipFile(user_dir + 'segmentations.zip', 'w') |
|
|
66 |
with st.expander("Информация о каждом фото"): |
|
|
67 |
info = st.info('Делаем предсказания, пожалуйста, подождите') |
|
|
68 |
for _paths in paths: |
|
|
69 |
for img, annotation, original_path in make_masks(_paths, models, transforms, multi_class): |
|
|
70 |
name = original_path.split('/')[-1].split('.')[0] |
|
|
71 |
name = name.replace('\\', '/') |
|
|
72 |
|
|
|
73 |
# saving annotation |
|
|
74 |
annotation_path = os.path.join(user_dir, 'annotations', name + '_annotation.txt') |
|
|
75 |
with open(annotation_path, mode='w') as f: |
|
|
76 |
f.write(annotation) |
|
|
77 |
|
|
|
78 |
info.empty() |
|
|
79 |
|
|
|
80 |
# name and annotation |
|
|
81 |
st.markdown(f'<h3>{name}</h3>', unsafe_allow_html=True) |
|
|
82 |
if not show_legend: |
|
|
83 |
if len(annotation.split('\n')) == 3: |
|
|
84 |
st.markdown('[red] - consolidation') |
|
|
85 |
st.markdown('[green] - ground-glass') |
|
|
86 |
else: |
|
|
87 |
st.markdown('[yellow] - disease') |
|
|
88 |
for line in annotation.split('\n'): |
|
|
89 |
st.markdown(line) |
|
|
90 |
|
|
|
91 |
col1, col2 = st.columns(2) |
|
|
92 |
|
|
|
93 |
# original image |
|
|
94 |
original = np.array(Image.open(original_path)) |
|
|
95 |
col1.header("Оригинал") |
|
|
96 |
col1.image(original, width=350) |
|
|
97 |
|
|
|
98 |
# refactoring image |
|
|
99 |
if show_legend: |
|
|
100 |
img = make_legend(img, annotation) |
|
|
101 |
|
|
|
102 |
# saving image |
|
|
103 |
path = os.path.join(user_dir, 'segmentations', name + '_mask.png') |
|
|
104 |
cv2.imwrite(path, img) |
|
|
105 |
|
|
|
106 |
# adding in zip |
|
|
107 |
zip_obj.write(path) |
|
|
108 |
zip_obj.write(annotation_path) |
|
|
109 |
|
|
|
110 |
# show segmentation |
|
|
111 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
|
|
112 |
img = img / 255 # to [0;1] range |
|
|
113 |
# print(img.shape, img.dtype, img) |
|
|
114 |
col2.header("Сегментация") |
|
|
115 |
col2.image(img, width=350) |
|
|
116 |
|
|
|
117 |
st.markdown('<br />', unsafe_allow_html=True) |
|
|
118 |
|
|
|
119 |
zip_obj.close() |
|
|
120 |
|
|
|
121 |
# download segmentation zip |
|
|
122 |
with st.expander("Скачать сегментации"): |
|
|
123 |
with open(os.path.join(user_dir, 'segmentations.zip'), 'rb') as file: |
|
|
124 |
st.download_button( |
|
|
125 |
label="Архив сегментаций", |
|
|
126 |
data=file, |
|
|
127 |
file_name="segmentations.zip") |
|
|
128 |
|
|
|
129 |
|
|
|
130 |
if __name__ == '__main__': |
|
|
131 |
main() |