Spaces:
Running
Running
Zengyf-CVer
commited on
Commit
•
652d352
1
Parent(s):
38921d8
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
-
# Gradio YOLOv8 Det v1.1.
|
2 |
# 创建人:曾逸夫
|
3 |
-
# 创建时间:2023-11-
|
4 |
-
# pip install gradio>=4.1.
|
5 |
|
6 |
import argparse
|
7 |
import csv
|
@@ -34,7 +34,7 @@ from util.fonts_opt import is_fonts
|
|
34 |
ROOT_PATH = sys.path[0] # 根目录
|
35 |
|
36 |
# Gradio YOLOv8 Det版本
|
37 |
-
GYD_VERSION = "Gradio YOLOv8 Det v1.1.
|
38 |
|
39 |
# 文件后缀
|
40 |
suffix_list = [".csv", ".yaml"]
|
@@ -45,9 +45,36 @@ FONTSIZE = 25
|
|
45 |
# 目标尺寸
|
46 |
obj_style = ["小目标", "中目标", "大目标"]
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
def parse_args(known=False):
|
50 |
-
parser = argparse.ArgumentParser(description=
|
51 |
parser.add_argument("--model_name", "-mn", default="yolov8s", type=str, help="model name")
|
52 |
parser.add_argument(
|
53 |
"--model_cfg",
|
@@ -63,6 +90,13 @@ def parse_args(known=False):
|
|
63 |
type=str,
|
64 |
help="cls name",
|
65 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
parser.add_argument(
|
67 |
"--nms_conf",
|
68 |
"-conf",
|
@@ -400,7 +434,8 @@ def yolo_cls_img(img_path, model_name):
|
|
400 |
|
401 |
clas_name_list = []
|
402 |
for i in clas_index_list:
|
403 |
-
clas_name_list.append(predict_results.names[i])
|
|
|
404 |
|
405 |
clsRatio_dict = {}
|
406 |
index_cls = 0
|
@@ -415,13 +450,14 @@ def yolo_cls_img(img_path, model_name):
|
|
415 |
def main(args):
|
416 |
gr.close_all()
|
417 |
|
418 |
-
global model_cls_name_cp, cls_name
|
419 |
|
420 |
nms_conf = args.nms_conf
|
421 |
nms_iou = args.nms_iou
|
422 |
model_name = args.model_name
|
423 |
model_cfg = args.model_cfg
|
424 |
cls_name = args.cls_name
|
|
|
425 |
inference_size = args.inference_size
|
426 |
max_detnum = args.max_detnum
|
427 |
slider_step = args.slider_step
|
@@ -430,18 +466,24 @@ def main(args):
|
|
430 |
|
431 |
model_names = yaml_csv(model_cfg, "model_names") # 模型名称
|
432 |
model_cls_name = yaml_csv(cls_name, "model_cls_name") # 类别名称
|
|
|
433 |
|
434 |
model_cls_name_cp = model_cls_name.copy() # 类别名称
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
435 |
|
436 |
# ------------ Gradio Blocks ------------
|
437 |
-
with gr.Blocks() as gyd:
|
438 |
with gr.Row():
|
439 |
-
gr.Markdown(
|
440 |
-
<img src='https://pycver.gitee.io/ows-pics/imgs/gradio_yolov8_det_logo.png' alt='Simple Icons' ></a>\
|
441 |
-
<p align='center'>基于 Gradio 的 YOLOv8 通用计算机视觉演示系统</p><p align='center'>集成目标检测、图像分割和图像分类于一体,可自定义检测模型</p>"
|
442 |
-
)
|
443 |
with gr.Row():
|
444 |
-
gr.Markdown(
|
445 |
with gr.Row():
|
446 |
with gr.Column(scale=1):
|
447 |
with gr.Tabs():
|
@@ -463,6 +505,12 @@ def main(args):
|
|
463 |
with gr.Row():
|
464 |
gr.ClearButton(inputs_img, value="清除")
|
465 |
det_btn_img = gr.Button(value='检测', variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
466 |
|
467 |
with gr.TabItem("图像分类"):
|
468 |
with gr.Row():
|
@@ -472,6 +520,12 @@ def main(args):
|
|
472 |
with gr.Row():
|
473 |
gr.ClearButton(inputs_img, value="清除")
|
474 |
det_btn_img_cls = gr.Button(value='检测', variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
475 |
|
476 |
with gr.Column(scale=1):
|
477 |
with gr.Tabs():
|
@@ -490,20 +544,6 @@ def main(args):
|
|
490 |
outputs_ratio_cls = gr.Label(label="图像分类结果")
|
491 |
|
492 |
|
493 |
-
with gr.Row():
|
494 |
-
example_list = [
|
495 |
-
["./img_examples/bus.jpg", "yolov8s", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
496 |
-
["./img_examples/giraffe.jpg", "yolov8l", "cpu", 320, 0.5, 0.45, 100, "所有尺寸"],
|
497 |
-
["./img_examples/zidane.jpg", "yolov8m", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
498 |
-
["./img_examples/Millenial-at-work.jpg", "yolov8x", "cpu", 1280, 0.5, 0.5, 100, "所有尺寸"],
|
499 |
-
["./img_examples/bus.jpg", "yolov8s-seg", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
500 |
-
["./img_examples/Millenial-at-work.jpg", "yolov8x-seg", "cpu", 1280, 0.5, 0.5, 100, "所有尺寸"],]
|
501 |
-
gr.Examples(example_list,
|
502 |
-
[inputs_img, inputs_model, device_opt, inputs_size, input_conf, inputs_iou, max_det, obj_size],
|
503 |
-
[outputs_img, outputs_objSize, outputs_clsSize],
|
504 |
-
yolo_det_img,
|
505 |
-
cache_examples=False)
|
506 |
-
|
507 |
det_btn_img.click(fn=yolo_det_img,
|
508 |
inputs=[
|
509 |
inputs_img, inputs_model, device_opt, inputs_size, input_conf, inputs_iou, max_det,
|
|
|
1 |
+
# Gradio YOLOv8 Det v1.1.1
|
2 |
# 创建人:曾逸夫
|
3 |
+
# 创建时间:2023-11-10
|
4 |
+
# pip install gradio>=4.1.2
|
5 |
|
6 |
import argparse
|
7 |
import csv
|
|
|
34 |
ROOT_PATH = sys.path[0] # 根目录
|
35 |
|
36 |
# Gradio YOLOv8 Det版本
|
37 |
+
GYD_VERSION = "Gradio YOLOv8 Det v1.1.1"
|
38 |
|
39 |
# 文件后缀
|
40 |
suffix_list = [".csv", ".yaml"]
|
|
|
45 |
# 目标尺寸
|
46 |
obj_style = ["小目标", "中目标", "大目标"]
|
47 |
|
48 |
+
GYD_TITLE = """
|
49 |
+
<p align='center'><a href='https://gitee.com/CV_Lab/gradio-yolov8-det'>
|
50 |
+
<img src='https://pycver.gitee.io/ows-pics/imgs/gradio_yolov8_det_logo.png' alt='Simple Icons' ></a>
|
51 |
+
<p align='center'>基于 Gradio 的 YOLOv8 通用计算机视觉演示系统</p><p align='center'>集成目标检测、图像分割和图像分类于一体,可自定义检测模型</p>
|
52 |
+
</p>
|
53 |
+
"""
|
54 |
+
|
55 |
+
GYD_SUB_TITLE = """
|
56 |
+
作者:曾逸夫,Gitee:https://gitee.com/PyCVer ,Github:https://github.com/Zengyf-CVer
|
57 |
+
"""
|
58 |
+
|
59 |
+
EXAMPLES_DET = [
|
60 |
+
["./img_examples/bus.jpg", "yolov8s", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
61 |
+
["./img_examples/giraffe.jpg", "yolov8l", "cpu", 320, 0.5, 0.45, 100, "所有尺寸"],
|
62 |
+
["./img_examples/zidane.jpg", "yolov8m", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
63 |
+
["./img_examples/Millenial-at-work.jpg", "yolov8x", "cpu", 1280, 0.5, 0.5, 100, "所有尺寸"],
|
64 |
+
["./img_examples/bus.jpg", "yolov8s-seg", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
65 |
+
["./img_examples/Millenial-at-work.jpg", "yolov8x-seg", "cpu", 1280, 0.5, 0.5, 100, "所有尺寸"],]
|
66 |
+
|
67 |
+
|
68 |
+
EXAMPLES_CLAS = [
|
69 |
+
["./img_examples/bus.jpg", "yolov8s-cls"],
|
70 |
+
["./img_examples/giraffe.jpg", "yolov8l-cls"],
|
71 |
+
["./img_examples/zidane.jpg", "yolov8m-cls"],
|
72 |
+
["./img_examples/Millenial-at-work.jpg", "yolov8m-cls"],
|
73 |
+
["./img_examples/bus.jpg", "yolov8m-cls"],
|
74 |
+
["./img_examples/Millenial-at-work.jpg", "yolov8m-cls"],]
|
75 |
|
76 |
def parse_args(known=False):
|
77 |
+
parser = argparse.ArgumentParser(description=GYD_VERSION)
|
78 |
parser.add_argument("--model_name", "-mn", default="yolov8s", type=str, help="model name")
|
79 |
parser.add_argument(
|
80 |
"--model_cfg",
|
|
|
90 |
type=str,
|
91 |
help="cls name",
|
92 |
)
|
93 |
+
parser.add_argument(
|
94 |
+
"--cls_imgnet_name",
|
95 |
+
"-cin",
|
96 |
+
default="./cls_name/cls_imgnet_name_zh.yaml",
|
97 |
+
type=str,
|
98 |
+
help="cls ImageNet name",
|
99 |
+
)
|
100 |
parser.add_argument(
|
101 |
"--nms_conf",
|
102 |
"-conf",
|
|
|
434 |
|
435 |
clas_name_list = []
|
436 |
for i in clas_index_list:
|
437 |
+
# clas_name_list.append(predict_results.names[i])
|
438 |
+
clas_name_list.append(model_cls_imagenet_name_cp[i])
|
439 |
|
440 |
clsRatio_dict = {}
|
441 |
index_cls = 0
|
|
|
450 |
def main(args):
|
451 |
gr.close_all()
|
452 |
|
453 |
+
global model_cls_name_cp, model_cls_imagenet_name_cp, cls_name
|
454 |
|
455 |
nms_conf = args.nms_conf
|
456 |
nms_iou = args.nms_iou
|
457 |
model_name = args.model_name
|
458 |
model_cfg = args.model_cfg
|
459 |
cls_name = args.cls_name
|
460 |
+
cls_imagenet_name = args.cls_imgnet_name # ImageNet类别
|
461 |
inference_size = args.inference_size
|
462 |
max_detnum = args.max_detnum
|
463 |
slider_step = args.slider_step
|
|
|
466 |
|
467 |
model_names = yaml_csv(model_cfg, "model_names") # 模型名称
|
468 |
model_cls_name = yaml_csv(cls_name, "model_cls_name") # 类别名称
|
469 |
+
model_cls_imagenet_name = yaml_csv(cls_imagenet_name, "model_cls_name") # 类别名称
|
470 |
|
471 |
model_cls_name_cp = model_cls_name.copy() # 类别名称
|
472 |
+
model_cls_imagenet_name_cp = model_cls_imagenet_name.copy() # 类别名称
|
473 |
+
|
474 |
+
custom_theme = gr.themes.Soft(primary_hue="blue").set(
|
475 |
+
button_secondary_background_fill="*neutral_100",
|
476 |
+
button_secondary_background_fill_hover="*neutral_200")
|
477 |
+
custom_css = '''#disp_image {
|
478 |
+
text-align: center; /* Horizontally center the content */
|
479 |
+
}'''
|
480 |
|
481 |
# ------------ Gradio Blocks ------------
|
482 |
+
with gr.Blocks(theme=custom_theme, css=custom_css) as gyd:
|
483 |
with gr.Row():
|
484 |
+
gr.Markdown(GYD_TITLE)
|
|
|
|
|
|
|
485 |
with gr.Row():
|
486 |
+
gr.Markdown(GYD_SUB_TITLE)
|
487 |
with gr.Row():
|
488 |
with gr.Column(scale=1):
|
489 |
with gr.Tabs():
|
|
|
505 |
with gr.Row():
|
506 |
gr.ClearButton(inputs_img, value="清除")
|
507 |
det_btn_img = gr.Button(value='检测', variant="primary")
|
508 |
+
with gr.Row():
|
509 |
+
gr.Examples(examples=EXAMPLES_DET,
|
510 |
+
fn=yolo_det_img,
|
511 |
+
inputs=[inputs_img, inputs_model, device_opt, inputs_size, input_conf, inputs_iou, max_det, obj_size],
|
512 |
+
# outputs=[outputs_img, outputs_objSize, outputs_clsSize],
|
513 |
+
cache_examples=False)
|
514 |
|
515 |
with gr.TabItem("图像分类"):
|
516 |
with gr.Row():
|
|
|
520 |
with gr.Row():
|
521 |
gr.ClearButton(inputs_img, value="清除")
|
522 |
det_btn_img_cls = gr.Button(value='检测', variant="primary")
|
523 |
+
with gr.Row():
|
524 |
+
gr.Examples(examples=EXAMPLES_CLAS,
|
525 |
+
fn=yolo_cls_img,
|
526 |
+
inputs=[inputs_img_cls, inputs_model_cls],
|
527 |
+
# outputs=[outputs_img_cls, outputs_ratio_cls],
|
528 |
+
cache_examples=False)
|
529 |
|
530 |
with gr.Column(scale=1):
|
531 |
with gr.Tabs():
|
|
|
544 |
outputs_ratio_cls = gr.Label(label="图像分类结果")
|
545 |
|
546 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
547 |
det_btn_img.click(fn=yolo_det_img,
|
548 |
inputs=[
|
549 |
inputs_img, inputs_model, device_opt, inputs_size, input_conf, inputs_iou, max_det,
|