CheRy01 commited on
Commit
4f65fb5
1 Parent(s): f6d6ae3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -39
app.py CHANGED
@@ -6,62 +6,50 @@ from PIL import Image
6
  from roboflow import Roboflow
7
  from ultralytics import YOLO
8
 
9
- def summary(model, input_size):
10
- from torchsummary import summary
11
- summary(model, input_size=input_size)
 
12
 
13
- def load_model(file_path):
14
- # Load the Roboflow model
15
- rf = Roboflow(api_key="K1TXQnJq7EE7yoCf1g3C")
16
- project = rf.workspace("fyp-l87nq").project("bone-fracture-detection-rkuqr")
17
- model = project.version(3).model
18
 
19
- # Load the model weights into a PyTorch model
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
- return pytorch_model
 
 
25
 
26
- file_path = 'yolov8s.pt'
27
- model = load_model(file_path)
28
 
29
- def predict_fracture(image):
30
- # Preprocess the image for the Roboflow model
31
- img = Image.fromarray(image)
32
- img_tensor = to_tensor(img).unsqueeze(0) # Convert image to tensor and add batch dimension
33
 
34
- # Perform inference with the Roboflow model
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 = int(box[5])
43
- score = float(box[4])
44
- if label == 0: # Assuming 0 corresponds to the bone fracture class
45
- color = "red" if score > 0.5 else "orange" # Adjust the threshold as needed
46
- xmin, ymin, xmax, ymax = box[:4].int().tolist()
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 Image.fromarray((np.uint8(img_with_boxes)))
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
- fn=lambda *args, **kwargs: predict_fracture(args[0], load_model),
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 Roboflow's YOLOv8 model.",
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
+