kedimestan commited on
Commit
be786c9
·
verified ·
1 Parent(s): 9a25ce4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import gradio as gr
2
  import sahi
3
  import torch
4
- from ultralyticsplus import YOLO, render_model_output
5
 
6
- # Images
7
  sahi.utils.file.download_from_url(
8
  "https://raw.githubusercontent.com/kadirnar/dethub/main/data/images/highway.jpg",
9
  "highway.jpg",
@@ -17,7 +17,7 @@ sahi.utils.file.download_from_url(
17
  "zidane.jpg",
18
  )
19
 
20
-
21
  model_names = [
22
  "yolov8n-seg.pt",
23
  "yolov8s-seg.pt",
@@ -26,10 +26,10 @@ model_names = [
26
  "yolov8x-seg.pt",
27
  ]
28
 
 
29
  current_model_name = "yolov8m-seg.pt"
30
  model = YOLO(current_model_name)
31
 
32
-
33
  def yolov8_inference(
34
  image: gr.Image = None,
35
  model_name: gr.Dropdown = None,
@@ -38,7 +38,7 @@ def yolov8_inference(
38
  iou_threshold: gr.Slider = 0.45,
39
  ):
40
  """
41
- YOLOv8 inference function
42
  Args:
43
  image: Input image
44
  model_name: Name of the model
@@ -46,29 +46,34 @@ def yolov8_inference(
46
  conf_threshold: Confidence threshold
47
  iou_threshold: IOU threshold
48
  Returns:
49
- Rendered image
50
  """
51
  global model
52
  global current_model_name
 
 
53
  if model_name != current_model_name:
54
  model = YOLO(model_name)
55
  current_model_name = model_name
 
 
56
  model.overrides["conf"] = conf_threshold
57
  model.overrides["iou"] = iou_threshold
 
 
58
  results = model.predict(image, imgsz=image_size, return_outputs=True)
59
- masks=[]
60
- for i in results:
61
- masks.append([i.masks,i.labels])
62
- renders = []
63
- for image_results in model.predict(image, imgsz=image_size, return_outputs=True):
64
- render = render_model_output(
65
- model=model, image=image, model_output=image_results
66
- )
67
- renders.append(render)
68
-
69
- return masks
70
-
71
 
 
72
  inputs = [
73
  gr.Image(type="filepath", label="Input Image"),
74
  gr.Dropdown(
@@ -83,14 +88,18 @@ inputs = [
83
  gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
84
  ]
85
 
86
- outputs = gr.Image(type="filepath", label="Output Image")
87
- title = "Ultralytics YOLOv8 Segmentation Demo"
88
 
 
 
89
  examples = [
90
  ["zidane.jpg", "yolov8m-seg.pt", 640, 0.6, 0.45],
91
  ["highway.jpg", "yolov8m-seg.pt", 640, 0.25, 0.45],
92
  ["small-vehicles1.jpeg", "yolov8m-seg.pt", 640, 0.25, 0.45],
93
  ]
 
 
94
  demo_app = gr.Interface(
95
  fn=yolov8_inference,
96
  inputs=inputs,
@@ -100,4 +109,6 @@ demo_app = gr.Interface(
100
  cache_examples=True,
101
  theme="default",
102
  )
103
- demo_app.queue().launch(debug=True)
 
 
 
1
  import gradio as gr
2
  import sahi
3
  import torch
4
+ from ultralyticsplus import YOLO
5
 
6
+ # Download example images
7
  sahi.utils.file.download_from_url(
8
  "https://raw.githubusercontent.com/kadirnar/dethub/main/data/images/highway.jpg",
9
  "highway.jpg",
 
17
  "zidane.jpg",
18
  )
19
 
20
+ # Define the model names
21
  model_names = [
22
  "yolov8n-seg.pt",
23
  "yolov8s-seg.pt",
 
26
  "yolov8x-seg.pt",
27
  ]
28
 
29
+ # Set the initial model
30
  current_model_name = "yolov8m-seg.pt"
31
  model = YOLO(current_model_name)
32
 
 
33
  def yolov8_inference(
34
  image: gr.Image = None,
35
  model_name: gr.Dropdown = None,
 
38
  iou_threshold: gr.Slider = 0.45,
39
  ):
40
  """
41
+ YOLOv8 inference function to return masks and label names for each detected object
42
  Args:
43
  image: Input image
44
  model_name: Name of the model
 
46
  conf_threshold: Confidence threshold
47
  iou_threshold: IOU threshold
48
  Returns:
49
+ Object masks, coordinates, and label names
50
  """
51
  global model
52
  global current_model_name
53
+
54
+ # Check if a new model is selected
55
  if model_name != current_model_name:
56
  model = YOLO(model_name)
57
  current_model_name = model_name
58
+
59
+ # Set the confidence and IOU thresholds
60
  model.overrides["conf"] = conf_threshold
61
  model.overrides["iou"] = iou_threshold
62
+
63
+ # Perform model prediction
64
  results = model.predict(image, imgsz=image_size, return_outputs=True)
65
+
66
+ # Extract masks, coordinates, and label names
67
+ output = []
68
+ for result in results:
69
+ for mask, box in zip(result.masks.xy, result.boxes):
70
+ label = model.names[int(box.cls[0])]
71
+ mask_coords = mask.tolist() # Convert mask coordinates to list format
72
+ output.append({"label": label, "mask_coords": mask_coords})
73
+
74
+ return output
 
 
75
 
76
+ # Define Gradio input and output components
77
  inputs = [
78
  gr.Image(type="filepath", label="Input Image"),
79
  gr.Dropdown(
 
88
  gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
89
  ]
90
 
91
+ # Output the object masks and label names as a JSON-like format
92
+ outputs = gr.JSON(label="Detected Objects with Masks and Labels")
93
 
94
+ # Title and example inputs
95
+ title = "Ultralytics YOLOv8 Segmentation Demo"
96
  examples = [
97
  ["zidane.jpg", "yolov8m-seg.pt", 640, 0.6, 0.45],
98
  ["highway.jpg", "yolov8m-seg.pt", 640, 0.25, 0.45],
99
  ["small-vehicles1.jpeg", "yolov8m-seg.pt", 640, 0.25, 0.45],
100
  ]
101
+
102
+ # Create the Gradio interface
103
  demo_app = gr.Interface(
104
  fn=yolov8_inference,
105
  inputs=inputs,
 
109
  cache_examples=True,
110
  theme="default",
111
  )
112
+
113
+ # Launch the app
114
+ demo_app.queue().launch(debug=True)