Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,40 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from ultralytics import YOLO
|
| 3 |
-
import cv2
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
torch.serialization.add_safe_globals([DetectionModel])
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
model = YOLO("best.pt")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
def detect_fracture(image):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# Gradio
|
| 18 |
demo = gr.Interface(
|
| 19 |
fn=detect_fracture,
|
| 20 |
inputs=gr.Image(type="numpy", label="Upload Bone X-ray"),
|
| 21 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 22 |
title="Human Bone Fracture Detection",
|
| 23 |
-
description="Upload an X-ray image to detect types of human bone fractures using YOLOv8."
|
| 24 |
)
|
| 25 |
|
| 26 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from ultralytics.nn.tasks import DetectionModel # ✅ Correct import for PyTorch 2.6 fix
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
# Fix for PyTorch 2.6 safe loading
|
| 7 |
torch.serialization.add_safe_globals([DetectionModel])
|
| 8 |
|
| 9 |
+
# Load YOLOv8 model
|
| 10 |
model = YOLO("best.pt")
|
| 11 |
|
| 12 |
+
# Detection function
|
| 13 |
def detect_fracture(image):
|
| 14 |
+
# Run detection
|
| 15 |
+
results = model.predict(source=image, save=False)
|
| 16 |
+
annotated_img = results[0].plot() # Annotated image with bounding boxes
|
| 17 |
+
|
| 18 |
+
# Extract labels & confidence scores
|
| 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__":
|