File size: 6,946 Bytes
a326b94
3bb1400
04a7bfd
3982789
6cc7ff9
4ae300b
3982789
d42aec0
3982789
 
04a7bfd
005d8cf
d42aec0
3982789
 
4ae300b
9aecd9e
4ae300b
9aecd9e
4ae300b
 
9aecd9e
 
4ae300b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9aecd9e
d42aec0
9aecd9e
 
 
4ae300b
 
 
 
 
 
9aecd9e
 
4ae300b
 
 
 
 
9aecd9e
 
4ae300b
 
 
 
9aecd9e
 
4ae300b
 
 
 
9aecd9e
 
4ae300b
9aecd9e
4ae300b
9aecd9e
 
4ae300b
d42aec0
4ae300b
9aecd9e
 
4ae300b
 
 
9aecd9e
 
4ae300b
 
d42aec0
9aecd9e
4ae300b
 
 
9aecd9e
 
4ae300b
 
3982789
 
 
 
005d8cf
88fb5fa
d119a53
4ae300b
 
 
d119a53
 
806ecee
3982789
 
 
 
 
4ae300b
3982789
9aecd9e
3bb1400
3982789
88fb5fa
3982789
88fb5fa
3982789
d42aec0
9aecd9e
88fb5fa
 
9aecd9e
6cc7ff9
88fb5fa
 
9aecd9e
 
 
3982789
0000f4a
d119a53
3982789
c16e85e
4ae300b
d42aec0
 
4ae300b
 
d42aec0
88fb5fa
d42aec0
4ae300b
3982789
af17427
4ae300b
 
 
3982789
4ae300b
 
 
 
 
3982789
4ae300b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d42aec0
9aecd9e
4ae300b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ea5ee2
3bb1400
d119a53
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import streamlit as st
from transformers import pipeline
from PIL import Image, ImageDraw
import torch

# Configuration page
st.set_page_config(
    page_title="Fraktur Detektion",
    layout="wide",
    initial_sidebar_state="collapsed"
)

# CSS optimisé
st.markdown("""
<style>
    /* Reset complet */
    .stApp {
        background-color: var(--background-color) !important;
        padding: 0 !important;
        max-height: 600px !important;
        overflow: hidden !important;
    }
    
    /* Variables de thème */
    [data-theme="light"] {
        --background-color: #ffffff;
        --text-color: #1f2937;
        --border-color: #e5e7eb;
        --secondary-bg: #f3f4f6;
    }
    
    [data-theme="dark"] {
        --background-color: #1f2937;
        --text-color: #f3f4f6;
        --border-color: #4b5563;
        --secondary-bg: #374151;
    }
    
    /* Conteneur principal */
    .block-container {
        padding: 0.5rem !important;
        max-width: 100% !important;
    }
    
    /* Upload et contrôles */
    .uploadedFile {
        border: 1px dashed var(--border-color);
        border-radius: 0.375rem;
        padding: 0.25rem;
        background: var(--secondary-bg);
    }
    
    /* Images plus petites */
    .stImage > img {
        max-width: 250px !important;
        max-height: 250px !important;
        margin: 0 auto !important;
    }
    
    /* Tabs compacts */
    .stTabs [data-baseweb="tab-list"] {
        gap: 0.25rem;
        background: transparent;
    }
    
    .stTabs [data-baseweb="tab"] {
        padding: 0.25rem 0.5rem;
        background: var(--secondary-bg);
        border-radius: 0.375rem;
    }
    
    /* Résultats */
    .result-box {
        padding: 0.375rem;
        border-radius: 0.375rem;
        margin: 0.25rem 0;
        background: var(--secondary-bg);
        border: 1px solid var(--border-color);
        color: var(--text-color);
    }
    
    /* Cacher éléments inutiles */
    #MainMenu, footer, header, .viewerBadge_container__1QSob, .stDeployButton {
        display: none !important;
    }
    
    div[data-testid="stVerticalBlock"] {
        gap: 0.5rem !important;
    }
    
    /* Ajustements espacement */
    .st-emotion-cache-1kyxreq {
        margin-top: -1rem !important;
    }
    
    .st-emotion-cache-1wmy9hl {
        padding: 0 !important;
    }
</style>
""", unsafe_allow_html=True)

@st.cache_resource
def load_models():
    return {
        "KnochenAuge": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),  # L'œil des os
        "KnochenWächter": pipeline("image-classification", model="Heem2/bone-fracture-detection-using-xray"),  # Le gardien des os
        "RöntgenMeister": pipeline("image-classification",  # Le maître des rayons X
            model="nandodeomkar/autotrain-fracture-detection-using-google-vit-base-patch-16-54382127388")
    }

def translate_label(label):
    translations = {
        "fracture": "Knochenbruch",
        "no fracture": "Kein Bruch",
        "normal": "Normal",
        "abnormal": "Auffällig"
    }
    return translations.get(label.lower(), label)

def draw_boxes(image, predictions):
    draw = ImageDraw.Draw(image)
    for pred in predictions:
        box = pred['box']
        label = f"{translate_label(pred['label'])} ({pred['score']:.2%})"
        color = "#2563eb" if pred['score'] > 0.7 else "#eab308"
        
        draw.rectangle(
            [(box['xmin'], box['ymin']), (box['xmax'], box['ymax'])],
            outline=color,
            width=2
        )
        
        text_bbox = draw.textbbox((box['xmin'], box['ymin']-15), label)
        draw.rectangle(text_bbox, fill=color)
        draw.text((box['xmin'], box['ymin']-15), label, fill="white")
    return image

def main():
    models = load_models()
    
    # Contrôle de confiance compact
    conf_threshold = st.slider(
        "Konfidenzschwelle",
        min_value=0.0, max_value=1.0,
        value=0.60, step=0.05
    )
    
    # Upload plus propre
    uploaded_file = st.file_uploader("", type=['png', 'jpg', 'jpeg'])

    if uploaded_file:
        image = Image.open(uploaded_file)
        max_size = (250, 250)  # Taille réduite
        image.thumbnail(max_size, Image.Resampling.LANCZOS)

        tab1, tab2 = st.tabs(["📊 KI-Analyse", "🔍 Lokalisierung"])
        
        with tab1:
            # Afficher l'image originale seulement dans l'onglet Analyse
            st.image(image, use_container_width=False)
            
            model_names = {
                "KnochenWächter": "🛡️ Der KnochenWächter",
                "RöntgenMeister": "🎓 Der RöntgenMeister"
            }
            
            for model_key, display_name in model_names.items():
                st.markdown(f"<div style='font-weight:500; margin-top:0.5rem;'>{display_name}</div>", unsafe_allow_html=True)
                predictions = models[model_key](image)
                for pred in predictions:
                    if pred['score'] >= conf_threshold:
                        score_color = "#22c55e" if pred['score'] > 0.7 else "#eab308"
                        st.markdown(f"""
                            <div class='result-box'>
                                <span style='color: {score_color}; font-weight: 500;'>
                                    {pred['score']:.1%}
                                </span> - {translate_label(pred['label'])}
                            </div>
                        """, unsafe_allow_html=True)

        with tab2:
            # Dans l'onglet Lokalisierung, montrer directement l'image avec les boîtes
            with st.spinner("Analyse läuft..."):
                predictions = models["KnochenAuge"](image)
                filtered_preds = [p for p in predictions if p['score'] >= conf_threshold]
                
                if filtered_preds:
                    result_image = image.copy()
                    result_image = draw_boxes(result_image, filtered_preds)
                    st.markdown("### 👁️ Das KnochenAuge")
                    st.image(result_image, use_container_width=False)
                else:
                    st.info("Keine Auffälligkeiten erkannt")

    else:
        st.info("Röntgenbild hochladen (JPEG, PNG)")

    # Script pour la synchronisation du thème
    st.markdown("""
        <script>
            function updateTheme(isDark) {
                document.documentElement.setAttribute('data-theme', isDark ? 'dark' : 'light');
            }
            
            window.addEventListener('message', function(e) {
                if (e.data.type === 'theme-change') {
                    updateTheme(e.data.theme === 'dark');
                }
            });
            
            // Thème initial
            updateTheme(window.matchMedia('(prefers-color-scheme: dark)').matches);
        </script>
    """, unsafe_allow_html=True)

if __name__ == "__main__":
    main()