Update app.py
Browse files
app.py
CHANGED
@@ -120,7 +120,6 @@ def create_heatmap_overlay(image, box, score):
|
|
120 |
draw = ImageDraw.Draw(overlay)
|
121 |
|
122 |
def get_temp_color(value):
|
123 |
-
# Rouge pour valeurs élevées, bleu pour faibles
|
124 |
if value > 0.8:
|
125 |
return (255, 0, 0) # Rouge vif
|
126 |
elif value > 0.6:
|
@@ -135,14 +134,12 @@ def create_heatmap_overlay(image, box, score):
|
|
135 |
width = x2 - x1
|
136 |
height = y2 - y1
|
137 |
|
138 |
-
# Créer un effet de gradient radial
|
139 |
steps = 30
|
140 |
for i in range(steps):
|
141 |
alpha = int(255 * (1 - (i / steps)) * 0.7)
|
142 |
base_color = get_temp_color(score)
|
143 |
color = base_color + (alpha,)
|
144 |
|
145 |
-
# Effet radial
|
146 |
shrink_x = (i * width) / (steps * 2)
|
147 |
shrink_y = (i * height) / (steps * 2)
|
148 |
|
@@ -152,7 +149,6 @@ def create_heatmap_overlay(image, box, score):
|
|
152 |
outline=None
|
153 |
)
|
154 |
|
155 |
-
# Ajouter une bordure plus visible
|
156 |
border_color = get_temp_color(score) + (200,)
|
157 |
draw.rectangle([x1, y1, x2, y2], outline=border_color, width=2)
|
158 |
|
@@ -161,23 +157,19 @@ def create_heatmap_overlay(image, box, score):
|
|
161 |
def draw_boxes(image, predictions):
|
162 |
result_image = image.copy().convert('RGBA')
|
163 |
|
164 |
-
# Trier les prédictions par score pour afficher les plus fortes en dernier
|
165 |
sorted_predictions = sorted(predictions, key=lambda x: x['score'])
|
166 |
|
167 |
for pred in sorted_predictions:
|
168 |
box = pred['box']
|
169 |
score = pred['score']
|
170 |
|
171 |
-
# Créer et appliquer la carte thermique
|
172 |
heatmap = create_heatmap_overlay(image, box, score)
|
173 |
result_image = Image.alpha_composite(result_image, heatmap)
|
174 |
|
175 |
-
# Ajouter le label avec la température
|
176 |
draw = ImageDraw.Draw(result_image)
|
177 |
-
temp = 36.5 + (score * 2.5)
|
178 |
label = f"{translate_label(pred['label'])} ({score:.1%}) • {temp:.1f}°C"
|
179 |
|
180 |
-
# Background du texte avec dégradé
|
181 |
text_bbox = draw.textbbox((box['xmin'], box['ymin']-25), label)
|
182 |
padding = 3
|
183 |
text_bbox = (
|
@@ -186,7 +178,6 @@ def draw_boxes(image, predictions):
|
|
186 |
)
|
187 |
draw.rectangle(text_bbox, fill="#000000CC")
|
188 |
|
189 |
-
# Texte
|
190 |
draw.text(
|
191 |
(box['xmin'], box['ymin']-25),
|
192 |
label,
|
@@ -216,90 +207,82 @@ def main():
|
|
216 |
analyze_button = st.button("Analysieren")
|
217 |
|
218 |
if uploaded_file and analyze_button:
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
|
|
|
|
239 |
|
240 |
-
|
241 |
-
|
242 |
-
st.write("### 🔍 Analyse Ergebnisse")
|
243 |
-
col1, col2 = st.columns(2)
|
244 |
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
|
|
|
|
|
|
|
|
|
|
271 |
st.markdown(f"""
|
272 |
<div class="result-box" style="color: #1a1a1a;">
|
273 |
-
<
|
274 |
-
|
275 |
-
</span> - {translate_label(pred['label'])}
|
276 |
</div>
|
277 |
""", unsafe_allow_html=True)
|
278 |
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
# Vérification et localisation des fractures
|
292 |
-
predictions = models["KnochenAuge"](image)
|
293 |
-
filtered_preds = [p for p in predictions if p['score'] >= conf_threshold
|
294 |
-
and 'fracture' in p['label'].lower()]
|
295 |
-
|
296 |
-
if filtered_preds:
|
297 |
-
st.write("#### 🎯 Fraktur Lokalisation")
|
298 |
-
result_image = draw_boxes(image, filtered_preds)
|
299 |
-
st.image(result_image, use_container_width=True)
|
300 |
-
else:
|
301 |
-
st.write("#### 🖼️ Röntgenbild")
|
302 |
-
st.image(image, use_container_width=True)
|
303 |
|
304 |
if __name__ == "__main__":
|
305 |
-
main()
|
|
|
120 |
draw = ImageDraw.Draw(overlay)
|
121 |
|
122 |
def get_temp_color(value):
|
|
|
123 |
if value > 0.8:
|
124 |
return (255, 0, 0) # Rouge vif
|
125 |
elif value > 0.6:
|
|
|
134 |
width = x2 - x1
|
135 |
height = y2 - y1
|
136 |
|
|
|
137 |
steps = 30
|
138 |
for i in range(steps):
|
139 |
alpha = int(255 * (1 - (i / steps)) * 0.7)
|
140 |
base_color = get_temp_color(score)
|
141 |
color = base_color + (alpha,)
|
142 |
|
|
|
143 |
shrink_x = (i * width) / (steps * 2)
|
144 |
shrink_y = (i * height) / (steps * 2)
|
145 |
|
|
|
149 |
outline=None
|
150 |
)
|
151 |
|
|
|
152 |
border_color = get_temp_color(score) + (200,)
|
153 |
draw.rectangle([x1, y1, x2, y2], outline=border_color, width=2)
|
154 |
|
|
|
157 |
def draw_boxes(image, predictions):
|
158 |
result_image = image.copy().convert('RGBA')
|
159 |
|
|
|
160 |
sorted_predictions = sorted(predictions, key=lambda x: x['score'])
|
161 |
|
162 |
for pred in sorted_predictions:
|
163 |
box = pred['box']
|
164 |
score = pred['score']
|
165 |
|
|
|
166 |
heatmap = create_heatmap_overlay(image, box, score)
|
167 |
result_image = Image.alpha_composite(result_image, heatmap)
|
168 |
|
|
|
169 |
draw = ImageDraw.Draw(result_image)
|
170 |
+
temp = 36.5 + (score * 2.5)
|
171 |
label = f"{translate_label(pred['label'])} ({score:.1%}) • {temp:.1f}°C"
|
172 |
|
|
|
173 |
text_bbox = draw.textbbox((box['xmin'], box['ymin']-25), label)
|
174 |
padding = 3
|
175 |
text_bbox = (
|
|
|
178 |
)
|
179 |
draw.rectangle(text_bbox, fill="#000000CC")
|
180 |
|
|
|
181 |
draw.text(
|
182 |
(box['xmin'], box['ymin']-25),
|
183 |
label,
|
|
|
207 |
analyze_button = st.button("Analysieren")
|
208 |
|
209 |
if uploaded_file and analyze_button:
|
210 |
+
with st.spinner("Bild wird analysiert..."):
|
211 |
+
image = Image.open(uploaded_file)
|
212 |
+
results_container = st.container()
|
213 |
+
|
214 |
+
predictions_watcher = models["KnochenWächter"](image)
|
215 |
+
predictions_master = models["RöntgenMeister"](image)
|
216 |
+
predictions_locator = models["KnochenAuge"](image)
|
217 |
+
|
218 |
+
has_fracture = False
|
219 |
+
max_fracture_score = 0
|
220 |
+
filtered_locations = [p for p in predictions_locator
|
221 |
+
if p['score'] >= conf_threshold
|
222 |
+
and 'fracture' in p['label'].lower()]
|
223 |
+
|
224 |
+
for pred in predictions_watcher:
|
225 |
+
if pred['score'] >= conf_threshold and 'fracture' in pred['label'].lower():
|
226 |
+
has_fracture = True
|
227 |
+
max_fracture_score = max(max_fracture_score, pred['score'])
|
228 |
+
|
229 |
+
with results_container:
|
230 |
+
st.write("### 🔍 Analyse Ergebnisse")
|
231 |
+
col1, col2 = st.columns(2)
|
232 |
|
233 |
+
with col1:
|
234 |
+
st.write("#### 🤖 KI-Diagnose")
|
|
|
|
|
235 |
|
236 |
+
st.write("##### 🛡️ KnochenWächter")
|
237 |
+
for pred in predictions_watcher:
|
238 |
+
if pred['score'] >= conf_threshold:
|
239 |
+
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
240 |
+
label_lower = pred['label'].lower()
|
241 |
+
if 'fracture' in label_lower:
|
242 |
+
has_fracture = True
|
243 |
+
max_fracture_score = max(max_fracture_score, pred['score'])
|
244 |
+
st.markdown(f"""
|
245 |
+
<div class="result-box" style="color: #1a1a1a;">
|
246 |
+
<span style="color: {confidence_color}; font-weight: 500;">
|
247 |
+
{pred['score']:.1%}
|
248 |
+
</span> - {translate_label(pred['label'])}
|
249 |
+
</div>
|
250 |
+
""", unsafe_allow_html=True)
|
251 |
+
|
252 |
+
st.write("#### 🎓 RöntgenMeister")
|
253 |
+
for pred in predictions_master:
|
254 |
+
if pred['score'] >= conf_threshold:
|
255 |
+
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
256 |
+
st.markdown(f"""
|
257 |
+
<div class="result-box" style="color: #1a1a1a;">
|
258 |
+
<span style="color: {confidence_color}; font-weight: 500;">
|
259 |
+
{pred['score']:.1%}
|
260 |
+
</span> - {translate_label(pred['label'])}
|
261 |
+
</div>
|
262 |
+
""", unsafe_allow_html=True)
|
263 |
+
|
264 |
+
if max_fracture_score > 0:
|
265 |
+
st.write("#### 📊 Wahrscheinlichkeit")
|
266 |
+
no_fracture_prob = 1 - max_fracture_score
|
267 |
st.markdown(f"""
|
268 |
<div class="result-box" style="color: #1a1a1a;">
|
269 |
+
Knochenbruch: <strong style="color: #0066cc">{max_fracture_score:.1%}</strong><br>
|
270 |
+
Kein Knochenbruch: <strong style="color: #ffa500">{no_fracture_prob:.1%}</strong>
|
|
|
271 |
</div>
|
272 |
""", unsafe_allow_html=True)
|
273 |
|
274 |
+
with col2:
|
275 |
+
predictions = models["KnochenAuge"](image)
|
276 |
+
filtered_preds = [p for p in predictions if p['score'] >= conf_threshold
|
277 |
+
and 'fracture' in p['label'].lower()]
|
278 |
+
|
279 |
+
if filtered_preds:
|
280 |
+
st.write("#### 🎯 Fraktur Lokalisation")
|
281 |
+
result_image = draw_boxes(image, filtered_preds)
|
282 |
+
st.image(result_image, use_container_width=True)
|
283 |
+
else:
|
284 |
+
st.write("#### 🖼️ Röntgenbild")
|
285 |
+
st.image(image, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
|
287 |
if __name__ == "__main__":
|
288 |
+
main()
|