Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from ultralyticsplus import YOLO, render_result
|
4 |
+
|
5 |
+
# Download some example X-ray images
|
6 |
+
torch.hub.download_url_to_file(
|
7 |
+
'https://example.com/path/to/xray1.jpg', 'xray1.jpg')
|
8 |
+
torch.hub.download_url_to_file(
|
9 |
+
'https://example.com/path/to/xray2.jpg', 'xray2.jpg')
|
10 |
+
torch.hub.download_url_to_file(
|
11 |
+
'https://example.com/path/to/xray3.jpg', 'xray3.jpg')
|
12 |
+
|
13 |
+
def yoloV8_func(image: gr.inputs.Image = None,
|
14 |
+
image_size: gr.inputs.Slider = 640,
|
15 |
+
conf_threshold: gr.inputs.Slider = 0.4,
|
16 |
+
iou_threshold: gr.inputs.Slider = 0.50):
|
17 |
+
"""This function performs YOLOv8 object detection on the given image.
|
18 |
+
|
19 |
+
Args:
|
20 |
+
image (gr.inputs.Image, optional): Input image to detect objects on. Defaults to None.
|
21 |
+
image_size (gr.inputs.Slider, optional): Desired image size for the model. Defaults to 640.
|
22 |
+
conf_threshold (gr.inputs.Slider, optional): Confidence threshold for object detection. Defaults to 0.4.
|
23 |
+
iou_threshold (gr.inputs.Slider, optional): Intersection over Union threshold for object detection. Defaults to 0.50.
|
24 |
+
"""
|
25 |
+
# Load the YOLOv8 model from the 'best.pt' checkpoint
|
26 |
+
model_path = "best.pt" # Update this to the path of your YOLOv8 model trained for bone fracture detection
|
27 |
+
model = YOLO(model_path)
|
28 |
+
|
29 |
+
# Perform object detection on the input image using the YOLOv8 model
|
30 |
+
results = model.predict(image,
|
31 |
+
conf=conf_threshold,
|
32 |
+
iou=iou_threshold,
|
33 |
+
imgsz=image_size)
|
34 |
+
|
35 |
+
# Print the detected objects' information (class, coordinates, and probability)
|
36 |
+
box = results[0].boxes
|
37 |
+
print("Object type:", box.cls)
|
38 |
+
print("Coordinates:", box.xyxy)
|
39 |
+
print("Probability:", box.conf)
|
40 |
+
|
41 |
+
# Render the output image with bounding boxes around detected objects
|
42 |
+
render = render_result(model=model, image=image, result=results[0])
|
43 |
+
return render
|
44 |
+
|
45 |
+
inputs = [
|
46 |
+
gr.inputs.Image(type="filepath", label="Input X-ray Image"),
|
47 |
+
gr.inputs.Slider(minimum=320, maximum=1280, default=640,
|
48 |
+
step=32, label="Image Size"),
|
49 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25,
|
50 |
+
step=0.05, label="Confidence Threshold"),
|
51 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45,
|
52 |
+
step=0.05, label="IOU Threshold"),
|
53 |
+
]
|
54 |
+
|
55 |
+
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
56 |
+
|
57 |
+
title = "YOLOv8 Bone Fracture Detection"
|
58 |
+
|
59 |
+
examples = [['xray1.jpg', 640, 0.5, 0.7],
|
60 |
+
['xray2.jpg', 800, 0.5, 0.6],
|
61 |
+
['xray3.jpg', 900, 0.5, 0.8]]
|
62 |
+
|
63 |
+
yolo_app = gr.Interface(
|
64 |
+
fn=yoloV8_func,
|
65 |
+
inputs=inputs,
|
66 |
+
outputs=outputs,
|
67 |
+
title=title,
|
68 |
+
examples=examples,
|
69 |
+
cache_examples=True,
|
70 |
+
)
|
71 |
+
|
72 |
+
# Launch the Gradio interface in debug mode with queue enabled
|
73 |
+
yolo_app.launch(debug=True, enable_queue=True)
|