Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
|
|
|
|
|
|
3 |
from ultralytics import YOLO
|
4 |
|
5 |
-
file_path = 'best.pt'
|
6 |
|
7 |
def load_model(file_path):
|
8 |
# Load the Roboflow model
|
@@ -19,7 +22,8 @@ def load_model(file_path):
|
|
19 |
|
20 |
def predict_fracture(image):
|
21 |
# Preprocess the image for the Roboflow model
|
22 |
-
|
|
|
23 |
|
24 |
# Perform inference with the Roboflow model
|
25 |
with torch.no_grad():
|
@@ -37,14 +41,19 @@ def predict_fracture(image):
|
|
37 |
img_with_boxes.rectangle([xmin, ymin, xmax, ymax], outline=color, width=2)
|
38 |
img_with_boxes.text((xmin, ymin), f"Fracture: {score:.2f}", font_size=12, color=color)
|
39 |
|
40 |
-
return img_with_boxes
|
41 |
-
|
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",
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from roboflow import Roboflow
|
6 |
from ultralytics import YOLO
|
7 |
|
8 |
+
#file_path = 'best.pt'
|
9 |
|
10 |
def load_model(file_path):
|
11 |
# Load the Roboflow model
|
|
|
22 |
|
23 |
def predict_fracture(image):
|
24 |
# Preprocess the image for the Roboflow model
|
25 |
+
img = Image.fromarray(image)
|
26 |
+
img_tensor = to_tensor(img).unsqueeze(0) # Convert image to tensor and add batch dimension
|
27 |
|
28 |
# Perform inference with the Roboflow model
|
29 |
with torch.no_grad():
|
|
|
41 |
img_with_boxes.rectangle([xmin, ymin, xmax, ymax], outline=color, width=2)
|
42 |
img_with_boxes.text((xmin, ymin), f"Fracture: {score:.2f}", font_size=12, color=color)
|
43 |
|
44 |
+
return np.array(img_with_boxes)
|
|
|
45 |
|
46 |
+
# Define the to_tensor function
|
47 |
+
def to_tensor(image):
|
48 |
+
image = np.array(image) / 255.0
|
49 |
+
return torch.from_numpy(image.transpose((2, 0, 1))).float()
|
50 |
+
|
51 |
# Gradio Interface
|
52 |
iface = gr.Interface(
|
53 |
predict_fracture,
|
54 |
inputs=gr.Image(),
|
55 |
outputs=gr.Image(),
|
56 |
+
load_model=load_model,
|
57 |
live=True,
|
58 |
#capture_session=True,
|
59 |
title="Bone Fracture Detection",
|