Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,62 +6,50 @@ from PIL import Image
|
|
6 |
from roboflow import Roboflow
|
7 |
from ultralytics import YOLO
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
def load_model(
|
14 |
-
# Load the Roboflow
|
15 |
-
|
16 |
-
project = rf.workspace("fyp-l87nq").project("bone-fracture-detection-rkuqr")
|
17 |
-
model = project.version(3).model
|
18 |
|
19 |
-
|
20 |
-
pytorch_model = YOLO('data.yaml')
|
21 |
-
pytorch_model.load_state_dict(torch.load(file_path, map_location=torch.device('cpu')))
|
22 |
-
pytorch_model.eval()
|
23 |
|
24 |
-
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
img = Image.fromarray(image)
|
32 |
-
img_tensor = to_tensor(img).unsqueeze(0) # Convert image to tensor and add batch dimension
|
33 |
|
34 |
-
#
|
35 |
-
with torch.no_grad():
|
36 |
-
output = model(img_tensor)
|
37 |
-
|
38 |
-
# Postprocess the inference output
|
39 |
-
results = output[0]
|
40 |
img_with_boxes = image.copy()
|
41 |
-
for box in results:
|
42 |
-
label =
|
43 |
-
score =
|
44 |
-
if label ==
|
45 |
-
color = "red"
|
46 |
-
xmin, ymin, xmax, ymax = box[
|
47 |
img_with_boxes.rectangle([xmin, ymin, xmax, ymax], outline=color, width=2)
|
48 |
img_with_boxes.text((xmin, ymin), f"Fracture: {score:.2f}", font_size=12, color=color)
|
49 |
|
50 |
-
return
|
51 |
|
52 |
-
# Define the to_tensor function
|
53 |
-
def to_tensor(image):
|
54 |
-
image = np.array(image) / 255.0
|
55 |
-
return torch.from_numpy(image.transpose((2, 0, 1))).float()
|
56 |
-
|
57 |
# Gradio Interface
|
58 |
iface = gr.Interface(
|
59 |
-
|
60 |
inputs=gr.Image(),
|
61 |
outputs=gr.Image(),
|
62 |
live=True,
|
|
|
63 |
title="Bone Fracture Detection",
|
64 |
-
description="Upload an X-ray image to detect bone fractures using
|
65 |
)
|
66 |
|
67 |
iface.launch()
|
|
|
|
6 |
from roboflow import Roboflow
|
7 |
from ultralytics import YOLO
|
8 |
|
9 |
+
# Initialize Roboflow
|
10 |
+
rf = Roboflow(api_key="K1TXQnJq7EE7yoCf1g3C")
|
11 |
+
project = rf.workspace().project("bone-fracture-detection-rkuqr")
|
12 |
+
model = project.version("3").model
|
13 |
|
14 |
+
def load_model():
|
15 |
+
# Load the model from Roboflow
|
16 |
+
yolov8_model = model.deploy()
|
|
|
|
|
17 |
|
18 |
+
return yolov8_model
|
|
|
|
|
|
|
19 |
|
20 |
+
def predict_fracture(image_path):
|
21 |
+
# Load the model
|
22 |
+
yolov8_model = load_model()
|
23 |
|
24 |
+
# Open the image
|
25 |
+
image = Image.open(image_path)
|
26 |
|
27 |
+
# Perform inference
|
28 |
+
results = yolov8_model.predict(image_path, confidence=40, overlap=30)
|
|
|
|
|
29 |
|
30 |
+
# Display the results on the image
|
|
|
|
|
|
|
|
|
|
|
31 |
img_with_boxes = image.copy()
|
32 |
+
for box in results["objects"]:
|
33 |
+
label = box["class"]
|
34 |
+
score = box["score"]
|
35 |
+
if label == "fracture":
|
36 |
+
color = "red"
|
37 |
+
xmin, ymin, xmax, ymax = box["bbox"]
|
38 |
img_with_boxes.rectangle([xmin, ymin, xmax, ymax], outline=color, width=2)
|
39 |
img_with_boxes.text((xmin, ymin), f"Fracture: {score:.2f}", font_size=12, color=color)
|
40 |
|
41 |
+
return img_with_boxes
|
42 |
|
|
|
|
|
|
|
|
|
|
|
43 |
# Gradio Interface
|
44 |
iface = gr.Interface(
|
45 |
+
predict_fracture,
|
46 |
inputs=gr.Image(),
|
47 |
outputs=gr.Image(),
|
48 |
live=True,
|
49 |
+
#capture_session=True,
|
50 |
title="Bone Fracture Detection",
|
51 |
+
description="Upload an X-ray image to detect bone fractures using YOLOv8.",
|
52 |
)
|
53 |
|
54 |
iface.launch()
|
55 |
+
|