Update app.py
Browse files
app.py
CHANGED
@@ -119,49 +119,81 @@ def create_heatmap_overlay(image, box, score):
|
|
119 |
overlay = Image.new('RGBA', image.size, (0, 0, 0, 0))
|
120 |
draw = ImageDraw.Draw(overlay)
|
121 |
|
122 |
-
def
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
128 |
|
129 |
x1, y1 = box['xmin'], box['ymin']
|
130 |
x2, y2 = box['xmax'], box['ymax']
|
|
|
|
|
131 |
|
132 |
-
|
|
|
133 |
for i in range(steps):
|
134 |
-
alpha = int(255 * (1 - i/steps) * 0.
|
135 |
-
|
136 |
-
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
return overlay
|
143 |
|
144 |
def draw_boxes(image, predictions):
|
145 |
result_image = image.copy().convert('RGBA')
|
146 |
|
147 |
-
|
|
|
|
|
|
|
148 |
box = pred['box']
|
149 |
score = pred['score']
|
150 |
-
label = f"{translate_label(pred['label'])} ({score:.1%})"
|
151 |
|
|
|
152 |
heatmap = create_heatmap_overlay(image, box, score)
|
153 |
result_image = Image.alpha_composite(result_image, heatmap)
|
154 |
|
|
|
155 |
draw = ImageDraw.Draw(result_image)
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
160 |
)
|
|
|
161 |
|
162 |
-
|
163 |
-
draw.
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
return result_image
|
167 |
|
@@ -184,22 +216,37 @@ def main():
|
|
184 |
analyze_button = st.button("Analysieren")
|
185 |
|
186 |
if uploaded_file and analyze_button:
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
st.write("### 🔍 Analyse Ergebnisse")
|
191 |
-
|
192 |
-
col1, col2 = st.columns(2)
|
193 |
-
|
194 |
-
with col1:
|
195 |
-
st.write("#### 🤖 KI-Diagnose")
|
196 |
|
197 |
-
#
|
198 |
predictions_watcher = models["KnochenWächter"](image)
|
|
|
|
|
|
|
|
|
199 |
has_fracture = False
|
200 |
max_fracture_score = 0
|
201 |
-
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
for pred in predictions_watcher:
|
204 |
if pred['score'] >= conf_threshold:
|
205 |
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|
|
|
119 |
overlay = Image.new('RGBA', image.size, (0, 0, 0, 0))
|
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:
|
127 |
+
return (255, 69, 0) # Rouge-orange
|
128 |
+
elif value > 0.4:
|
129 |
+
return (255, 165, 0) # Orange
|
130 |
+
else:
|
131 |
+
return (255, 255, 0) # Jaune
|
132 |
|
133 |
x1, y1 = box['xmin'], box['ymin']
|
134 |
x2, y2 = box['xmax'], box['ymax']
|
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 |
+
|
149 |
+
draw.rectangle(
|
150 |
+
[x1 + shrink_x, y1 + shrink_y, x2 - shrink_x, y2 - shrink_y],
|
151 |
+
fill=color,
|
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 |
|
159 |
return overlay
|
160 |
|
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) # Simulation de température: 36.5°C - 39°C
|
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 = (
|
184 |
+
text_bbox[0]-padding, text_bbox[1]-padding,
|
185 |
+
text_bbox[2]+padding, text_bbox[3]+padding
|
186 |
)
|
187 |
+
draw.rectangle(text_bbox, fill="#000000CC")
|
188 |
|
189 |
+
# Texte
|
190 |
+
draw.text(
|
191 |
+
(box['xmin'], box['ymin']-25),
|
192 |
+
label,
|
193 |
+
fill="#FFFFFF",
|
194 |
+
stroke_width=1,
|
195 |
+
stroke_fill="#000000"
|
196 |
+
)
|
197 |
|
198 |
return result_image
|
199 |
|
|
|
216 |
analyze_button = st.button("Analysieren")
|
217 |
|
218 |
if uploaded_file and analyze_button:
|
219 |
+
with st.spinner("Bild wird analysiert..."):
|
220 |
+
image = Image.open(uploaded_file)
|
221 |
+
results_container = st.container()
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
|
223 |
+
# Récupération de toutes les prédictions d'abord
|
224 |
predictions_watcher = models["KnochenWächter"](image)
|
225 |
+
predictions_master = models["RöntgenMeister"](image)
|
226 |
+
predictions_locator = models["KnochenAuge"](image)
|
227 |
+
|
228 |
+
# Filtrage et traitement des résultats
|
229 |
has_fracture = False
|
230 |
max_fracture_score = 0
|
231 |
+
filtered_locations = [p for p in predictions_locator
|
232 |
+
if p['score'] >= conf_threshold
|
233 |
+
and 'fracture' in p['label'].lower()]
|
234 |
+
|
235 |
+
for pred in predictions_watcher:
|
236 |
+
if pred['score'] >= conf_threshold and 'fracture' in pred['label'].lower():
|
237 |
+
has_fracture = True
|
238 |
+
max_fracture_score = max(max_fracture_score, pred['score'])
|
239 |
+
|
240 |
+
# Affichage des résultats
|
241 |
+
with results_container:
|
242 |
+
st.write("### 🔍 Analyse Ergebnisse")
|
243 |
+
col1, col2 = st.columns(2)
|
244 |
+
|
245 |
+
with col1:
|
246 |
+
st.write("#### 🤖 KI-Diagnose")
|
247 |
+
|
248 |
+
# KnochenWächter
|
249 |
+
st.write("##### 🛡️ KnochenWächter")
|
250 |
for pred in predictions_watcher:
|
251 |
if pred['score'] >= conf_threshold:
|
252 |
confidence_color = '#0066cc' if pred['score'] > 0.7 else '#ffa500'
|