Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,121 +2,44 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from ultralytics import YOLO
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
from
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
iou_threshold: gr.inputs.Slider = 0.50):
|
44 |
-
"""This function performs YOLOv8 object detection on the given image.
|
45 |
-
|
46 |
-
Args:
|
47 |
-
image (gr.inputs.Image, optional): Input image to detect objects on. Defaults to None.
|
48 |
-
image_size (gr.inputs.Slider, optional): Desired image size for the model. Defaults to 640.
|
49 |
-
conf_threshold (gr.inputs.Slider, optional): Confidence threshold for object detection. Defaults to 0.4.
|
50 |
-
iou_threshold (gr.inputs.Slider, optional): Intersection over Union threshold for object detection. Defaults to 0.50.
|
51 |
-
"""
|
52 |
-
# Load the YOLOv8 model from the 'best.pt' checkpoint
|
53 |
-
model_path = "best.pt"
|
54 |
-
model = YOLO(model_path)
|
55 |
-
|
56 |
-
# Perform object detection on the input image using the YOLOv8 model
|
57 |
-
results = model.predict(image,
|
58 |
-
conf=conf_threshold,
|
59 |
-
iou=iou_threshold,
|
60 |
-
imgsz=image_size)
|
61 |
-
|
62 |
-
# Print the detected objects' information (class, coordinates, and probability)
|
63 |
-
box = results[0].boxes
|
64 |
-
print("Object type:", box.cls)
|
65 |
-
print("Coordinates:", box.xyxy)
|
66 |
-
print("Probability:", box.conf)
|
67 |
-
|
68 |
-
# Custom rendering function using OpenCV
|
69 |
-
def render_custom(model, image, result):
|
70 |
-
class_ids = result.boxes.cls.cpu().numpy()
|
71 |
-
confidences = result.boxes.conf.cpu().numpy()
|
72 |
-
x1y1x2y2 = result.boxes.xyxy.cpu().numpy()
|
73 |
-
|
74 |
-
for i in range(len(class_ids)):
|
75 |
-
class_id = class_ids[i]
|
76 |
-
confidence = confidences[i]
|
77 |
-
x1, y1, x2, y2 = x1y1x2y2[i]
|
78 |
-
|
79 |
-
label = f"{model.names[class_id]} {confidence:.2f}"
|
80 |
-
color = (0, 255, 0) # Green color for bounding boxes
|
81 |
-
|
82 |
-
cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
|
83 |
-
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
84 |
-
|
85 |
-
return image
|
86 |
-
|
87 |
-
# Render the output image with bounding boxes around detected objects
|
88 |
-
custom_render = render_custom(model, image, results[0])
|
89 |
-
return custom_render
|
90 |
-
|
91 |
-
|
92 |
-
inputs = [
|
93 |
-
gr.inputs.Image(type="filepath", label="Input Image"),
|
94 |
-
gr.inputs.Slider(minimum=320, maximum=1280, default=640,
|
95 |
-
step=32, label="Image Size"),
|
96 |
-
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25,
|
97 |
-
step=0.05, label="Confidence Threshold"),
|
98 |
-
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45,
|
99 |
-
step=0.05, label="IOU Threshold"),
|
100 |
-
]
|
101 |
-
|
102 |
-
|
103 |
-
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
104 |
-
|
105 |
-
title = "Bone Fracture Detection"
|
106 |
-
|
107 |
-
|
108 |
-
examples = [['one.jpg', 640, 0.5, 0.7],
|
109 |
-
['two.jpg', 800, 0.5, 0.6],
|
110 |
-
['three.jpg', 900, 0.5, 0.8]]
|
111 |
-
|
112 |
-
yolo_app = gr.Interface(
|
113 |
-
fn=yoloV8_func,
|
114 |
-
inputs=inputs,
|
115 |
-
outputs=outputs,
|
116 |
-
title=title,
|
117 |
-
examples=examples,
|
118 |
-
cache_examples=True,
|
119 |
)
|
120 |
|
121 |
-
|
122 |
-
yolo_app.launch(debug=True, enable_queue=True)
|
|
|
2 |
import torch
|
3 |
from ultralytics import YOLO
|
4 |
|
5 |
+
file_path = 'best.pt'
|
6 |
+
|
7 |
+
def load_model(file_path):
|
8 |
+
# load the model weights from the file
|
9 |
+
yolov8_model = torch.load(file_path)
|
10 |
+
|
11 |
+
yolov8_model.eval()
|
12 |
+
|
13 |
+
return yolov8_model
|
14 |
+
|
15 |
+
def predict_fracture(image):
|
16 |
+
# Preprocess the image for YOLOv8
|
17 |
+
img_tensor = to_tensor(image).unsqueeze(0) # Convert image to tensor and add batch dimension
|
18 |
+
results = yolov8_model(img_tensor) # Perform inference
|
19 |
+
|
20 |
+
# Display the results on the image
|
21 |
+
img_with_boxes = image.copy()
|
22 |
+
for box in results.xyxy[0]:
|
23 |
+
label = int(box[5])
|
24 |
+
score = float(box[4])
|
25 |
+
if label == 0: # Assuming 0 corresponds to the bone fracture class
|
26 |
+
color = "red" if score > 0.5 else "orange" # Adjust the threshold as needed
|
27 |
+
xmin, ymin, xmax, ymax = box[:4].int().tolist()
|
28 |
+
img_with_boxes.rectangle([xmin, ymin, xmax, ymax], outline=color, width=2)
|
29 |
+
img_with_boxes.text((xmin, ymin), f"Fracture: {score:.2f}", font_size=12, color=color)
|
30 |
+
|
31 |
+
return img_with_boxes
|
32 |
+
|
33 |
+
|
34 |
+
# Gradio Interface
|
35 |
+
iface = gr.Interface(
|
36 |
+
predict_fracture,
|
37 |
+
inputs=gr.Image(),
|
38 |
+
outputs=gr.Image(),
|
39 |
+
live=True,
|
40 |
+
#capture_session=True,
|
41 |
+
title="Bone Fracture Detection",
|
42 |
+
description="Upload an X-ray image to detect bone fractures using YOLOv8.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
)
|
44 |
|
45 |
+
iface.launch()
|
|