File size: 7,192 Bytes
f700114
ebfde4d
f700114
 
6cc7ff9
f700114
 
 
 
 
005d8cf
f700114
3982789
f700114
aae61a3
f700114
9aecd9e
f700114
 
 
 
 
ebfde4d
 
3982789
f700114
 
 
 
 
ebfde4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d119a53
f700114
 
 
 
3982789
c16e85e
f700114
aae61a3
 
 
f700114
 
 
 
 
 
aae61a3
f700114
 
 
 
aae61a3
 
f700114
aae61a3
f700114
9516554
aae61a3
f700114
 
 
aae61a3
 
 
 
f700114
 
aae61a3
 
ebfde4d
 
 
 
 
f700114
 
 
 
 
 
 
 
ebfde4d
 
 
 
 
 
f700114
 
 
 
 
aae61a3
 
 
 
 
 
 
 
 
 
 
 
 
 
f700114
 
 
 
aae61a3
 
ebfde4d
 
 
 
 
 
 
 
aae61a3
 
ebfde4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f700114
ebfde4d
 
 
 
 
 
f700114
ebfde4d
 
 
 
6ea5ee2
f700114
 
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
import streamlit as st
from transformers import pipeline, AutoImageProcessor
from PIL import Image, ImageDraw
import torch

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

st.markdown("""
<style>
    .stApp {
        background-color: transparent !important;
        padding: 0 !important;
    }
    
    [data-theme="light"] {
        --background-color: #ffffff;
        --text-color: #1f2937;
        --border-color: #e5e7eb;
        --button-color: #2563eb;
        --button-hover: #1d4ed8;
    }
    
    [data-theme="dark"] {
        --background-color: #1f2937;
        --text-color: #f3f4f6;
        --border-color: #4b5563;
        --button-color: #3b82f6;
        --button-hover: #2563eb;
    }
    
    .stButton > button {
        background-color: var(--button-color) !important;
        color: white !important;
        border: none !important;
        padding: 0.5rem 1rem !important;
        border-radius: 0.375rem !important;
        font-weight: 500 !important;
        transition: background-color 0.2s !important;
    }
    
    .stButton > button:hover {
        background-color: var(--button-hover) !important;
    }
    
    .block-container {
        padding: 0.5rem !important;
        max-width: 100% !important;
    }
    
    .stImage > img {
        max-height: 250px !important;
        width: auto !important;
        margin: 0 auto !important;
    }
    
    .result-box {
        padding: 0.375rem;
        border-radius: 0.375rem;
        margin: 0.25rem 0;
        background: var(--background-color);
        border: 1px solid var(--border-color);
        color: var(--text-color);
    }
    
    h2, h3, h4 {
        margin: 0.5rem 0 !important;
        color: var(--text-color) !important;
        font-size: 1rem !important;
    }
    
    #MainMenu, footer, header {
        display: none !important;
    }
    
    .uploadedFile {
        border: 1px dashed var(--border-color);
        border-radius: 0.375rem;
        padding: 0.25rem;
    }
    
    .row-widget.stButton {
        text-align: center;
        margin: 1rem 0;
    }
    
    div[data-testid="stFileUploader"] {
        width: 100%;
    }
</style>
""", unsafe_allow_html=True)

@st.cache_resource
def load_models():
    return {
        "KnochenAuge": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),
        "KnochenWächter": pipeline("image-classification", 
            model="Heem2/bone-fracture-detection-using-xray",
            image_processor=AutoImageProcessor.from_pretrained("Heem2/bone-fracture-detection-using-xray")),
        "RöntgenMeister": pipeline("image-classification", 
            model="nandodeomkar/autotrain-fracture-detection-using-google-vit-base-patch-16-54382127388",
            image_processor=AutoImageProcessor.from_pretrained("nandodeomkar/autotrain-fracture-detection-using-google-vit-base-patch-16-54382127388"))
    }

def draw_boxes(image, predictions):
    draw = ImageDraw.Draw(image)
    for pred in predictions:
        if pred['label'].lower() == 'fracture' and pred['score'] > 0.6:
            box = pred['box']
            label = f"Fraktur ({pred['score']:.2%})"
            color = "#2563eb"
            
            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()
    
    st.markdown("### 📤 Röntgenbilder Upload")
    
    # File uploader avec label caché
    uploaded_files = st.file_uploader(
        "Wählen Sie Röntgenbilder aus",
        type=['png', 'jpg', 'jpeg'],
        accept_multiple_files=True,
        label_visibility="collapsed"
    )
    
    if uploaded_files:
        # Bouton d'analyse
        if st.button("Bilder analysieren", key="analyze_button"):
            col1, col2 = st.columns([1, 1])
            
            for idx, uploaded_file in enumerate(uploaded_files):
                image = Image.open(uploaded_file)
                
                # Analyse avec KnochenAuge (localisierung)
                predictions = models["KnochenAuge"](image)
                fractures_found = any(p['label'].lower() == 'fracture' and p['score'] > 0.6 for p in predictions)
                
                # Afficher uniquement si des fractures sont détectées
                if fractures_found:
                    with col1 if idx % 2 == 0 else col2:
                        result_image = image.copy()
                        result_image = draw_boxes(result_image, predictions)
                        st.image(result_image, caption=f"Bild {idx + 1}", use_column_width=True)
                        
                        # Analyse KnochenWächter et RöntgenMeister
                        pred_wachter = models["KnochenWächter"](image)[0]
                        pred_meister = models["RöntgenMeister"](image)[0]
                        
                        if pred_wachter['score'] > 0.6 or pred_meister['score'] > 0.6:
                            st.markdown(f"""
                                <div class='result-box'>
                                    <span style='color: #2563eb'>KnochenWächter:</span> {pred_wachter['score']:.1%}<br>
                                    <span style='color: #2563eb'>RöntgenMeister:</span> {pred_meister['score']:.1%}
                                </div>
                            """, unsafe_allow_html=True)

    # Script pour la synchronisation du thème
    st.markdown("""
        <script>
            // Fonction pour mettre à jour le thème
            function updateTheme(isDark) {
                document.documentElement.setAttribute('data-theme', isDark ? 'dark' : 'light');
                
                // Mise à jour des styles en fonction du thème
                const root = document.documentElement;
                if (isDark) {
                    root.style.setProperty('--background-color', '#1f2937');
                    root.style.setProperty('--text-color', '#f3f4f6');
                    root.style.setProperty('--border-color', '#4b5563');
                } else {
                    root.style.setProperty('--background-color', '#ffffff');
                    root.style.setProperty('--text-color', '#1f2937');
                    root.style.setProperty('--border-color', '#e5e7eb');
                }
            }
            
            // Écouter les messages du parent
            window.addEventListener('message', function(e) {
                if (e.data.type === 'theme-change') {
                    updateTheme(e.data.theme === 'dark');
                }
            });
            
            // Thème initial basé sur les préférences système
            updateTheme(window.matchMedia('(prefers-color-scheme: dark)').matches);
        </script>
    """, unsafe_allow_html=True)

if __name__ == "__main__":
    main()