Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,6 @@ 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/obss/sahi/main/tests/data/small-vehicles1.jpeg",
|
| 9 |
-
# "highway.jpg",
|
| 10 |
-
# )
|
| 11 |
-
# sahi.utils.file.download_from_url(
|
| 12 |
-
# "https://raw.githubusercontent.com/obss/sahi/main/tests/data/small-vehicles1.jpeg",
|
| 13 |
-
# "small-vehicles1.jpeg",
|
| 14 |
-
# )
|
| 15 |
-
# sahi.utils.file.download_from_url(
|
| 16 |
-
# "https://raw.githubusercontent.com/ultralytics/yolov5/master/data/images/zidane.jpg",
|
| 17 |
-
# "zidane.jpg",
|
| 18 |
-
# )
|
| 19 |
-
|
| 20 |
-
|
| 21 |
model_names = [
|
| 22 |
"yolov8n-seg.pt",
|
| 23 |
"yolov8s-seg.pt",
|
|
@@ -29,13 +14,12 @@ model_names = [
|
|
| 29 |
current_model_name = "yolov8m-seg.pt"
|
| 30 |
model = YOLO(current_model_name)
|
| 31 |
|
| 32 |
-
|
| 33 |
def yolov8_inference(
|
| 34 |
image: gr.inputs.Image = None,
|
| 35 |
-
model_name:
|
| 36 |
-
image_size:
|
| 37 |
-
conf_threshold:
|
| 38 |
-
iou_threshold:
|
| 39 |
):
|
| 40 |
"""
|
| 41 |
YOLOv8 inference function
|
|
@@ -49,10 +33,8 @@ def yolov8_inference(
|
|
| 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)
|
|
@@ -65,9 +47,8 @@ def yolov8_inference(
|
|
| 65 |
|
| 66 |
return renders[0]
|
| 67 |
|
| 68 |
-
|
| 69 |
inputs = [
|
| 70 |
-
gr.Image(type="
|
| 71 |
gr.Dropdown(
|
| 72 |
model_names,
|
| 73 |
value=current_model_name,
|
|
@@ -80,21 +61,21 @@ inputs = [
|
|
| 80 |
gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
|
| 81 |
]
|
| 82 |
|
| 83 |
-
outputs = gr.Image(type="
|
| 84 |
title = "Ultralytics YOLOv8 Segmentation Demo"
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
]
|
| 91 |
demo_app = gr.Interface(
|
| 92 |
fn=yolov8_inference,
|
| 93 |
inputs=inputs,
|
| 94 |
outputs=outputs,
|
| 95 |
title=title,
|
| 96 |
-
examples=
|
| 97 |
cache_examples=True,
|
| 98 |
theme="default",
|
| 99 |
)
|
|
|
|
|
|
|
| 100 |
demo_app.launch(debug=True, enable_queue=True, live=True)
|
|
|
|
| 3 |
import torch
|
| 4 |
from ultralyticsplus import YOLO, render_model_output
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
model_names = [
|
| 7 |
"yolov8n-seg.pt",
|
| 8 |
"yolov8s-seg.pt",
|
|
|
|
| 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 |
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 |
|
| 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 |
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)
|