ma4389 commited on
Commit
f92619f
·
verified ·
1 Parent(s): 15f6f94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -26
app.py CHANGED
@@ -1,41 +1,35 @@
1
  import torch
 
2
  from ultralytics import YOLO
3
  from ultralytics.nn.tasks import DetectionModel
4
  from ultralytics.nn.modules.conv import Conv
5
  import torch.nn as nn
 
6
  import gradio as gr
7
 
8
- # Whitelist all necessary classes for PyTorch 2.6 safe loading
9
  torch.serialization.add_safe_globals([DetectionModel, nn.Sequential, Conv])
10
 
11
- # Load YOLOv8 model
12
- model = YOLO("best.pt") # Change to your model file path
13
 
14
- # Detection function
15
- def detect_fracture(image):
16
- results = model.predict(source=image, save=False)
17
- annotated_img = results[0].plot()
 
 
 
 
18
 
19
- detections = []
20
- for box in results[0].boxes:
21
- cls_id = int(box.cls[0])
22
- label = model.names[cls_id]
23
- conf = float(box.conf[0])
24
- detections.append(f"{label}: {conf:.2f}")
25
-
26
- return annotated_img, "\n".join(detections) if detections else "No fracture detected"
27
-
28
- # Gradio app
29
- demo = gr.Interface(
30
- fn=detect_fracture,
31
- inputs=gr.Image(type="numpy", label="Upload Bone X-ray"),
32
- outputs=[
33
- gr.Image(type="numpy", label="Detection Result"),
34
- gr.Textbox(label="Detected Fractures & Confidence Scores")
35
- ],
36
  title="Human Bone Fracture Detection",
37
- description="Upload an X-ray image to detect types of human bone fractures using a YOLOv8 model."
38
  )
39
 
40
  if __name__ == "__main__":
41
- demo.launch(debug=True)
 
1
  import torch
2
+ import ultralytics
3
  from ultralytics import YOLO
4
  from ultralytics.nn.tasks import DetectionModel
5
  from ultralytics.nn.modules.conv import Conv
6
  import torch.nn as nn
7
+ import cv2
8
  import gradio as gr
9
 
10
+ # ---- FIX for PyTorch 2.6+ ----
11
  torch.serialization.add_safe_globals([DetectionModel, nn.Sequential, Conv])
12
 
13
+ # ---- Load trained YOLO model ----
14
+ model = YOLO("best.pt") # Ensure your model file is in the same folder
15
 
16
+ # ---- Prediction function ----
17
+ def predict(image):
18
+ # Run inference
19
+ results = model.predict(source=image, conf=0.25)
20
+ # Draw boxes on the image
21
+ result_image = results[0].plot()
22
+ # Convert BGR → RGB for Gradio
23
+ return cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
24
 
25
+ # ---- Gradio Interface ----
26
+ iface = gr.Interface(
27
+ fn=predict,
28
+ inputs=gr.Image(type="filepath", label="Upload Bone X-ray"),
29
+ outputs=gr.Image(type="numpy", label="Detection Result"),
 
 
 
 
 
 
 
 
 
 
 
 
30
  title="Human Bone Fracture Detection",
31
+ description="Upload an X-ray image to detect types of human bone fractures using YOLOv8."
32
  )
33
 
34
  if __name__ == "__main__":
35
+ iface.launch()