Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,14 @@ import streamlit as st
|
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image, ImageDraw
|
4 |
import numpy as np
|
5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
st.set_page_config(
|
8 |
page_title="Fraktur Detektion",
|
@@ -126,21 +133,17 @@ def create_heatmap_overlay(image, box, score):
|
|
126 |
x1, y1 = box['xmin'], box['ymin']
|
127 |
x2, y2 = box['xmax'], box['ymax']
|
128 |
|
129 |
-
# Couleur basée sur le score
|
130 |
if score > 0.8:
|
131 |
-
fill_color = (255, 0, 0, 100)
|
132 |
border_color = (255, 0, 0, 255)
|
133 |
elif score > 0.6:
|
134 |
-
fill_color = (255, 165, 0, 100)
|
135 |
border_color = (255, 165, 0, 255)
|
136 |
else:
|
137 |
-
fill_color = (255, 255, 0, 100)
|
138 |
border_color = (255, 255, 0, 255)
|
139 |
|
140 |
-
# Rectangle semi-transparent
|
141 |
draw.rectangle([x1, y1, x2, y2], fill=fill_color)
|
142 |
-
|
143 |
-
# Bordure
|
144 |
draw.rectangle([x1, y1, x2, y2], outline=border_color, width=2)
|
145 |
|
146 |
return overlay
|
@@ -152,20 +155,16 @@ def draw_boxes(image, predictions):
|
|
152 |
box = pred['box']
|
153 |
score = pred['score']
|
154 |
|
155 |
-
# Création de l'overlay
|
156 |
overlay = create_heatmap_overlay(image, box, score)
|
157 |
result_image = Image.alpha_composite(result_image, overlay)
|
158 |
|
159 |
-
# Ajout du texte
|
160 |
draw = ImageDraw.Draw(result_image)
|
161 |
temp = 36.5 + (score * 2.5)
|
162 |
label = f"{translate_label(pred['label'])} ({score:.1%} • {temp:.1f}°C)"
|
163 |
|
164 |
-
# Fond noir pour le texte
|
165 |
text_bbox = draw.textbbox((box['xmin'], box['ymin']-20), label)
|
166 |
draw.rectangle(text_bbox, fill=(0, 0, 0, 180))
|
167 |
|
168 |
-
# Texte en blanc
|
169 |
draw.text(
|
170 |
(box['xmin'], box['ymin']-20),
|
171 |
label,
|
@@ -175,100 +174,99 @@ def draw_boxes(image, predictions):
|
|
175 |
return result_image
|
176 |
|
177 |
def main():
|
178 |
-
|
179 |
-
|
180 |
-
with st.container():
|
181 |
-
st.write("### 📤 Röntgenbild hochladen")
|
182 |
-
uploaded_file = st.file_uploader("Bild auswählen", type=['png', 'jpg', 'jpeg'], label_visibility="collapsed")
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
"Konfidenzschwelle",
|
188 |
-
min_value=0.0, max_value=1.0,
|
189 |
-
value=0.60, step=0.05,
|
190 |
-
label_visibility="visible"
|
191 |
-
)
|
192 |
-
with col2:
|
193 |
-
analyze_button = st.button("Analysieren")
|
194 |
-
|
195 |
-
if uploaded_file and analyze_button:
|
196 |
-
with st.spinner("Bild wird analysiert..."):
|
197 |
-
image = Image.open(uploaded_file)
|
198 |
-
results_container = st.container()
|
199 |
-
|
200 |
-
predictions_watcher = models["KnochenWächter"](image)
|
201 |
-
predictions_master = models["RöntgenMeister"](image)
|
202 |
-
predictions_locator = models["KnochenAuge"](image)
|
203 |
-
|
204 |
-
has_fracture = False
|
205 |
-
max_fracture_score = 0
|
206 |
-
filtered_locations = [p for p in predictions_locator
|
207 |
-
if p['score'] >= conf_threshold]
|
208 |
-
|
209 |
-
for pred in predictions_watcher:
|
210 |
-
if pred['score'] >= conf_threshold and 'fracture' in pred['label'].lower():
|
211 |
-
has_fracture = True
|
212 |
-
max_fracture_score = max(max_fracture_score, pred['score'])
|
213 |
|
214 |
-
|
215 |
-
|
216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
217 |
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
st.markdown("#### 🛡️ KnochenWächter")
|
222 |
-
# Afficher tous les résultats de KnochenWächter
|
223 |
-
for pred in predictions_watcher:
|
224 |
-
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
225 |
-
label_lower = pred['label'].lower()
|
226 |
-
# Mettre à jour max_fracture_score seulement pour les fractures
|
227 |
-
if pred['score'] >= conf_threshold and 'fracture' in label_lower:
|
228 |
-
has_fracture = True
|
229 |
-
max_fracture_score = max(max_fracture_score, pred['score'])
|
230 |
-
# Afficher tous les résultats
|
231 |
-
st.markdown(f"""
|
232 |
-
<div class="result-box" style="color: #1a1a1a;">
|
233 |
-
<span style="color: {confidence_color}; font-weight: 500;">
|
234 |
-
{pred['score']:.1%}
|
235 |
-
</span> - {translate_label(pred['label'])}
|
236 |
-
</div>
|
237 |
-
""", unsafe_allow_html=True)
|
238 |
-
|
239 |
-
st.markdown("#### 🎓 RöntgenMeister")
|
240 |
-
# Afficher tous les résultats de RöntgenMeister
|
241 |
-
for pred in predictions_master:
|
242 |
-
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
243 |
-
st.markdown(f"""
|
244 |
-
<div class="result-box" style="color: #1a1a1a;">
|
245 |
-
<span style="color: {confidence_color}; font-weight: 500;">
|
246 |
-
{pred['score']:.1%}
|
247 |
-
</span> - {translate_label(pred['label'])}
|
248 |
-
</div>
|
249 |
-
""", unsafe_allow_html=True)
|
250 |
-
|
251 |
-
if max_fracture_score > 0:
|
252 |
-
st.write("#### 📊 Wahrscheinlichkeit")
|
253 |
-
no_fracture_prob = 1 - max_fracture_score
|
254 |
-
st.markdown(f"""
|
255 |
-
<div class="result-box" style="color: #1a1a1a;">
|
256 |
-
Knochenbruch: <strong style="color: #0066cc">{max_fracture_score:.1%}</strong><br>
|
257 |
-
Kein Knochenbruch: <strong style="color: #ffa500">{no_fracture_prob:.1%}</strong>
|
258 |
-
</div>
|
259 |
-
""", unsafe_allow_html=True)
|
260 |
|
261 |
-
|
262 |
-
|
263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
|
273 |
if __name__ == "__main__":
|
274 |
main()
|
|
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image, ImageDraw
|
4 |
import numpy as np
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Configuration de l'environnement
|
8 |
+
os.environ['STREAMLIT_SERVER_PORT'] = '7860'
|
9 |
+
os.environ['STREAMLIT_SERVER_ADDRESS'] = '0.0.0.0'
|
10 |
+
os.environ['STREAMLIT_SERVER_HEADLESS'] = 'true'
|
11 |
+
os.environ['STREAMLIT_SERVER_ENABLE_CORS'] = 'true'
|
12 |
+
os.environ['STREAMLIT_SERVER_ENABLE_XSRF_PROTECTION'] = 'false'
|
13 |
|
14 |
st.set_page_config(
|
15 |
page_title="Fraktur Detektion",
|
|
|
133 |
x1, y1 = box['xmin'], box['ymin']
|
134 |
x2, y2 = box['xmax'], box['ymax']
|
135 |
|
|
|
136 |
if score > 0.8:
|
137 |
+
fill_color = (255, 0, 0, 100)
|
138 |
border_color = (255, 0, 0, 255)
|
139 |
elif score > 0.6:
|
140 |
+
fill_color = (255, 165, 0, 100)
|
141 |
border_color = (255, 165, 0, 255)
|
142 |
else:
|
143 |
+
fill_color = (255, 255, 0, 100)
|
144 |
border_color = (255, 255, 0, 255)
|
145 |
|
|
|
146 |
draw.rectangle([x1, y1, x2, y2], fill=fill_color)
|
|
|
|
|
147 |
draw.rectangle([x1, y1, x2, y2], outline=border_color, width=2)
|
148 |
|
149 |
return overlay
|
|
|
155 |
box = pred['box']
|
156 |
score = pred['score']
|
157 |
|
|
|
158 |
overlay = create_heatmap_overlay(image, box, score)
|
159 |
result_image = Image.alpha_composite(result_image, overlay)
|
160 |
|
|
|
161 |
draw = ImageDraw.Draw(result_image)
|
162 |
temp = 36.5 + (score * 2.5)
|
163 |
label = f"{translate_label(pred['label'])} ({score:.1%} • {temp:.1f}°C)"
|
164 |
|
|
|
165 |
text_bbox = draw.textbbox((box['xmin'], box['ymin']-20), label)
|
166 |
draw.rectangle(text_bbox, fill=(0, 0, 0, 180))
|
167 |
|
|
|
168 |
draw.text(
|
169 |
(box['xmin'], box['ymin']-20),
|
170 |
label,
|
|
|
174 |
return result_image
|
175 |
|
176 |
def main():
|
177 |
+
try:
|
178 |
+
models = load_models()
|
|
|
|
|
|
|
179 |
|
180 |
+
with st.container():
|
181 |
+
st.write("### 📤 Röntgenbild hochladen")
|
182 |
+
uploaded_file = st.file_uploader("Bild auswählen", type=['png', 'jpg', 'jpeg'], label_visibility="collapsed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
|
184 |
+
col1, col2 = st.columns([2, 1])
|
185 |
+
with col1:
|
186 |
+
conf_threshold = st.slider(
|
187 |
+
"Konfidenzschwelle",
|
188 |
+
min_value=0.0, max_value=1.0,
|
189 |
+
value=0.60, step=0.05,
|
190 |
+
label_visibility="visible"
|
191 |
+
)
|
192 |
+
with col2:
|
193 |
+
analyze_button = st.button("Analysieren")
|
194 |
+
|
195 |
+
if uploaded_file and analyze_button:
|
196 |
+
with st.spinner("Bild wird analysiert..."):
|
197 |
+
image = Image.open(uploaded_file)
|
198 |
+
results_container = st.container()
|
199 |
|
200 |
+
predictions_watcher = models["KnochenWächter"](image)
|
201 |
+
predictions_master = models["RöntgenMeister"](image)
|
202 |
+
predictions_locator = models["KnochenAuge"](image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
+
has_fracture = False
|
205 |
+
max_fracture_score = 0
|
206 |
+
filtered_locations = [p for p in predictions_locator
|
207 |
+
if p['score'] >= conf_threshold]
|
208 |
+
|
209 |
+
for pred in predictions_watcher:
|
210 |
+
if pred['score'] >= conf_threshold and 'fracture' in pred['label'].lower():
|
211 |
+
has_fracture = True
|
212 |
+
max_fracture_score = max(max_fracture_score, pred['score'])
|
213 |
+
|
214 |
+
with results_container:
|
215 |
+
st.write("### 🔍 Analyse Ergebnisse")
|
216 |
+
col1, col2 = st.columns(2)
|
217 |
+
|
218 |
+
with col1:
|
219 |
+
st.write("#### 🤖 KI-Diagnose")
|
220 |
+
|
221 |
+
st.markdown("#### 🛡️ KnochenWächter")
|
222 |
+
for pred in predictions_watcher:
|
223 |
+
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
224 |
+
label_lower = pred['label'].lower()
|
225 |
+
if pred['score'] >= conf_threshold and 'fracture' in label_lower:
|
226 |
+
has_fracture = True
|
227 |
+
max_fracture_score = max(max_fracture_score, pred['score'])
|
228 |
+
st.markdown(f"""
|
229 |
+
<div class="result-box" style="color: #1a1a1a;">
|
230 |
+
<span style="color: {confidence_color}; font-weight: 500;">
|
231 |
+
{pred['score']:.1%}
|
232 |
+
</span> - {translate_label(pred['label'])}
|
233 |
+
</div>
|
234 |
+
""", unsafe_allow_html=True)
|
235 |
+
|
236 |
+
st.markdown("#### 🎓 RöntgenMeister")
|
237 |
+
for pred in predictions_master:
|
238 |
+
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
239 |
+
st.markdown(f"""
|
240 |
+
<div class="result-box" style="color: #1a1a1a;">
|
241 |
+
<span style="color: {confidence_color}; font-weight: 500;">
|
242 |
+
{pred['score']:.1%}
|
243 |
+
</span> - {translate_label(pred['label'])}
|
244 |
+
</div>
|
245 |
+
""", unsafe_allow_html=True)
|
246 |
+
|
247 |
+
if max_fracture_score > 0:
|
248 |
+
st.write("#### 📊 Wahrscheinlichkeit")
|
249 |
+
no_fracture_prob = 1 - max_fracture_score
|
250 |
+
st.markdown(f"""
|
251 |
+
<div class="result-box" style="color: #1a1a1a;">
|
252 |
+
Knochenbruch: <strong style="color: #0066cc">{max_fracture_score:.1%}</strong><br>
|
253 |
+
Kein Knochenbruch: <strong style="color: #ffa500">{no_fracture_prob:.1%}</strong>
|
254 |
+
</div>
|
255 |
+
""", unsafe_allow_html=True)
|
256 |
|
257 |
+
with col2:
|
258 |
+
predictions = models["KnochenAuge"](image)
|
259 |
+
filtered_preds = [p for p in predictions if p['score'] >= conf_threshold]
|
260 |
+
|
261 |
+
if filtered_preds:
|
262 |
+
st.write("#### 🎯 Fraktur Lokalisation")
|
263 |
+
result_image = draw_boxes(image, filtered_preds)
|
264 |
+
st.image(result_image, use_container_width=True)
|
265 |
+
else:
|
266 |
+
st.write("#### 🖼️ Röntgenbild")
|
267 |
+
st.image(image, use_container_width=True)
|
268 |
+
except Exception as e:
|
269 |
+
st.error(f"Ein Fehler ist aufgetreten: {str(e)}")
|
270 |
|
271 |
if __name__ == "__main__":
|
272 |
main()
|