yassonee commited on
Commit
6ea5ee2
·
verified ·
1 Parent(s): 3cabc2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -43
app.py CHANGED
@@ -1,55 +1,69 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
3
  from PIL import Image
4
  import numpy as np
5
  import cv2
6
 
7
- st.set_page_config(page_title="Détection de fractures osseuses")
8
- st.title("Détection de fractures osseuses par rayons X")
9
 
10
  @st.cache_resource
11
  def load_model():
12
- return pipeline(
13
- "object-detection",
14
- model="anirban22/detr-resnet-50-med_fracture",
15
- threshold=0.1
16
- )
17
 
18
- model = load_model()
 
 
 
 
 
 
 
 
 
19
 
20
- uploaded_file = st.file_uploader("Téléchargez une image radiographique", type=["jpg", "jpeg", "png"])
 
21
 
22
- if uploaded_file:
23
- # Convert uploaded file to PIL Image
24
- image = Image.open(uploaded_file).convert('RGB')
25
- if image.size[0] > 800:
26
- ratio = 800.0 / image.size[0]
27
- size = (800, int(image.size[1] * ratio))
28
- image = image.resize(size, Image.Resampling.LANCZOS)
29
-
30
- # Pass PIL Image directly to model
31
- predictions = model(image)
32
-
33
- col1, col2 = st.columns(2)
34
-
35
- with col1:
36
- st.image(image, caption="Image originale", use_container_width=True)
37
-
38
- with col2:
39
- img_array = np.array(image)
40
- for pred in predictions:
41
- box = pred['box']
42
- score = pred['score']
43
- x1, y1, x2, y2 = [int(i) for i in [box['xmin'], box['ymin'], box['xmax'], box['ymax']]]
44
- cv2.rectangle(img_array, (x1, y1), (x2, y2), (255, 0, 0), 3)
45
- text = f"Fracture: {score:.2f}"
46
- cv2.putText(img_array, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
47
 
48
- st.image(img_array, caption="Détection des fractures", use_container_width=True)
49
-
50
- if predictions:
51
- st.subheader(f"🚨 {len(predictions)} fractures détectées:")
52
- for idx, pred in enumerate(predictions, 1):
53
- st.warning(f"Fracture {idx}: Confiance {pred['score']*100:.1f}%")
54
- else:
55
- st.warning("⚠️ Aucune fracture détectée")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from transformers import AutoModelForObjectDetection
3
+ import torch
4
  from PIL import Image
5
  import numpy as np
6
  import cv2
7
 
8
+ st.set_page_config(page_title="Détection de nodules pulmonaires")
9
+ st.title("Détection de nodules pulmonaires sur images scanner")
10
 
11
  @st.cache_resource
12
  def load_model():
13
+ model = AutoModelForObjectDetection.from_pretrained("monai-test/lung_nodule_ct_detection")
14
+ model.eval()
15
+ return model
 
 
16
 
17
+ def process_image(image):
18
+ # Convertir en niveau de gris
19
+ img_array = np.array(image.convert('L'))
20
+ # Normaliser
21
+ normalized = (img_array - img_array.min()) / (img_array.max() - img_array.min())
22
+ # Redimensionner
23
+ resized = cv2.resize(normalized, (512, 512))
24
+ # Préparer pour PyTorch
25
+ tensor = torch.FloatTensor(resized).unsqueeze(0).unsqueeze(0)
26
+ return tensor
27
 
28
+ try:
29
+ model = load_model()
30
 
31
+ uploaded_file = st.file_uploader("Téléchargez une image scanner", type=["jpg", "jpeg", "png"])
32
+
33
+ if uploaded_file:
34
+ image = Image.open(uploaded_file)
35
+
36
+ col1, col2 = st.columns(2)
37
+
38
+ with col1:
39
+ st.image(image, caption="Image originale", use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
+ with col2:
42
+ with torch.no_grad():
43
+ input_tensor = process_image(image)
44
+ predictions = model(input_tensor)
45
+
46
+ # Visualisation
47
+ img_array = np.array(image)
48
+ for pred in predictions:
49
+ if pred['score'] > 0.5:
50
+ box = pred['box']
51
+ x1, y1, x2, y2 = map(int, [box['xmin'], box['ymin'], box['xmax'], box['ymax']])
52
+ cv2.rectangle(img_array, (x1, y1), (x2, y2), (255, 0, 0), 2)
53
+ text = f"Nodule: {pred['score']:.2f}"
54
+ cv2.putText(img_array, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
55
+
56
+ st.image(img_array, caption="Détections", use_container_width=True)
57
+
58
+ # Résultats
59
+ if len(predictions) > 0:
60
+ st.warning(f"⚠️ {len(predictions)} nodules détectés")
61
+ for i, pred in enumerate(predictions, 1):
62
+ if pred['score'] > 0.5:
63
+ st.write(f"Nodule {i}: Confiance {pred['score']:.1%}")
64
+ else:
65
+ st.success("✅ Aucun nodule détecté")
66
+
67
+ except Exception as e:
68
+ st.error(f"Erreur lors du chargement du modèle: {str(e)}")
69
+ st.info("Veuillez vérifier que le modèle est correctement configuré sur Hugging Face.")