Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 9 |
torch.serialization.add_safe_globals([DetectionModel, nn.Sequential, Conv])
|
| 10 |
|
| 11 |
-
# Load
|
| 12 |
-
model = YOLO("best.pt") #
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 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
|
| 38 |
)
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
-
|
|
|
|
| 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()
|