Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
-
# Gradio YOLOv8 Det v1.3.
|
| 2 |
# 创建人:曾逸夫
|
| 3 |
-
# 创建时间:
|
| 4 |
-
# pip install gradio>=4.
|
| 5 |
# python gradio_yolov8_det_v1.py
|
| 6 |
|
| 7 |
|
|
|
|
| 8 |
import argparse
|
| 9 |
import csv
|
| 10 |
import random
|
|
@@ -36,9 +37,6 @@ from PIL import Image, ImageDraw, ImageFont
|
|
| 36 |
|
| 37 |
from util.fonts_opt import is_fonts
|
| 38 |
|
| 39 |
-
# Gradio YOLOv8 Det版本
|
| 40 |
-
GYD_VERSION = "Gradio YOLOv8 Det v1.2.1"
|
| 41 |
-
|
| 42 |
# 文件后缀
|
| 43 |
suffix_list = [".csv", ".yaml"]
|
| 44 |
|
|
@@ -63,9 +61,9 @@ GYD_SUB_TITLE = """
|
|
| 63 |
"""
|
| 64 |
|
| 65 |
EXAMPLES_DET = [
|
| 66 |
-
["./img_examples/bus.jpg", "yolov8s", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
| 67 |
-
["./img_examples/giraffe.jpg", "yolov8l", "cpu", 320, 0.5, 0.45, 100, "所有尺寸"],
|
| 68 |
-
["./img_examples/zidane.jpg", "yolov8m", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
| 69 |
[
|
| 70 |
"./img_examples/Millenial-at-work.jpg",
|
| 71 |
"yolov8x",
|
|
@@ -74,9 +72,10 @@ EXAMPLES_DET = [
|
|
| 74 |
0.5,
|
| 75 |
0.5,
|
| 76 |
100,
|
|
|
|
| 77 |
"所有尺寸",
|
| 78 |
],
|
| 79 |
-
["./img_examples/bus.jpg", "yolov8s-seg", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
| 80 |
[
|
| 81 |
"./img_examples/Millenial-at-work.jpg",
|
| 82 |
"yolov8x-seg",
|
|
@@ -85,17 +84,18 @@ EXAMPLES_DET = [
|
|
| 85 |
0.5,
|
| 86 |
0.5,
|
| 87 |
100,
|
|
|
|
| 88 |
"所有尺寸",
|
| 89 |
],
|
| 90 |
]
|
| 91 |
|
| 92 |
EXAMPLES_CLAS = [
|
| 93 |
-
["./img_examples/img_clas/ILSVRC2012_val_00000008.JPEG", "yolov8s-cls"],
|
| 94 |
-
["./img_examples/img_clas/ILSVRC2012_val_00000018.JPEG", "yolov8l-cls"],
|
| 95 |
-
["./img_examples/img_clas/ILSVRC2012_val_00000023.JPEG", "yolov8m-cls"],
|
| 96 |
-
["./img_examples/img_clas/ILSVRC2012_val_00000067.JPEG", "yolov8m-cls"],
|
| 97 |
-
["./img_examples/img_clas/ILSVRC2012_val_00000077.JPEG", "yolov8m-cls"],
|
| 98 |
-
["./img_examples/img_clas/ILSVRC2012_val_00000247.JPEG", "yolov8m-cls"],
|
| 99 |
]
|
| 100 |
|
| 101 |
GYD_CSS = """#disp_image {
|
|
@@ -103,8 +103,11 @@ GYD_CSS = """#disp_image {
|
|
| 103 |
}"""
|
| 104 |
|
| 105 |
|
|
|
|
|
|
|
|
|
|
| 106 |
def parse_args(known=False):
|
| 107 |
-
parser = argparse.ArgumentParser(description=
|
| 108 |
parser.add_argument(
|
| 109 |
"--model_name", "-mn", default="yolov8s", type=str, help="model name"
|
| 110 |
)
|
|
@@ -315,9 +318,18 @@ def seg_output(img_path, seg_mask_list, color_list, cls_list):
|
|
| 315 |
|
| 316 |
# 目标检测和图像分割模型加载
|
| 317 |
def model_det_loading(
|
| 318 |
-
img_path,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
):
|
| 320 |
model = YOLO(yolo_model)
|
|
|
|
|
|
|
| 321 |
|
| 322 |
results = model(
|
| 323 |
source=img_path,
|
|
@@ -325,6 +337,7 @@ def model_det_loading(
|
|
| 325 |
imgsz=infer_size,
|
| 326 |
conf=conf,
|
| 327 |
iou=iou,
|
|
|
|
| 328 |
max_det=max_det,
|
| 329 |
)
|
| 330 |
results = list(results)[0]
|
|
@@ -332,17 +345,25 @@ def model_det_loading(
|
|
| 332 |
|
| 333 |
|
| 334 |
# 图像分类模型加载
|
| 335 |
-
def model_cls_loading(img_path, yolo_model="yolov8s-cls.pt"):
|
| 336 |
model = YOLO(yolo_model)
|
| 337 |
|
| 338 |
-
results = model(source=img_path)
|
| 339 |
results = list(results)[0]
|
| 340 |
return results
|
| 341 |
|
| 342 |
|
| 343 |
# YOLOv8图片检测函数
|
| 344 |
def yolo_det_img(
|
| 345 |
-
img_path,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
):
|
| 347 |
global model, model_name_tmp, device_tmp
|
| 348 |
|
|
@@ -363,8 +384,10 @@ def yolo_det_img(
|
|
| 363 |
iou,
|
| 364 |
infer_size,
|
| 365 |
max_det,
|
|
|
|
| 366 |
yolo_model=f"{model_name}.pt",
|
| 367 |
)
|
|
|
|
| 368 |
# 检测参数
|
| 369 |
xyxy_list = predict_results.boxes.xyxy.cpu().numpy().tolist()
|
| 370 |
conf_list = predict_results.boxes.conf.cpu().numpy().tolist()
|
|
@@ -520,9 +543,11 @@ def yolo_det_img(
|
|
| 520 |
|
| 521 |
|
| 522 |
# YOLOv8图片分类函数
|
| 523 |
-
def yolo_cls_img(img_path, model_name):
|
| 524 |
# 模型加载
|
| 525 |
-
predict_results = model_cls_loading(
|
|
|
|
|
|
|
| 526 |
|
| 527 |
det_img = Image.open(img_path)
|
| 528 |
clas_ratio_list = predict_results.probs.top5conf.tolist()
|
|
@@ -572,8 +597,6 @@ def main(args):
|
|
| 572 |
button_secondary_background_fill_hover="*neutral_200",
|
| 573 |
)
|
| 574 |
|
| 575 |
-
custom_css = GYD_CSS
|
| 576 |
-
|
| 577 |
# ------------ Gradio Blocks ------------
|
| 578 |
with gr.Blocks(theme=custom_theme, css=custom_css) as gyd:
|
| 579 |
with gr.Row():
|
|
@@ -581,110 +604,203 @@ def main(args):
|
|
| 581 |
with gr.Row():
|
| 582 |
gr.Markdown(GYD_SUB_TITLE)
|
| 583 |
with gr.Row():
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
|
|
|
| 587 |
with gr.Column(scale=1):
|
| 588 |
with gr.Row():
|
| 589 |
-
inputs_img = gr.Image(
|
|
|
|
|
|
|
| 590 |
with gr.Row():
|
| 591 |
-
device_opt = gr.Radio(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 592 |
with gr.Row():
|
| 593 |
-
inputs_model = gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
with gr.Accordion("高级设置", open=True):
|
| 595 |
with gr.Row():
|
| 596 |
-
inputs_size = gr.Slider(
|
| 597 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 598 |
with gr.Row():
|
| 599 |
-
input_conf = gr.Slider(
|
| 600 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 601 |
with gr.Row():
|
| 602 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 603 |
with gr.Row():
|
| 604 |
gr.ClearButton(inputs_img, value="清除")
|
| 605 |
-
det_btn_img = gr.Button(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 606 |
with gr.Column(scale=1):
|
| 607 |
# with gr.Row():
|
| 608 |
# outputs_img = gr.Image(type="pil", label="检测图片")
|
| 609 |
with gr.Row():
|
| 610 |
-
outputs_img_slider = ImageSlider(
|
|
|
|
|
|
|
| 611 |
with gr.Row():
|
| 612 |
outputs_imgfiles = gr.Files(label="图片下载")
|
| 613 |
with gr.Row():
|
| 614 |
outputs_objSize = gr.Label(label="目标尺寸占比统计")
|
| 615 |
with gr.Row():
|
| 616 |
outputs_clsSize = gr.Label(label="类别检测占比统计")
|
|
|
|
|
|
|
| 617 |
with gr.Row():
|
| 618 |
gr.Examples(
|
| 619 |
examples=EXAMPLES_DET,
|
| 620 |
fn=yolo_det_img,
|
| 621 |
inputs=[
|
| 622 |
-
inputs_img,
|
| 623 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
# outputs=[outputs_img, outputs_objSize, outputs_clsSize],
|
| 625 |
-
cache_examples=False
|
|
|
|
| 626 |
|
| 627 |
-
|
| 628 |
-
|
|
|
|
| 629 |
with gr.Column(scale=1):
|
| 630 |
with gr.Row():
|
| 631 |
-
inputs_img_cls = gr.Image(
|
|
|
|
|
|
|
| 632 |
with gr.Row():
|
| 633 |
-
|
| 634 |
-
"
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
with gr.Row():
|
| 639 |
gr.ClearButton(inputs_img, value="清除")
|
| 640 |
-
det_btn_img_cls = gr.Button(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 641 |
with gr.Column(scale=1):
|
| 642 |
with gr.Row():
|
| 643 |
outputs_img_cls = gr.Image(type="pil", label="检测图片")
|
| 644 |
with gr.Row():
|
| 645 |
outputs_ratio_cls = gr.Label(label="图像分类结果")
|
|
|
|
|
|
|
| 646 |
with gr.Row():
|
| 647 |
gr.Examples(
|
| 648 |
examples=EXAMPLES_CLAS,
|
| 649 |
fn=yolo_cls_img,
|
| 650 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 651 |
# outputs=[outputs_img_cls, outputs_ratio_cls],
|
| 652 |
-
cache_examples=False
|
| 653 |
-
|
| 654 |
|
| 655 |
with gr.Accordion("Gradio YOLOv8 Det 安装与使用教程"):
|
| 656 |
-
gr.
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
|
|
|
| 688 |
|
| 689 |
det_btn_img.click(
|
| 690 |
fn=yolo_det_img,
|
|
@@ -696,6 +812,7 @@ def main(args):
|
|
| 696 |
input_conf,
|
| 697 |
inputs_iou,
|
| 698 |
max_det,
|
|
|
|
| 699 |
obj_size,
|
| 700 |
],
|
| 701 |
outputs=[
|
|
@@ -708,7 +825,7 @@ def main(args):
|
|
| 708 |
|
| 709 |
det_btn_img_cls.click(
|
| 710 |
fn=yolo_cls_img,
|
| 711 |
-
inputs=[inputs_img_cls, inputs_model_cls],
|
| 712 |
outputs=[outputs_img_cls, outputs_ratio_cls],
|
| 713 |
)
|
| 714 |
|
|
@@ -726,4 +843,4 @@ if __name__ == "__main__":
|
|
| 726 |
favicon_path="./icon/logo.ico", # 网页图标
|
| 727 |
show_error=True, # 在浏览器控制台中显示错误信息
|
| 728 |
quiet=True, # 禁止大多数打印语句
|
| 729 |
-
)
|
|
|
|
| 1 |
+
# Gradio YOLOv8 Det v1.3.1
|
| 2 |
# 创建人:曾逸夫
|
| 3 |
+
# 创建时间:2024-01-03
|
| 4 |
+
# pip install gradio>=4.12.0
|
| 5 |
# python gradio_yolov8_det_v1.py
|
| 6 |
|
| 7 |
|
| 8 |
+
import __init__
|
| 9 |
import argparse
|
| 10 |
import csv
|
| 11 |
import random
|
|
|
|
| 37 |
|
| 38 |
from util.fonts_opt import is_fonts
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
# 文件后缀
|
| 41 |
suffix_list = [".csv", ".yaml"]
|
| 42 |
|
|
|
|
| 61 |
"""
|
| 62 |
|
| 63 |
EXAMPLES_DET = [
|
| 64 |
+
["./img_examples/bus.jpg", "yolov8s", "cpu", 640, 0.6, 0.5, 100, [], "所有尺寸"],
|
| 65 |
+
["./img_examples/giraffe.jpg", "yolov8l", "cpu", 320, 0.5, 0.45, 100, [], "所有尺寸"],
|
| 66 |
+
["./img_examples/zidane.jpg", "yolov8m", "cpu", 640, 0.6, 0.5, 100, [], "所有尺寸"],
|
| 67 |
[
|
| 68 |
"./img_examples/Millenial-at-work.jpg",
|
| 69 |
"yolov8x",
|
|
|
|
| 72 |
0.5,
|
| 73 |
0.5,
|
| 74 |
100,
|
| 75 |
+
[],
|
| 76 |
"所有尺寸",
|
| 77 |
],
|
| 78 |
+
["./img_examples/bus.jpg", "yolov8s-seg", "cpu", 640, 0.6, 0.5, 100, [], "所有尺寸"],
|
| 79 |
[
|
| 80 |
"./img_examples/Millenial-at-work.jpg",
|
| 81 |
"yolov8x-seg",
|
|
|
|
| 84 |
0.5,
|
| 85 |
0.5,
|
| 86 |
100,
|
| 87 |
+
[],
|
| 88 |
"所有尺寸",
|
| 89 |
],
|
| 90 |
]
|
| 91 |
|
| 92 |
EXAMPLES_CLAS = [
|
| 93 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000008.JPEG", "cpu", "yolov8s-cls"],
|
| 94 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000018.JPEG", "cpu", "yolov8l-cls"],
|
| 95 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000023.JPEG", "cpu", "yolov8m-cls"],
|
| 96 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000067.JPEG", "cpu", "yolov8m-cls"],
|
| 97 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000077.JPEG", "cpu", "yolov8m-cls"],
|
| 98 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000247.JPEG", "cpu", "yolov8m-cls"],
|
| 99 |
]
|
| 100 |
|
| 101 |
GYD_CSS = """#disp_image {
|
|
|
|
| 103 |
}"""
|
| 104 |
|
| 105 |
|
| 106 |
+
custom_css = "./gyd_style.css"
|
| 107 |
+
|
| 108 |
+
|
| 109 |
def parse_args(known=False):
|
| 110 |
+
parser = argparse.ArgumentParser(description=__init__.__version__)
|
| 111 |
parser.add_argument(
|
| 112 |
"--model_name", "-mn", default="yolov8s", type=str, help="model name"
|
| 113 |
)
|
|
|
|
| 318 |
|
| 319 |
# 目标检测和图像分割模型加载
|
| 320 |
def model_det_loading(
|
| 321 |
+
img_path,
|
| 322 |
+
device_opt,
|
| 323 |
+
conf,
|
| 324 |
+
iou,
|
| 325 |
+
infer_size,
|
| 326 |
+
max_det,
|
| 327 |
+
inputs_cls_name,
|
| 328 |
+
yolo_model="yolov8n.pt",
|
| 329 |
):
|
| 330 |
model = YOLO(yolo_model)
|
| 331 |
+
if inputs_cls_name == []:
|
| 332 |
+
inputs_cls_name = None
|
| 333 |
|
| 334 |
results = model(
|
| 335 |
source=img_path,
|
|
|
|
| 337 |
imgsz=infer_size,
|
| 338 |
conf=conf,
|
| 339 |
iou=iou,
|
| 340 |
+
classes=inputs_cls_name,
|
| 341 |
max_det=max_det,
|
| 342 |
)
|
| 343 |
results = list(results)[0]
|
|
|
|
| 345 |
|
| 346 |
|
| 347 |
# 图像分类模型加载
|
| 348 |
+
def model_cls_loading(img_path, device_opt, yolo_model="yolov8s-cls.pt"):
|
| 349 |
model = YOLO(yolo_model)
|
| 350 |
|
| 351 |
+
results = model(source=img_path, device=device_opt)
|
| 352 |
results = list(results)[0]
|
| 353 |
return results
|
| 354 |
|
| 355 |
|
| 356 |
# YOLOv8图片检测函数
|
| 357 |
def yolo_det_img(
|
| 358 |
+
img_path,
|
| 359 |
+
model_name,
|
| 360 |
+
device_opt,
|
| 361 |
+
infer_size,
|
| 362 |
+
conf,
|
| 363 |
+
iou,
|
| 364 |
+
max_det,
|
| 365 |
+
inputs_cls_name,
|
| 366 |
+
obj_size,
|
| 367 |
):
|
| 368 |
global model, model_name_tmp, device_tmp
|
| 369 |
|
|
|
|
| 384 |
iou,
|
| 385 |
infer_size,
|
| 386 |
max_det,
|
| 387 |
+
inputs_cls_name,
|
| 388 |
yolo_model=f"{model_name}.pt",
|
| 389 |
)
|
| 390 |
+
|
| 391 |
# 检测参数
|
| 392 |
xyxy_list = predict_results.boxes.xyxy.cpu().numpy().tolist()
|
| 393 |
conf_list = predict_results.boxes.conf.cpu().numpy().tolist()
|
|
|
|
| 543 |
|
| 544 |
|
| 545 |
# YOLOv8图片分类函数
|
| 546 |
+
def yolo_cls_img(img_path, device_opt, model_name):
|
| 547 |
# 模型加载
|
| 548 |
+
predict_results = model_cls_loading(
|
| 549 |
+
img_path, device_opt, yolo_model=f"{model_name}.pt"
|
| 550 |
+
)
|
| 551 |
|
| 552 |
det_img = Image.open(img_path)
|
| 553 |
clas_ratio_list = predict_results.probs.top5conf.tolist()
|
|
|
|
| 597 |
button_secondary_background_fill_hover="*neutral_200",
|
| 598 |
)
|
| 599 |
|
|
|
|
|
|
|
| 600 |
# ------------ Gradio Blocks ------------
|
| 601 |
with gr.Blocks(theme=custom_theme, css=custom_css) as gyd:
|
| 602 |
with gr.Row():
|
|
|
|
| 604 |
with gr.Row():
|
| 605 |
gr.Markdown(GYD_SUB_TITLE)
|
| 606 |
with gr.Row():
|
| 607 |
+
with gr.Tabs():
|
| 608 |
+
with gr.TabItem("目标检测与图像分割"):
|
| 609 |
+
with gr.Row():
|
| 610 |
+
with gr.Group(elem_id="show_box"):
|
| 611 |
with gr.Column(scale=1):
|
| 612 |
with gr.Row():
|
| 613 |
+
inputs_img = gr.Image(
|
| 614 |
+
image_mode="RGB", type="filepath", label="原始图片"
|
| 615 |
+
)
|
| 616 |
with gr.Row():
|
| 617 |
+
device_opt = gr.Radio(
|
| 618 |
+
choices=["cpu", 0, 1, 2, 3],
|
| 619 |
+
value="cpu",
|
| 620 |
+
label="设备",
|
| 621 |
+
)
|
| 622 |
with gr.Row():
|
| 623 |
+
inputs_model = gr.Dropdown(
|
| 624 |
+
choices=model_names,
|
| 625 |
+
value=model_name,
|
| 626 |
+
type="value",
|
| 627 |
+
label="模型",
|
| 628 |
+
)
|
| 629 |
with gr.Accordion("高级设置", open=True):
|
| 630 |
with gr.Row():
|
| 631 |
+
inputs_size = gr.Slider(
|
| 632 |
+
320,
|
| 633 |
+
1600,
|
| 634 |
+
step=1,
|
| 635 |
+
value=inference_size,
|
| 636 |
+
label="推理尺寸",
|
| 637 |
+
)
|
| 638 |
+
max_det = gr.Slider(
|
| 639 |
+
1,
|
| 640 |
+
1000,
|
| 641 |
+
step=1,
|
| 642 |
+
value=max_detnum,
|
| 643 |
+
label="最大检测数",
|
| 644 |
+
)
|
| 645 |
with gr.Row():
|
| 646 |
+
input_conf = gr.Slider(
|
| 647 |
+
0,
|
| 648 |
+
1,
|
| 649 |
+
step=slider_step,
|
| 650 |
+
value=nms_conf,
|
| 651 |
+
label="置信度阈值",
|
| 652 |
+
)
|
| 653 |
+
inputs_iou = gr.Slider(
|
| 654 |
+
0,
|
| 655 |
+
1,
|
| 656 |
+
step=slider_step,
|
| 657 |
+
value=nms_iou,
|
| 658 |
+
label="IoU 阈值",
|
| 659 |
+
)
|
| 660 |
with gr.Row():
|
| 661 |
+
inputs_cls_name = gr.Dropdown(
|
| 662 |
+
choices=model_cls_name_cp,
|
| 663 |
+
value=[],
|
| 664 |
+
multiselect=True,
|
| 665 |
+
allow_custom_value=True,
|
| 666 |
+
type="index",
|
| 667 |
+
label="类别选择",
|
| 668 |
+
)
|
| 669 |
+
with gr.Row():
|
| 670 |
+
obj_size = gr.Radio(
|
| 671 |
+
choices=["所有尺寸", "小目标", "中目标", "大目标"],
|
| 672 |
+
value="所有尺寸",
|
| 673 |
+
label="目标尺寸",
|
| 674 |
+
)
|
| 675 |
with gr.Row():
|
| 676 |
gr.ClearButton(inputs_img, value="清除")
|
| 677 |
+
det_btn_img = gr.Button(
|
| 678 |
+
value="检测", variant="primary"
|
| 679 |
+
)
|
| 680 |
+
|
| 681 |
+
with gr.Group(elem_id="show_box"):
|
| 682 |
with gr.Column(scale=1):
|
| 683 |
# with gr.Row():
|
| 684 |
# outputs_img = gr.Image(type="pil", label="检测图片")
|
| 685 |
with gr.Row():
|
| 686 |
+
outputs_img_slider = ImageSlider(
|
| 687 |
+
position=0.5, label="检测图片"
|
| 688 |
+
)
|
| 689 |
with gr.Row():
|
| 690 |
outputs_imgfiles = gr.Files(label="图片下载")
|
| 691 |
with gr.Row():
|
| 692 |
outputs_objSize = gr.Label(label="目标尺寸占比统计")
|
| 693 |
with gr.Row():
|
| 694 |
outputs_clsSize = gr.Label(label="类别检测占比统计")
|
| 695 |
+
|
| 696 |
+
with gr.Group(elem_id="show_box"):
|
| 697 |
with gr.Row():
|
| 698 |
gr.Examples(
|
| 699 |
examples=EXAMPLES_DET,
|
| 700 |
fn=yolo_det_img,
|
| 701 |
inputs=[
|
| 702 |
+
inputs_img,
|
| 703 |
+
inputs_model,
|
| 704 |
+
device_opt,
|
| 705 |
+
inputs_size,
|
| 706 |
+
input_conf,
|
| 707 |
+
inputs_iou,
|
| 708 |
+
max_det,
|
| 709 |
+
inputs_cls_name,
|
| 710 |
+
obj_size,
|
| 711 |
+
],
|
| 712 |
# outputs=[outputs_img, outputs_objSize, outputs_clsSize],
|
| 713 |
+
cache_examples=False,
|
| 714 |
+
)
|
| 715 |
|
| 716 |
+
with gr.TabItem("图像分类"):
|
| 717 |
+
with gr.Row():
|
| 718 |
+
with gr.Group(elem_id="show_box"):
|
| 719 |
with gr.Column(scale=1):
|
| 720 |
with gr.Row():
|
| 721 |
+
inputs_img_cls = gr.Image(
|
| 722 |
+
image_mode="RGB", type="filepath", label="原始图片"
|
| 723 |
+
)
|
| 724 |
with gr.Row():
|
| 725 |
+
device_opt_cls = gr.Radio(
|
| 726 |
+
choices=["cpu", "0", "1", "2", "3"],
|
| 727 |
+
value="cpu",
|
| 728 |
+
label="设备",
|
| 729 |
+
)
|
| 730 |
+
with gr.Row():
|
| 731 |
+
inputs_model_cls = gr.Dropdown(
|
| 732 |
+
choices=[
|
| 733 |
+
"yolov8n-cls",
|
| 734 |
+
"yolov8s-cls",
|
| 735 |
+
"yolov8l-cls",
|
| 736 |
+
"yolov8m-cls",
|
| 737 |
+
"yolov8x-cls",
|
| 738 |
+
],
|
| 739 |
+
value="yolov8s-cls",
|
| 740 |
+
type="value",
|
| 741 |
+
label="模型",
|
| 742 |
+
)
|
| 743 |
with gr.Row():
|
| 744 |
gr.ClearButton(inputs_img, value="清除")
|
| 745 |
+
det_btn_img_cls = gr.Button(
|
| 746 |
+
value="检测", variant="primary"
|
| 747 |
+
)
|
| 748 |
+
|
| 749 |
+
with gr.Group(elem_id="show_box"):
|
| 750 |
with gr.Column(scale=1):
|
| 751 |
with gr.Row():
|
| 752 |
outputs_img_cls = gr.Image(type="pil", label="检测图片")
|
| 753 |
with gr.Row():
|
| 754 |
outputs_ratio_cls = gr.Label(label="图像分类结果")
|
| 755 |
+
|
| 756 |
+
with gr.Group(elem_id="show_box"):
|
| 757 |
with gr.Row():
|
| 758 |
gr.Examples(
|
| 759 |
examples=EXAMPLES_CLAS,
|
| 760 |
fn=yolo_cls_img,
|
| 761 |
+
inputs=[
|
| 762 |
+
inputs_img_cls,
|
| 763 |
+
device_opt_cls,
|
| 764 |
+
inputs_model_cls,
|
| 765 |
+
],
|
| 766 |
# outputs=[outputs_img_cls, outputs_ratio_cls],
|
| 767 |
+
cache_examples=False,
|
| 768 |
+
)
|
| 769 |
|
| 770 |
with gr.Accordion("Gradio YOLOv8 Det 安装与使用教程"):
|
| 771 |
+
with gr.Group(elem_id="show_box"):
|
| 772 |
+
gr.Markdown(
|
| 773 |
+
"""## Gradio YOLOv8 Det 安装与使用教程
|
| 774 |
+
```shell
|
| 775 |
+
conda create -n yolo python==3.8
|
| 776 |
+
conda activate yolo # 进入环境
|
| 777 |
+
git clone https://gitee.com/CV_Lab/gradio-yolov8-det.git
|
| 778 |
+
cd gradio-yolov8-det
|
| 779 |
+
pip install -r ./requirements.txt -U
|
| 780 |
+
```
|
| 781 |
+
```shell
|
| 782 |
+
# 共享模式
|
| 783 |
+
python gradio_yolov8_det_v1.py -is # 在浏览器中以共享模式打开,https://**.gradio.app/
|
| 784 |
+
# 自定义模型配置
|
| 785 |
+
python gradio_yolov8_det_v1.py -mc ./model_config/model_name_all.yaml
|
| 786 |
+
# 自定义下拉框默认模型名称
|
| 787 |
+
python gradio_yolov8_det_v1.py -mn yolov8m
|
| 788 |
+
# 自定义类别名称
|
| 789 |
+
python gradio_yolov8_det_v1.py -cls ./cls_name/cls_name_zh.yaml (目标检测与图像分割)
|
| 790 |
+
python gradio_yolov8_det_v1.py -cin ./cls_name/cls_imgnet_name_zh.yaml (图像分类)
|
| 791 |
+
# 自定义NMS置信度阈值
|
| 792 |
+
python gradio_yolov8_det_v1.py -conf 0.8
|
| 793 |
+
# 自定义NMS IoU阈值
|
| 794 |
+
python gradio_yolov8_det_v1.py -iou 0.5
|
| 795 |
+
# 设置推理尺寸,默认为640
|
| 796 |
+
python gradio_yolov8_det_v1.py -isz 320
|
| 797 |
+
# 设置最大检测数,默认为50
|
| 798 |
+
python gradio_yolov8_det_v1.py -mdn 100
|
| 799 |
+
# 设置滑块步长,默认为0.05
|
| 800 |
+
python gradio_yolov8_det_v1.py -ss 0.01
|
| 801 |
+
```
|
| 802 |
+
"""
|
| 803 |
+
)
|
| 804 |
|
| 805 |
det_btn_img.click(
|
| 806 |
fn=yolo_det_img,
|
|
|
|
| 812 |
input_conf,
|
| 813 |
inputs_iou,
|
| 814 |
max_det,
|
| 815 |
+
inputs_cls_name,
|
| 816 |
obj_size,
|
| 817 |
],
|
| 818 |
outputs=[
|
|
|
|
| 825 |
|
| 826 |
det_btn_img_cls.click(
|
| 827 |
fn=yolo_cls_img,
|
| 828 |
+
inputs=[inputs_img_cls, device_opt_cls, inputs_model_cls],
|
| 829 |
outputs=[outputs_img_cls, outputs_ratio_cls],
|
| 830 |
)
|
| 831 |
|
|
|
|
| 843 |
favicon_path="./icon/logo.ico", # 网页图标
|
| 844 |
show_error=True, # 在浏览器控制台中显示错误信息
|
| 845 |
quiet=True, # 禁止大多数打印语句
|
| 846 |
+
)
|