Update app.py
Browse files
app.py
CHANGED
@@ -2,81 +2,14 @@ import streamlit as st
|
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image, ImageDraw
|
4 |
import numpy as np
|
|
|
5 |
|
6 |
-
# Script pour intercepter et désactiver les WebSockets avant le chargement de l'application
|
7 |
-
st.markdown("""
|
8 |
-
<!DOCTYPE html>
|
9 |
-
<html>
|
10 |
-
<head>
|
11 |
-
<script>
|
12 |
-
// Exécution immédiate avant tout autre script
|
13 |
-
(function() {
|
14 |
-
if (navigator.userAgent.indexOf("Edge") > -1) {
|
15 |
-
// Intercepter toutes les requêtes WebSocket avant leur création
|
16 |
-
const OriginalWebSocket = window.WebSocket;
|
17 |
-
const storedWS = [];
|
18 |
-
|
19 |
-
window.WebSocket = function(url) {
|
20 |
-
if (url.includes('_stcore/stream')) {
|
21 |
-
return {
|
22 |
-
send: function() {},
|
23 |
-
close: function() {},
|
24 |
-
addEventListener: function(event, callback) {
|
25 |
-
if (event === 'open') {
|
26 |
-
setTimeout(callback, 100);
|
27 |
-
}
|
28 |
-
},
|
29 |
-
removeEventListener: function() {}
|
30 |
-
};
|
31 |
-
}
|
32 |
-
const ws = new OriginalWebSocket(url);
|
33 |
-
storedWS.push(ws);
|
34 |
-
return ws;
|
35 |
-
};
|
36 |
-
|
37 |
-
// Supprimer les messages d'erreur
|
38 |
-
const originalConsoleError = console.error;
|
39 |
-
console.error = function(msg) {
|
40 |
-
if (typeof msg === 'string' && (msg.includes('WebSocket') || msg.includes('Failed to connect'))) {
|
41 |
-
return;
|
42 |
-
}
|
43 |
-
originalConsoleError.apply(console, arguments);
|
44 |
-
};
|
45 |
-
|
46 |
-
// Message pour les utilisateurs Edge
|
47 |
-
window.addEventListener('load', function() {
|
48 |
-
const notice = document.createElement('div');
|
49 |
-
notice.style.cssText = `
|
50 |
-
position: fixed;
|
51 |
-
top: 10px;
|
52 |
-
left: 50%;
|
53 |
-
transform: translateX(-50%);
|
54 |
-
background: #ffeb3b;
|
55 |
-
color: #000;
|
56 |
-
padding: 10px 20px;
|
57 |
-
border-radius: 5px;
|
58 |
-
z-index: 999999;
|
59 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
|
60 |
-
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
61 |
-
`;
|
62 |
-
notice.innerHTML = '⚠️ Für die beste Erfahrung verwenden Sie bitte Chrome, Firefox oder Safari.';
|
63 |
-
document.body.appendChild(notice);
|
64 |
-
});
|
65 |
-
}
|
66 |
-
})();
|
67 |
-
</script>
|
68 |
-
</head>
|
69 |
-
</html>
|
70 |
-
""", unsafe_allow_html=True)
|
71 |
-
|
72 |
-
# Configuration de la page
|
73 |
st.set_page_config(
|
74 |
page_title="Fraktur Detektion",
|
75 |
layout="wide",
|
76 |
initial_sidebar_state="collapsed"
|
77 |
)
|
78 |
|
79 |
-
# Styles CSS
|
80 |
st.markdown("""
|
81 |
<style>
|
82 |
.stApp {
|
@@ -84,8 +17,25 @@ st.markdown("""
|
|
84 |
}
|
85 |
|
86 |
.block-container {
|
|
|
|
|
87 |
max-width: 1400px !important;
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
}
|
90 |
|
91 |
.result-box {
|
@@ -96,30 +46,54 @@ st.markdown("""
|
|
96 |
border: 1px solid #e9ecef;
|
97 |
}
|
98 |
|
99 |
-
|
100 |
-
|
|
|
101 |
}
|
102 |
|
103 |
-
.
|
104 |
-
|
|
|
|
|
|
|
105 |
}
|
106 |
|
107 |
-
.
|
108 |
-
|
|
|
|
|
|
|
109 |
}
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
.stApp { opacity: 1 !important; }
|
114 |
-
iframe { visibility: visible !important; }
|
115 |
-
.element-container { opacity: 1 !important; }
|
116 |
-
div[data-testid="stStatusWidget"] { display: none !important; }
|
117 |
-
div[data-baseweb="notification"] { display: none !important; }
|
118 |
}
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
display: none !important;
|
124 |
}
|
125 |
</style>
|
@@ -152,17 +126,21 @@ def create_heatmap_overlay(image, box, score):
|
|
152 |
x1, y1 = box['xmin'], box['ymin']
|
153 |
x2, y2 = box['xmax'], box['ymax']
|
154 |
|
|
|
155 |
if score > 0.8:
|
156 |
-
fill_color = (255, 0, 0, 100)
|
157 |
border_color = (255, 0, 0, 255)
|
158 |
elif score > 0.6:
|
159 |
-
fill_color = (255, 165, 0, 100)
|
160 |
border_color = (255, 165, 0, 255)
|
161 |
else:
|
162 |
-
fill_color = (255, 255, 0, 100)
|
163 |
border_color = (255, 255, 0, 255)
|
164 |
|
|
|
165 |
draw.rectangle([x1, y1, x2, y2], fill=fill_color)
|
|
|
|
|
166 |
draw.rectangle([x1, y1, x2, y2], outline=border_color, width=2)
|
167 |
|
168 |
return overlay
|
@@ -174,16 +152,20 @@ def draw_boxes(image, predictions):
|
|
174 |
box = pred['box']
|
175 |
score = pred['score']
|
176 |
|
|
|
177 |
overlay = create_heatmap_overlay(image, box, score)
|
178 |
result_image = Image.alpha_composite(result_image, overlay)
|
179 |
|
|
|
180 |
draw = ImageDraw.Draw(result_image)
|
181 |
temp = 36.5 + (score * 2.5)
|
182 |
label = f"{translate_label(pred['label'])} ({score:.1%} • {temp:.1f}°C)"
|
183 |
|
|
|
184 |
text_bbox = draw.textbbox((box['xmin'], box['ymin']-20), label)
|
185 |
draw.rectangle(text_bbox, fill=(0, 0, 0, 180))
|
186 |
|
|
|
187 |
draw.text(
|
188 |
(box['xmin'], box['ymin']-20),
|
189 |
label,
|
@@ -193,8 +175,9 @@ def draw_boxes(image, predictions):
|
|
193 |
return result_image
|
194 |
|
195 |
def main():
|
196 |
-
|
197 |
-
|
|
|
198 |
st.write("### 📤 Röntgenbild hochladen")
|
199 |
uploaded_file = st.file_uploader("Bild auswählen", type=['png', 'jpg', 'jpeg'], label_visibility="collapsed")
|
200 |
|
@@ -209,83 +192,83 @@ def main():
|
|
209 |
with col2:
|
210 |
analyze_button = st.button("Analysieren")
|
211 |
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
"""
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
</span> - {translate_label(pred['label'])}
|
262 |
-
</div>
|
263 |
-
""", unsafe_allow_html=True)
|
264 |
-
|
265 |
-
if max_fracture_score > 0:
|
266 |
-
st.write("#### 📊 Wahrscheinlichkeit")
|
267 |
-
no_fracture_prob = 1 - max_fracture_score
|
268 |
-
st.markdown(f"""
|
269 |
-
<div class="result-box">
|
270 |
-
Knochenbruch: <strong style="color: #0066cc">{max_fracture_score:.1%}</strong><br>
|
271 |
-
Kein Knochenbruch: <strong style="color: #ffa500">{no_fracture_prob:.1%}</strong>
|
272 |
-
</div>
|
273 |
-
""", unsafe_allow_html=True)
|
274 |
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
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 |
-
except Exception as e:
|
288 |
-
st.error(f"Ein Fehler ist aufgetreten: {str(e)}")
|
289 |
|
290 |
if __name__ == "__main__":
|
291 |
main()
|
|
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image, ImageDraw
|
4 |
import numpy as np
|
5 |
+
import colorsys
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
st.set_page_config(
|
8 |
page_title="Fraktur Detektion",
|
9 |
layout="wide",
|
10 |
initial_sidebar_state="collapsed"
|
11 |
)
|
12 |
|
|
|
13 |
st.markdown("""
|
14 |
<style>
|
15 |
.stApp {
|
|
|
17 |
}
|
18 |
|
19 |
.block-container {
|
20 |
+
padding-top: 0 !important;
|
21 |
+
padding-bottom: 0 !important;
|
22 |
max-width: 1400px !important;
|
23 |
+
}
|
24 |
+
|
25 |
+
.upload-container {
|
26 |
+
background: white;
|
27 |
+
padding: 1.5rem;
|
28 |
+
border-radius: 10px;
|
29 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
30 |
+
margin-bottom: 1rem;
|
31 |
+
text-align: center;
|
32 |
+
}
|
33 |
+
|
34 |
+
.results-container {
|
35 |
+
background: white;
|
36 |
+
padding: 1.5rem;
|
37 |
+
border-radius: 10px;
|
38 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
39 |
}
|
40 |
|
41 |
.result-box {
|
|
|
46 |
border: 1px solid #e9ecef;
|
47 |
}
|
48 |
|
49 |
+
h1, h2, h3, h4, p {
|
50 |
+
color: #1a1a1a !important;
|
51 |
+
margin: 0.5rem 0 !important;
|
52 |
}
|
53 |
|
54 |
+
.stImage {
|
55 |
+
background: white;
|
56 |
+
padding: 0.5rem;
|
57 |
+
border-radius: 8px;
|
58 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
59 |
}
|
60 |
|
61 |
+
.stImage > img {
|
62 |
+
max-height: 300px !important;
|
63 |
+
width: auto !important;
|
64 |
+
margin: 0 auto !important;
|
65 |
+
display: block !important;
|
66 |
}
|
67 |
+
|
68 |
+
[data-testid="stFileUploader"] {
|
69 |
+
width: 100% !important;
|
|
|
|
|
|
|
|
|
|
|
70 |
}
|
71 |
+
|
72 |
+
.stFileUploaderFileName {
|
73 |
+
color: #1a1a1a !important;
|
74 |
+
}
|
75 |
+
|
76 |
+
.stButton > button {
|
77 |
+
width: 200px;
|
78 |
+
background-color: #f8f9fa !important;
|
79 |
+
color: #1a1a1a !important;
|
80 |
+
border: 1px solid #e9ecef !important;
|
81 |
+
padding: 0.5rem 1rem !important;
|
82 |
+
border-radius: 5px !important;
|
83 |
+
transition: all 0.3s ease !important;
|
84 |
+
}
|
85 |
+
|
86 |
+
.stButton > button:hover {
|
87 |
+
background-color: #e9ecef !important;
|
88 |
+
transform: translateY(-1px);
|
89 |
+
}
|
90 |
+
|
91 |
+
#MainMenu, footer, header, [data-testid="stToolbar"] {
|
92 |
+
display: none !important;
|
93 |
+
}
|
94 |
+
|
95 |
+
/* Hide deprecation warning */
|
96 |
+
[data-testid="stExpander"], .element-container:has(>.stAlert) {
|
97 |
display: none !important;
|
98 |
}
|
99 |
</style>
|
|
|
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) # Rouge
|
132 |
border_color = (255, 0, 0, 255)
|
133 |
elif score > 0.6:
|
134 |
+
fill_color = (255, 165, 0, 100) # Orange
|
135 |
border_color = (255, 165, 0, 255)
|
136 |
else:
|
137 |
+
fill_color = (255, 255, 0, 100) # Jaune
|
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 |
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 |
return result_image
|
176 |
|
177 |
def main():
|
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 |
|
|
|
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 |
+
# 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 |
+
with col2:
|
262 |
+
predictions = models["KnochenAuge"](image)
|
263 |
+
filtered_preds = [p for p in predictions if p['score'] >= conf_threshold]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
+
if filtered_preds:
|
266 |
+
st.write("#### 🎯 Fraktur Lokalisation")
|
267 |
+
result_image = draw_boxes(image, filtered_preds)
|
268 |
+
st.image(result_image, use_container_width=True)
|
269 |
+
else:
|
270 |
+
st.write("#### 🖼️ Röntgenbild")
|
271 |
+
st.image(image, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
|
273 |
if __name__ == "__main__":
|
274 |
main()
|