-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamlit_app.py
131 lines (107 loc) · 5.12 KB
/
streamlit_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
import streamlit as st
from PIL import Image
import numpy as np
from zipfile import ZipFile
import os
import cv2
from src.production import read_files, get_setup, make_masks, create_folder, make_legend
@st.cache
def cached_get_setup():
return get_setup()
def main():
models, transforms = cached_get_setup()
st.markdown(
f"""
<style>
.sidebar .sidebar-content {{
background: url("https://i.ibb.co/BL3qFQW/background.png");
background-repeat: repeat;
background-size: 100% auto;
}}
.reportview-container {{
background: url("https://i.ibb.co/BL3qFQW/background.png");
background-repeat: repeat;
background-size: 100% auto;
}}
.reportview-container .main .block-container{{
max-width: 850px;
padding-top: 0rem;
padding-right: 0rem;
padding-left: 0rem;
padding-bottom: 0rem;
}}
</style>
""",
unsafe_allow_html=True,
)
for folder in ['segmentations/', 'images/']:
create_folder(folder)
st.title('Сегментация поражения легких коронавирусной пневмонией')
st.subheader("Загрузка файлов")
filenames = st.file_uploader('Выберите или ператащите сюда снимки', type=['png', 'jpeg', 'jpg', '.nii', '.nii.gz'],
accept_multiple_files=True)
multi_class = st.checkbox(label='Мульти-классовая сегментация', value=False)
show_legend = st.checkbox(label='Легенда на картинке', value=False)
if st.button('Загрузить') and filenames:
paths, folder_name = read_files(filenames)
if not paths:
st.error('Неправильный формат или название файла')
else:
user_dir = "segmentations/" + folder_name
# creating folders
create_folder(user_dir)
create_folder(os.path.join(user_dir, 'segmentations'))
create_folder(os.path.join(user_dir, 'annotations'))
zip_obj = ZipFile(user_dir + 'segmentations.zip', 'w')
with st.expander("Информация о каждом фото"):
info = st.info('Делаем предсказания, пожалуйста, подождите')
for _paths in paths:
for img, annotation, original_path in make_masks(_paths, models, transforms, multi_class):
name = original_path.split('/')[-1].split('.')[0]
name = name.replace('\\', '/')
# saving annotation
annotation_path = os.path.join(user_dir, 'annotations', name + '_annotation.txt')
with open(annotation_path, mode='w') as f:
f.write(annotation)
info.empty()
# name and annotation
st.markdown(f'<h3>{name}</h3>', unsafe_allow_html=True)
if not show_legend:
if len(annotation.split('\n')) == 3:
st.markdown('[red] - consolidation')
st.markdown('[green] - ground-glass')
else:
st.markdown('[yellow] - disease')
for line in annotation.split('\n'):
st.markdown(line)
col1, col2 = st.columns(2)
# original image
original = np.array(Image.open(original_path))
col1.header("Оригинал")
col1.image(original, width=350)
# refactoring image
if show_legend:
img = make_legend(img, annotation)
# saving image
path = os.path.join(user_dir, 'segmentations', name + '_mask.png')
cv2.imwrite(path, img)
# adding in zip
zip_obj.write(path)
zip_obj.write(annotation_path)
# show segmentation
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img / 255 # to [0;1] range
# print(img.shape, img.dtype, img)
col2.header("Сегментация")
col2.image(img, width=350)
st.markdown('<br />', unsafe_allow_html=True)
zip_obj.close()
# download segmentation zip
with st.expander("Скачать сегментации"):
with open(os.path.join(user_dir, 'segmentations.zip'), 'rb') as file:
st.download_button(
label="Архив сегментаций",
data=file,
file_name="segmentations.zip")
if __name__ == '__main__':
main()