YONG627 commited on
Commit
e2adb40
·
verified ·
1 Parent(s): 47a6a81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -3,6 +3,8 @@ import sahi
3
  import torch
4
  from ultralyticsplus import YOLO, render_model_output
5
 
 
 
6
  model_names = [
7
  "yolov8n-seg.pt",
8
  "yolov8s-seg.pt",
@@ -14,12 +16,13 @@ model_names = [
14
  current_model_name = "yolov8m-seg.pt"
15
  model = YOLO(current_model_name)
16
 
 
17
  def yolov8_inference(
18
  image: gr.inputs.Image = None,
19
- model_name: str = current_model_name,
20
- image_size: int = 640,
21
- conf_threshold: float = 0.25,
22
- iou_threshold: float = 0.45,
23
  ):
24
  """
25
  YOLOv8 inference function
@@ -33,8 +36,10 @@ def yolov8_inference(
33
  Rendered image
34
  """
35
  global model
 
36
  if model_name != current_model_name:
37
  model = YOLO(model_name)
 
38
  model.overrides["conf"] = conf_threshold
39
  model.overrides["iou"] = iou_threshold
40
  results = model.predict(image, imgsz=image_size, return_outputs=True)
@@ -47,8 +52,9 @@ def yolov8_inference(
47
 
48
  return renders[0]
49
 
 
50
  inputs = [
51
- gr.Image(type="file", label="Input Image"),
52
  gr.Dropdown(
53
  model_names,
54
  value=current_model_name,
@@ -61,21 +67,21 @@ inputs = [
61
  gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
62
  ]
63
 
64
- outputs = gr.Image(type="file", label="Output Image")
65
  title = "Ultralytics YOLOv8 Segmentation Demo"
66
 
67
- # 设置默认输入参数
68
- default_input = ["ikun.jpg", current_model_name, 640, 0.6, 0.45]
 
69
 
 
70
  demo_app = gr.Interface(
71
  fn=yolov8_inference,
72
  inputs=inputs,
73
  outputs=outputs,
74
  title=title,
75
- examples=[default_input],
76
  cache_examples=True,
77
  theme="default",
78
  )
79
- #
80
- # 运行应用,并设置live=True
81
- demo_app.launch(debug=True, enable_queue=True, live=True)
 
3
  import torch
4
  from ultralyticsplus import YOLO, render_model_output
5
 
6
+
7
+
8
  model_names = [
9
  "yolov8n-seg.pt",
10
  "yolov8s-seg.pt",
 
16
  current_model_name = "yolov8m-seg.pt"
17
  model = YOLO(current_model_name)
18
 
19
+
20
  def yolov8_inference(
21
  image: gr.inputs.Image = None,
22
+ model_name: gr.inputs.Dropdown = None,
23
+ image_size: gr.inputs.Slider = 640,
24
+ conf_threshold: gr.inputs.Slider = 0.25,
25
+ iou_threshold: gr.inputs.Slider = 0.45,
26
  ):
27
  """
28
  YOLOv8 inference function
 
36
  Rendered image
37
  """
38
  global model
39
+ global current_model_name
40
  if model_name != current_model_name:
41
  model = YOLO(model_name)
42
+ current_model_name = model_name
43
  model.overrides["conf"] = conf_threshold
44
  model.overrides["iou"] = iou_threshold
45
  results = model.predict(image, imgsz=image_size, return_outputs=True)
 
52
 
53
  return renders[0]
54
 
55
+
56
  inputs = [
57
+ gr.Image(type="filepath", label="Input Image"),
58
  gr.Dropdown(
59
  model_names,
60
  value=current_model_name,
 
67
  gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
68
  ]
69
 
70
+ outputs = gr.Image(type="filepath", label="Output Image")
71
  title = "Ultralytics YOLOv8 Segmentation Demo"
72
 
73
+ examples = [
74
+ ["ikun.jpg", "yolov8m-seg.pt", 640, 0.6, 0.45],
75
+ ["people.jpg", "yolov8m-seg.pt", 640, 0.25, 0.45],
76
 
77
+ ]
78
  demo_app = gr.Interface(
79
  fn=yolov8_inference,
80
  inputs=inputs,
81
  outputs=outputs,
82
  title=title,
83
+ examples=examples,
84
  cache_examples=True,
85
  theme="default",
86
  )
87
+ demo_app.launch(debug=True, enable_queue=True)