|
import streamlit as st |
|
from transformers import pipeline |
|
from PIL import Image |
|
import numpy as np |
|
import cv2 |
|
|
|
st.set_page_config(page_title="Détection de fractures osseuses") |
|
st.title("Détection de fractures osseuses par rayons X") |
|
|
|
@st.cache_resource |
|
def load_model(): |
|
return pipeline("object-detection", model="anirban22/detr-resnet-50-med_fracture") |
|
|
|
model = load_model() |
|
|
|
uploaded_file = st.file_uploader("Téléchargez une image radiographique", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file: |
|
image = Image.open(uploaded_file) |
|
if image.size[0] > 800: |
|
ratio = 800.0 / image.size[0] |
|
size = (800, int(image.size[1] * ratio)) |
|
image = image.resize(size, Image.Resampling.LANCZOS) |
|
|
|
image_array = np.array(image) |
|
|
|
|
|
predictions = model(image) |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.image(image, caption="Image originale", use_container_width=True) |
|
|
|
with col2: |
|
|
|
img_with_boxes = image_array.copy() |
|
for pred in predictions: |
|
box = pred['box'] |
|
score = pred['score'] |
|
label = pred['label'] |
|
|
|
|
|
x1, y1, x2, y2 = [int(i) for i in [box['xmin'], box['ymin'], box['xmax'], box['ymax']]] |
|
cv2.rectangle(img_with_boxes, (x1, y1), (x2, y2), (255, 0, 0), 2) |
|
|
|
|
|
text = f"{label}: {score:.2f}" |
|
cv2.putText(img_with_boxes, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) |
|
|
|
st.image(img_with_boxes, caption="Fractures détectées", use_container_width=True) |
|
|
|
|
|
st.subheader("Résultats") |
|
if predictions: |
|
for pred in predictions: |
|
st.warning(f"⚠️ {pred['label']} détectée (Confiance: {pred['score']*100:.1f}%)") |
|
else: |
|
st.success("✅ Aucune fracture détectée") |
|
|
|
else: |
|
st.info("Veuillez télécharger une image radiographique pour l'analyse.") |