Spaces:
Running
Running
Zengyf-CVer
commited on
Commit
·
e1cb70c
1
Parent(s):
8ada2c0
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
-
# Gradio YOLOv8 Det v1.
|
2 |
# 创建人:曾逸夫
|
3 |
# 创建时间:2023-11-10
|
4 |
-
# pip install gradio>=4.
|
5 |
|
6 |
import argparse
|
7 |
import csv
|
@@ -31,10 +31,8 @@ from PIL import Image, ImageDraw, ImageFont
|
|
31 |
|
32 |
from util.fonts_opt import is_fonts
|
33 |
|
34 |
-
ROOT_PATH = sys.path[0] # 根目录
|
35 |
-
|
36 |
# Gradio YOLOv8 Det版本
|
37 |
-
GYD_VERSION = "Gradio YOLOv8 Det v1.
|
38 |
|
39 |
# 文件后缀
|
40 |
suffix_list = [".csv", ".yaml"]
|
@@ -50,6 +48,9 @@ GYD_TITLE = """
|
|
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 = """
|
@@ -57,21 +58,25 @@ GYD_SUB_TITLE = """
|
|
57 |
"""
|
58 |
|
59 |
EXAMPLES_DET = [
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
|
68 |
EXAMPLES_CLAS = [
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
def parse_args(known=False):
|
77 |
parser = argparse.ArgumentParser(description=GYD_VERSION)
|
@@ -93,7 +98,7 @@ def parse_args(known=False):
|
|
93 |
parser.add_argument(
|
94 |
"--cls_imgnet_name",
|
95 |
"-cin",
|
96 |
-
default="./cls_name/
|
97 |
type=str,
|
98 |
help="cls ImageNet name",
|
99 |
)
|
@@ -261,7 +266,7 @@ def seg_output(img_path, seg_mask_list, color_list, cls_list):
|
|
261 |
|
262 |
|
263 |
# 目标检测和图像分割模型加载
|
264 |
-
def
|
265 |
model = YOLO(yolo_model)
|
266 |
|
267 |
results = model(source=img_path, device=device_opt, imgsz=infer_size, conf=conf, iou=iou, max_det=max_det)
|
@@ -293,7 +298,13 @@ def yolo_det_img(img_path, model_name, device_opt, infer_size, conf, iou, max_de
|
|
293 |
cls_index_det_stat = [] # 1
|
294 |
|
295 |
# 模型加载
|
296 |
-
predict_results =
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
# 检测参数
|
298 |
xyxy_list = predict_results.boxes.xyxy.cpu().numpy().tolist()
|
299 |
conf_list = predict_results.boxes.conf.cpu().numpy().tolist()
|
@@ -341,7 +352,7 @@ def yolo_det_img(img_path, model_name, device_opt, infer_size, conf, iou, max_de
|
|
341 |
h_obj = y1 - y0
|
342 |
area_obj = w_obj * h_obj # 目标尺寸
|
343 |
|
344 |
-
if (obj_size ==
|
345 |
obj_cls_index = int(cls_list[i]) # 类别索引
|
346 |
cls_index_det_stat.append(obj_cls_index)
|
347 |
|
@@ -354,7 +365,7 @@ def yolo_det_img(img_path, model_name, device_opt, infer_size, conf, iou, max_de
|
|
354 |
score_det_stat.append(conf)
|
355 |
|
356 |
area_obj_all.append(area_obj)
|
357 |
-
elif (obj_size ==
|
358 |
obj_cls_index = int(cls_list[i]) # 类别索引
|
359 |
cls_index_det_stat.append(obj_cls_index)
|
360 |
|
@@ -367,7 +378,7 @@ def yolo_det_img(img_path, model_name, device_opt, infer_size, conf, iou, max_de
|
|
367 |
score_det_stat.append(conf)
|
368 |
|
369 |
area_obj_all.append(area_obj)
|
370 |
-
elif (obj_size ==
|
371 |
obj_cls_index = int(cls_list[i]) # 类别索引
|
372 |
cls_index_det_stat.append(obj_cls_index)
|
373 |
|
@@ -427,7 +438,7 @@ def yolo_cls_img(img_path, model_name):
|
|
427 |
|
428 |
# 模型加载
|
429 |
predict_results = model_cls_loading(img_path, yolo_model=f"{model_name}.pt")
|
430 |
-
|
431 |
det_img = Image.open(img_path)
|
432 |
clas_ratio_list = predict_results.probs.top5conf.tolist()
|
433 |
clas_index_list = predict_results.probs.top5
|
@@ -442,7 +453,7 @@ def yolo_cls_img(img_path, model_name):
|
|
442 |
clsDet_dict = Counter(clas_name_list)
|
443 |
for k, v in clsDet_dict.items():
|
444 |
clsRatio_dict[k] = clas_ratio_list[index_cls]
|
445 |
-
index_cls+=1
|
446 |
|
447 |
return det_img, clsRatio_dict
|
448 |
|
@@ -471,12 +482,10 @@ def main(args):
|
|
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 |
-
|
476 |
-
|
477 |
-
custom_css =
|
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:
|
@@ -506,26 +515,34 @@ def main(args):
|
|
506 |
gr.ClearButton(inputs_img, value="清除")
|
507 |
det_btn_img = gr.Button(value='检测', variant="primary")
|
508 |
with gr.Row():
|
509 |
-
gr.Examples(
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
|
|
|
|
|
|
515 |
with gr.TabItem("图像分类"):
|
516 |
with gr.Row():
|
517 |
inputs_img_cls = gr.Image(image_mode="RGB", type="filepath", label="原始图片")
|
518 |
with gr.Row():
|
519 |
-
inputs_model_cls = gr.Dropdown(choices=[
|
|
|
|
|
|
|
|
|
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(
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
|
|
529 |
|
530 |
with gr.Column(scale=1):
|
531 |
with gr.Tabs():
|
@@ -543,7 +560,6 @@ def main(args):
|
|
543 |
with gr.Row():
|
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,
|
@@ -551,9 +567,8 @@ def main(args):
|
|
551 |
outputs=[outputs_img, outputs_objSize, outputs_clsSize])
|
552 |
|
553 |
det_btn_img_cls.click(fn=yolo_cls_img,
|
554 |
-
|
555 |
-
|
556 |
-
outputs=[outputs_img_cls, outputs_ratio_cls])
|
557 |
|
558 |
return gyd
|
559 |
|
|
|
1 |
+
# Gradio YOLOv8 Det v1.2.0
|
2 |
# 创建人:曾逸夫
|
3 |
# 创建时间:2023-11-10
|
4 |
+
# pip install gradio>=4.3.0
|
5 |
|
6 |
import argparse
|
7 |
import csv
|
|
|
31 |
|
32 |
from util.fonts_opt import is_fonts
|
33 |
|
|
|
|
|
34 |
# Gradio YOLOv8 Det版本
|
35 |
+
GYD_VERSION = "Gradio YOLOv8 Det v1.2.0"
|
36 |
|
37 |
# 文件后缀
|
38 |
suffix_list = [".csv", ".yaml"]
|
|
|
48 |
<img src='https://pycver.gitee.io/ows-pics/imgs/gradio_yolov8_det_logo.png' alt='Simple Icons' ></a>
|
49 |
<p align='center'>基于 Gradio 的 YOLOv8 通用计算机视觉演示系统</p><p align='center'>集成目标检测、图像分割和图像分类于一体,可自定义检测模型</p>
|
50 |
</p>
|
51 |
+
<p align='center'>
|
52 |
+
<a href='https://gitee.com/CV_Lab/gradio-yolov8-det'><img src='https://gitee.com/CV_Lab/gradio-yolov8-det/widgets/widget_6.svg' alt='Fork me on Gitee'></img></a>
|
53 |
+
</p>
|
54 |
"""
|
55 |
|
56 |
GYD_SUB_TITLE = """
|
|
|
58 |
"""
|
59 |
|
60 |
EXAMPLES_DET = [
|
61 |
+
["./img_examples/bus.jpg", "yolov8s", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
62 |
+
["./img_examples/giraffe.jpg", "yolov8l", "cpu", 320, 0.5, 0.45, 100, "所有尺寸"],
|
63 |
+
["./img_examples/zidane.jpg", "yolov8m", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
64 |
+
["./img_examples/Millenial-at-work.jpg", "yolov8x", "cpu", 1280, 0.5, 0.5, 100, "所有尺寸"],
|
65 |
+
["./img_examples/bus.jpg", "yolov8s-seg", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
66 |
+
["./img_examples/Millenial-at-work.jpg", "yolov8x-seg", "cpu", 1280, 0.5, 0.5, 100, "所有尺寸"],]
|
|
|
67 |
|
68 |
EXAMPLES_CLAS = [
|
69 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000008.JPEG", "yolov8s-cls"],
|
70 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000018.JPEG", "yolov8l-cls"],
|
71 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000023.JPEG", "yolov8m-cls"],
|
72 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000067.JPEG", "yolov8m-cls"],
|
73 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000077.JPEG", "yolov8m-cls"],
|
74 |
+
["./img_examples/img_clas/ILSVRC2012_val_00000247.JPEG", "yolov8m-cls"],]
|
75 |
+
|
76 |
+
GYD_CSS = """#disp_image {
|
77 |
+
text-align: center; /* Horizontally center the content */
|
78 |
+
}"""
|
79 |
+
|
80 |
|
81 |
def parse_args(known=False):
|
82 |
parser = argparse.ArgumentParser(description=GYD_VERSION)
|
|
|
98 |
parser.add_argument(
|
99 |
"--cls_imgnet_name",
|
100 |
"-cin",
|
101 |
+
default="./cls_name/cls_imagenet_name_zh.yaml",
|
102 |
type=str,
|
103 |
help="cls ImageNet name",
|
104 |
)
|
|
|
266 |
|
267 |
|
268 |
# 目标检测和图像分割模型加载
|
269 |
+
def model_det_loading(img_path, device_opt, conf, iou, infer_size, max_det, yolo_model="yolov8n.pt"):
|
270 |
model = YOLO(yolo_model)
|
271 |
|
272 |
results = model(source=img_path, device=device_opt, imgsz=infer_size, conf=conf, iou=iou, max_det=max_det)
|
|
|
298 |
cls_index_det_stat = [] # 1
|
299 |
|
300 |
# 模型加载
|
301 |
+
predict_results = model_det_loading(img_path,
|
302 |
+
device_opt,
|
303 |
+
conf,
|
304 |
+
iou,
|
305 |
+
infer_size,
|
306 |
+
max_det,
|
307 |
+
yolo_model=f"{model_name}.pt")
|
308 |
# 检测参数
|
309 |
xyxy_list = predict_results.boxes.xyxy.cpu().numpy().tolist()
|
310 |
conf_list = predict_results.boxes.conf.cpu().numpy().tolist()
|
|
|
352 |
h_obj = y1 - y0
|
353 |
area_obj = w_obj * h_obj # 目标尺寸
|
354 |
|
355 |
+
if (obj_size == obj_style[0] and area_obj > 0 and area_obj <= 32 ** 2):
|
356 |
obj_cls_index = int(cls_list[i]) # 类别索引
|
357 |
cls_index_det_stat.append(obj_cls_index)
|
358 |
|
|
|
365 |
score_det_stat.append(conf)
|
366 |
|
367 |
area_obj_all.append(area_obj)
|
368 |
+
elif (obj_size == obj_style[1] and area_obj > 32 ** 2 and area_obj <= 96 ** 2):
|
369 |
obj_cls_index = int(cls_list[i]) # 类别索引
|
370 |
cls_index_det_stat.append(obj_cls_index)
|
371 |
|
|
|
378 |
score_det_stat.append(conf)
|
379 |
|
380 |
area_obj_all.append(area_obj)
|
381 |
+
elif (obj_size == obj_style[2] and area_obj > 96 ** 2):
|
382 |
obj_cls_index = int(cls_list[i]) # 类别索引
|
383 |
cls_index_det_stat.append(obj_cls_index)
|
384 |
|
|
|
438 |
|
439 |
# 模型加载
|
440 |
predict_results = model_cls_loading(img_path, yolo_model=f"{model_name}.pt")
|
441 |
+
|
442 |
det_img = Image.open(img_path)
|
443 |
clas_ratio_list = predict_results.probs.top5conf.tolist()
|
444 |
clas_index_list = predict_results.probs.top5
|
|
|
453 |
clsDet_dict = Counter(clas_name_list)
|
454 |
for k, v in clsDet_dict.items():
|
455 |
clsRatio_dict[k] = clas_ratio_list[index_cls]
|
456 |
+
index_cls += 1
|
457 |
|
458 |
return det_img, clsRatio_dict
|
459 |
|
|
|
482 |
model_cls_name_cp = model_cls_name.copy() # 类别名称
|
483 |
model_cls_imagenet_name_cp = model_cls_imagenet_name.copy() # 类别名称
|
484 |
|
485 |
+
custom_theme = gr.themes.Soft(primary_hue="blue").set(button_secondary_background_fill="*neutral_100",
|
486 |
+
button_secondary_background_fill_hover="*neutral_200")
|
487 |
+
|
488 |
+
custom_css = GYD_CSS
|
|
|
|
|
489 |
|
490 |
# ------------ Gradio Blocks ------------
|
491 |
with gr.Blocks(theme=custom_theme, css=custom_css) as gyd:
|
|
|
515 |
gr.ClearButton(inputs_img, value="清除")
|
516 |
det_btn_img = gr.Button(value='检测', variant="primary")
|
517 |
with gr.Row():
|
518 |
+
gr.Examples(
|
519 |
+
examples=EXAMPLES_DET,
|
520 |
+
fn=yolo_det_img,
|
521 |
+
inputs=[
|
522 |
+
inputs_img, inputs_model, device_opt, inputs_size, input_conf, inputs_iou, max_det,
|
523 |
+
obj_size],
|
524 |
+
# outputs=[outputs_img, outputs_objSize, outputs_clsSize],
|
525 |
+
cache_examples=False)
|
526 |
+
|
527 |
with gr.TabItem("图像分类"):
|
528 |
with gr.Row():
|
529 |
inputs_img_cls = gr.Image(image_mode="RGB", type="filepath", label="原始图片")
|
530 |
with gr.Row():
|
531 |
+
inputs_model_cls = gr.Dropdown(choices=[
|
532 |
+
"yolov8n-cls", "yolov8s-cls", "yolov8l-cls", "yolov8m-cls", "yolov8x-cls"],
|
533 |
+
value="yolov8s-cls",
|
534 |
+
type="value",
|
535 |
+
label="模型")
|
536 |
with gr.Row():
|
537 |
gr.ClearButton(inputs_img, value="清除")
|
538 |
det_btn_img_cls = gr.Button(value='检测', variant="primary")
|
539 |
with gr.Row():
|
540 |
+
gr.Examples(
|
541 |
+
examples=EXAMPLES_CLAS,
|
542 |
+
fn=yolo_cls_img,
|
543 |
+
inputs=[inputs_img_cls, inputs_model_cls],
|
544 |
+
# outputs=[outputs_img_cls, outputs_ratio_cls],
|
545 |
+
cache_examples=False)
|
546 |
|
547 |
with gr.Column(scale=1):
|
548 |
with gr.Tabs():
|
|
|
560 |
with gr.Row():
|
561 |
outputs_ratio_cls = gr.Label(label="图像分类结果")
|
562 |
|
|
|
563 |
det_btn_img.click(fn=yolo_det_img,
|
564 |
inputs=[
|
565 |
inputs_img, inputs_model, device_opt, inputs_size, input_conf, inputs_iou, max_det,
|
|
|
567 |
outputs=[outputs_img, outputs_objSize, outputs_clsSize])
|
568 |
|
569 |
det_btn_img_cls.click(fn=yolo_cls_img,
|
570 |
+
inputs=[inputs_img_cls, inputs_model_cls],
|
571 |
+
outputs=[outputs_img_cls, outputs_ratio_cls])
|
|
|
572 |
|
573 |
return gyd
|
574 |
|