yassonee commited on
Commit
f5a9b22
·
verified ·
1 Parent(s): f0f1078

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -24
app.py CHANGED
@@ -5,58 +5,59 @@ import numpy as np
5
  import cv2
6
 
7
  st.set_page_config(page_title="Détection de fractures osseuses")
8
-
9
  st.title("Détection de fractures osseuses par rayons X")
10
 
11
  @st.cache_resource
12
  def load_model():
13
- return pipeline("image-classification", model="Heem2/bone-fracture-detection-using-xray")
14
 
15
  model = load_model()
16
 
17
  uploaded_file = st.file_uploader("Téléchargez une image radiographique", type=["jpg", "jpeg", "png"])
18
 
19
  if uploaded_file:
20
- # Load and resize image
21
  image = Image.open(uploaded_file)
22
- # Resize to max 800px width while maintaining aspect ratio
23
  if image.size[0] > 800:
24
  ratio = 800.0 / image.size[0]
25
  size = (800, int(image.size[1] * ratio))
26
  image = image.resize(size, Image.Resampling.LANCZOS)
27
 
28
- # Convert to array for overlay
29
  image_array = np.array(image)
30
 
31
- # Make prediction
32
- result = model(image)[0] # Get only top prediction
33
 
34
- # Create columns for side by side display
35
  col1, col2 = st.columns(2)
36
 
37
  with col1:
38
  st.image(image, caption="Image originale", use_container_width=True)
39
 
40
  with col2:
41
- # Create colored overlay based on prediction
42
- overlay = np.zeros_like(image_array)
43
- if result['label'] == "FRACTURE":
44
- overlay[..., 0] = 255 # Red tint for fracture
45
- alpha = 0.3
46
- else:
47
- overlay[..., 1] = 255 # Green tint for normal
48
- alpha = 0.2
 
 
49
 
50
- # Blend images
51
- output = cv2.addWeighted(image_array, 1, overlay, alpha, 0)
52
- st.image(output, caption="Image analysée", use_container_width=True)
 
 
53
 
54
- # Display result
55
- st.subheader("Résultat")
56
- if result['label'] == "FRACTURE":
57
- st.error(f"⚠️ Fracture détectée (Confiance: {result['score']*100:.1f}%)")
 
58
  else:
59
- st.success(f"✅ Pas de fracture détectée (Confiance: {result['score']*100:.1f}%)")
60
 
61
  else:
62
  st.info("Veuillez télécharger une image radiographique pour l'analyse.")
 
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("object-detection", model="anirban22/detr-resnet-50-med_fracture")
13
 
14
  model = load_model()
15
 
16
  uploaded_file = st.file_uploader("Téléchargez une image radiographique", type=["jpg", "jpeg", "png"])
17
 
18
  if uploaded_file:
 
19
  image = Image.open(uploaded_file)
 
20
  if image.size[0] > 800:
21
  ratio = 800.0 / image.size[0]
22
  size = (800, int(image.size[1] * ratio))
23
  image = image.resize(size, Image.Resampling.LANCZOS)
24
 
 
25
  image_array = np.array(image)
26
 
27
+ # Get predictions
28
+ predictions = model(image)
29
 
30
+ # Create columns for display
31
  col1, col2 = st.columns(2)
32
 
33
  with col1:
34
  st.image(image, caption="Image originale", use_container_width=True)
35
 
36
  with col2:
37
+ # Draw bounding boxes
38
+ img_with_boxes = image_array.copy()
39
+ for pred in predictions:
40
+ box = pred['box']
41
+ score = pred['score']
42
+ label = pred['label']
43
+
44
+ # Draw rectangle
45
+ x1, y1, x2, y2 = [int(i) for i in [box['xmin'], box['ymin'], box['xmax'], box['ymax']]]
46
+ cv2.rectangle(img_with_boxes, (x1, y1), (x2, y2), (255, 0, 0), 2)
47
 
48
+ # Add label and score
49
+ text = f"{label}: {score:.2f}"
50
+ cv2.putText(img_with_boxes, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
51
+
52
+ st.image(img_with_boxes, caption="Fractures détectées", use_container_width=True)
53
 
54
+ # Display results
55
+ st.subheader("Résultats")
56
+ if predictions:
57
+ for pred in predictions:
58
+ st.warning(f"⚠️ {pred['label']} détectée (Confiance: {pred['score']*100:.1f}%)")
59
  else:
60
+ st.success("✅ Aucune fracture détectée")
61
 
62
  else:
63
  st.info("Veuillez télécharger une image radiographique pour l'analyse.")