Update app.py
Browse files
app.py
CHANGED
|
@@ -1,98 +1,56 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import
|
| 4 |
-
from PIL import Image, ImageDraw
|
| 5 |
import io
|
| 6 |
|
| 7 |
-
st.set_page_config(page_title="
|
| 8 |
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
-
return pipeline("
|
| 12 |
-
|
| 13 |
-
def draw_boxes(image, predictions):
|
| 14 |
-
draw = ImageDraw.Draw(image)
|
| 15 |
-
for pred in predictions:
|
| 16 |
-
box = pred['box']
|
| 17 |
-
label = f"{pred['label']} ({pred['score']:.2%})"
|
| 18 |
-
|
| 19 |
-
# Draw bounding box
|
| 20 |
-
draw.rectangle(
|
| 21 |
-
[(box['xmin'], box['ymin']), (box['xmax'], box['ymax'])],
|
| 22 |
-
outline="red",
|
| 23 |
-
width=3
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
# Draw label background
|
| 27 |
-
text_bbox = draw.textbbox((box['xmin'], box['ymin']), label)
|
| 28 |
-
draw.rectangle(text_bbox, fill="red")
|
| 29 |
-
|
| 30 |
-
# Draw label text
|
| 31 |
-
draw.text(
|
| 32 |
-
(box['xmin'], box['ymin']),
|
| 33 |
-
label,
|
| 34 |
-
fill="white"
|
| 35 |
-
)
|
| 36 |
-
return image
|
| 37 |
|
| 38 |
def main():
|
| 39 |
-
st.title("🦴
|
| 40 |
-
st.write("
|
| 41 |
|
| 42 |
pipe = load_model()
|
| 43 |
|
| 44 |
uploaded_file = st.file_uploader(
|
| 45 |
-
"
|
| 46 |
type=['png', 'jpg', 'jpeg']
|
| 47 |
)
|
| 48 |
|
| 49 |
conf_threshold = st.slider(
|
| 50 |
-
"
|
| 51 |
min_value=0.0,
|
| 52 |
max_value=1.0,
|
| 53 |
-
value=0.
|
| 54 |
-
step=0.
|
| 55 |
)
|
| 56 |
|
| 57 |
if uploaded_file:
|
| 58 |
-
col1, col2 = st.columns(2)
|
| 59 |
-
|
| 60 |
-
# Original image
|
| 61 |
image = Image.open(uploaded_file)
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
with st.spinner("Analyse en cours..."):
|
| 67 |
predictions = pipe(image)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
col2.header("Résultats de la détection")
|
| 81 |
-
col2.image(result_image)
|
| 82 |
-
|
| 83 |
-
# Display detailed predictions
|
| 84 |
-
if filtered_preds:
|
| 85 |
-
st.subheader("Détails des détections")
|
| 86 |
-
for pred in filtered_preds:
|
| 87 |
-
st.write(
|
| 88 |
-
f"• Type: {pred['label']} - "
|
| 89 |
-
f"Confiance: {pred['score']:.2%}"
|
| 90 |
-
)
|
| 91 |
-
else:
|
| 92 |
-
st.warning(
|
| 93 |
-
"Aucune fracture détectée avec le seuil de confiance actuel. "
|
| 94 |
-
"Essayez de baisser le seuil pour plus de résultats."
|
| 95 |
-
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
|
|
|
| 4 |
import io
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="Knochenbrucherkennung", layout="centered")
|
| 7 |
|
| 8 |
@st.cache_resource
|
| 9 |
def load_model():
|
| 10 |
+
return pipeline("image-classification", model="Heem2/bone-fracture-detection-using-xray")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def main():
|
| 13 |
+
st.title("🦴 Knochenbrucherkennung")
|
| 14 |
+
st.write("Laden Sie ein Röntgenbild hoch.")
|
| 15 |
|
| 16 |
pipe = load_model()
|
| 17 |
|
| 18 |
uploaded_file = st.file_uploader(
|
| 19 |
+
"Röntgenbild auswählen",
|
| 20 |
type=['png', 'jpg', 'jpeg']
|
| 21 |
)
|
| 22 |
|
| 23 |
conf_threshold = st.slider(
|
| 24 |
+
"Konfidenzschwelle",
|
| 25 |
min_value=0.0,
|
| 26 |
max_value=1.0,
|
| 27 |
+
value=0.3,
|
| 28 |
+
step=0.01
|
| 29 |
)
|
| 30 |
|
| 31 |
if uploaded_file:
|
|
|
|
|
|
|
|
|
|
| 32 |
image = Image.open(uploaded_file)
|
| 33 |
+
|
| 34 |
+
# Redimensionner l'image
|
| 35 |
+
max_size = (400, 400)
|
| 36 |
+
image.thumbnail(max_size, Image.Resampling.LANCZOS)
|
| 37 |
+
|
| 38 |
+
st.image(image, caption="Hochgeladenes Bild")
|
| 39 |
|
| 40 |
+
with st.spinner("Analyse läuft..."):
|
|
|
|
| 41 |
predictions = pipe(image)
|
| 42 |
|
| 43 |
+
st.subheader("Ergebnisse")
|
| 44 |
+
for pred in predictions:
|
| 45 |
+
if pred['score'] >= conf_threshold:
|
| 46 |
+
label = "Bruch erkannt" if "fracture" in pred['label'].lower() else "Kein Bruch"
|
| 47 |
+
st.write(f"• Diagnose: {label}")
|
| 48 |
+
st.write(f"• Konfidenz: {pred['score']:.2%}")
|
| 49 |
+
|
| 50 |
+
if "fracture" in pred['label'].lower() and pred['score'] >= conf_threshold:
|
| 51 |
+
st.warning("⚠️ Möglicher Knochenbruch erkannt!")
|
| 52 |
+
else:
|
| 53 |
+
st.success("✅ Kein Bruch erkannt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
main()
|