Spaces:
Running
Running
Zengyf-CVer
commited on
Commit
·
b2eff28
1
Parent(s):
6117c49
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,21 @@
|
|
1 |
-
# Gradio YOLOv8 Det
|
2 |
# 创建人:曾逸夫
|
3 |
-
# 创建时间:2023-10-
|
|
|
4 |
|
5 |
import argparse
|
6 |
import csv
|
7 |
-
import gc
|
8 |
-
import json
|
9 |
-
import os
|
10 |
import random
|
11 |
-
import shutil
|
12 |
import sys
|
13 |
from collections import Counter
|
14 |
from pathlib import Path
|
15 |
|
16 |
import cv2
|
17 |
import gradio as gr
|
18 |
-
import matplotlib.pyplot as plt
|
19 |
import numpy as np
|
20 |
-
import pandas as pd
|
21 |
-
import plotly.express as px
|
22 |
from matplotlib import font_manager
|
23 |
from ultralytics import YOLO
|
24 |
|
25 |
-
# pip_source = "https://pypi.tuna.tsinghua.edu.cn/simple some-package"
|
26 |
-
# os.system(f"pip install gradio --upgrade -i {pip_source}")
|
27 |
-
# os.system(f"pip install ultralytics --upgrade -i {pip_source}")
|
28 |
-
|
29 |
ROOT_PATH = sys.path[0] # 项目根目录
|
30 |
|
31 |
# --------------------- 字体库 ---------------------
|
@@ -36,7 +26,6 @@ SimSun = font_manager.FontProperties(fname=SimSun_path, size=12)
|
|
36 |
# 新罗马字体
|
37 |
TimesNesRoman = font_manager.FontProperties(fname=TimesNesRoman_path, size=12)
|
38 |
|
39 |
-
import torch
|
40 |
import yaml
|
41 |
from PIL import Image, ImageDraw, ImageFont
|
42 |
|
@@ -45,7 +34,7 @@ from util.fonts_opt import is_fonts
|
|
45 |
ROOT_PATH = sys.path[0] # 根目录
|
46 |
|
47 |
# Gradio YOLOv8 Det版本
|
48 |
-
GYD_VERSION = "Gradio YOLOv8 Det
|
49 |
|
50 |
# 文件后缀
|
51 |
suffix_list = [".csv", ".yaml"]
|
@@ -58,11 +47,9 @@ obj_style = ["小目标", "中目标", "大目标"]
|
|
58 |
|
59 |
|
60 |
def parse_args(known=False):
|
61 |
-
parser = argparse.ArgumentParser(description="Gradio YOLOv8 Det
|
62 |
parser.add_argument("--model_type", "-mt", default="online", type=str, help="model type")
|
63 |
parser.add_argument("--source", "-src", default="upload", type=str, help="image input source")
|
64 |
-
parser.add_argument("--source_video", "-src_v", default="upload", type=str, help="video input source")
|
65 |
-
parser.add_argument("--img_tool", "-it", default="editor", type=str, help="input image tool")
|
66 |
parser.add_argument("--model_name", "-mn", default="yolov8s", type=str, help="model name")
|
67 |
parser.add_argument(
|
68 |
"--model_cfg",
|
@@ -86,13 +73,6 @@ def parse_args(known=False):
|
|
86 |
help="model NMS confidence threshold",
|
87 |
)
|
88 |
parser.add_argument("--nms_iou", "-iou", default=0.45, type=float, help="model NMS IoU threshold")
|
89 |
-
parser.add_argument(
|
90 |
-
"--device",
|
91 |
-
"-dev",
|
92 |
-
default="cuda:0",
|
93 |
-
type=str,
|
94 |
-
help="cuda or cpu",
|
95 |
-
)
|
96 |
parser.add_argument("--inference_size", "-isz", default=640, type=int, help="model inference size")
|
97 |
parser.add_argument("--max_detnum", "-mdn", default=50, type=float, help="model max det num")
|
98 |
parser.add_argument("--slider_step", "-ss", default=0.05, type=float, help="slider step")
|
@@ -155,21 +135,11 @@ def check_online():
|
|
155 |
return False
|
156 |
|
157 |
|
158 |
-
# 模型加载
|
159 |
-
def model_loading(img_path, conf, iou, infer_size, yolo_model="yolov8n.pt"):
|
160 |
-
model = YOLO(yolo_model)
|
161 |
-
|
162 |
-
results = model(source=img_path, imgsz=infer_size, conf=conf, iou=iou)
|
163 |
-
results = list(results)[0]
|
164 |
-
return results
|
165 |
-
|
166 |
-
|
167 |
# 标签和边界框颜色设置
|
168 |
def color_set(cls_num):
|
169 |
color_list = []
|
170 |
for i in range(cls_num):
|
171 |
color = tuple(np.random.choice(range(256), size=3))
|
172 |
-
# color = ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])]
|
173 |
color_list.append(color)
|
174 |
|
175 |
return color_list
|
@@ -195,6 +165,7 @@ def pil_draw(img, score_l, bbox_l, cls_l, cls_index_l, textFont, color_list):
|
|
195 |
id = 0
|
196 |
|
197 |
for score, (xmin, ymin, xmax, ymax), label, cls_index in zip(score_l, bbox_l, cls_l, cls_index_l):
|
|
|
198 |
img_pil.rectangle([xmin, ymin, xmax, ymax], fill=None, outline=color_list[cls_index], width=2) # 边界框
|
199 |
countdown_msg = f"{id}-{label} {score:.2f}"
|
200 |
# text_w, text_h = textFont.getsize(countdown_msg) # 标签尺寸 pillow 9.5.0
|
@@ -257,8 +228,17 @@ def seg_output(img_path, seg_mask_list, color_list, cls_list):
|
|
257 |
return img_mask_merge
|
258 |
|
259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
# YOLOv8图片检测函数
|
261 |
-
def yolo_det_img(img_path, model_name, infer_size, conf, iou):
|
262 |
|
263 |
global model, model_name_tmp, device_tmp
|
264 |
|
@@ -269,10 +249,10 @@ def yolo_det_img(img_path, model_name, infer_size, conf, iou):
|
|
269 |
score_det_stat = [] # 置信度统计
|
270 |
bbox_det_stat = [] # 边界框统计
|
271 |
cls_det_stat = [] # 类别数量统计
|
272 |
-
cls_index_det_stat = [] #
|
273 |
|
274 |
# 模型加载
|
275 |
-
predict_results = model_loading(img_path, conf, iou, infer_size, yolo_model=f"{model_name}.pt")
|
276 |
# 检测参数
|
277 |
xyxy_list = predict_results.boxes.xyxy.cpu().numpy().tolist()
|
278 |
conf_list = predict_results.boxes.conf.cpu().numpy().tolist()
|
@@ -308,11 +288,6 @@ def yolo_det_img(img_path, model_name, infer_size, conf, iou):
|
|
308 |
textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/malgun.ttf"), size=FONTSIZE)
|
309 |
|
310 |
for i in range(len(xyxy_list)):
|
311 |
-
obj_cls_index = int(cls_list[i]) # 类别索引
|
312 |
-
cls_index_det_stat.append(obj_cls_index)
|
313 |
-
|
314 |
-
obj_cls = model_cls_name_cp[obj_cls_index] # 类别
|
315 |
-
cls_det_stat.append(obj_cls)
|
316 |
|
317 |
# ------------ 边框坐标 ------------
|
318 |
x0 = int(xyxy_list[i][0])
|
@@ -320,16 +295,63 @@ def yolo_det_img(img_path, model_name, infer_size, conf, iou):
|
|
320 |
x1 = int(xyxy_list[i][2])
|
321 |
y1 = int(xyxy_list[i][3])
|
322 |
|
323 |
-
bbox_det_stat.append((x0, y0, x1, y1))
|
324 |
-
|
325 |
-
conf = float(conf_list[i]) # 置信度
|
326 |
-
score_det_stat.append(conf)
|
327 |
-
|
328 |
# ---------- 加入目标尺寸 ----------
|
329 |
w_obj = x1 - x0
|
330 |
h_obj = y1 - y0
|
331 |
-
area_obj = w_obj * h_obj
|
332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
|
334 |
det_img = pil_draw(img, score_det_stat, bbox_det_stat, cls_det_stat, cls_index_det_stat, textFont, color_list)
|
335 |
|
@@ -353,10 +375,10 @@ def yolo_det_img(img_path, model_name, infer_size, conf, iou):
|
|
353 |
for k, v in clsDet_dict.items():
|
354 |
clsRatio_dict[k] = v / clsDet_dict_sum
|
355 |
|
|
|
356 |
return det_img, objSize_dict, clsRatio_dict
|
357 |
else:
|
358 |
-
|
359 |
-
return None, None, None
|
360 |
|
361 |
|
362 |
def main(args):
|
@@ -365,13 +387,13 @@ def main(args):
|
|
365 |
global model_cls_name_cp, cls_name
|
366 |
|
367 |
source = args.source
|
368 |
-
img_tool = args.img_tool
|
369 |
nms_conf = args.nms_conf
|
370 |
nms_iou = args.nms_iou
|
371 |
model_name = args.model_name
|
372 |
model_cfg = args.model_cfg
|
373 |
cls_name = args.cls_name
|
374 |
inference_size = args.inference_size
|
|
|
375 |
slider_step = args.slider_step
|
376 |
|
377 |
is_fonts(f"{ROOT_PATH}/fonts") # 检查字体文件
|
@@ -381,100 +403,76 @@ def main(args):
|
|
381 |
|
382 |
model_cls_name_cp = model_cls_name.copy() # 类别名称
|
383 |
|
384 |
-
#
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
0.5,],
|
446 |
-
[
|
447 |
-
"./img_examples/Millenial-at-work.jpg",
|
448 |
-
"yolov8x-seg",
|
449 |
-
1280,
|
450 |
-
0.5,
|
451 |
-
0.5,],]
|
452 |
-
|
453 |
-
# 接口
|
454 |
-
gyd_img = gr.Interface(
|
455 |
-
fn=yolo_det_img,
|
456 |
-
inputs=inputs_img_list,
|
457 |
-
outputs=outputs_img_list,
|
458 |
-
title=title,
|
459 |
-
description=description,
|
460 |
-
examples=examples_imgs,
|
461 |
-
cache_examples=False,
|
462 |
-
flagging_dir="run", # 输出目录
|
463 |
-
allow_flagging="manual",
|
464 |
-
flagging_options=["good", "generally", "bad"],
|
465 |
-
)
|
466 |
-
|
467 |
-
return gyd_img
|
468 |
|
469 |
|
470 |
if __name__ == "__main__":
|
471 |
args = parse_args()
|
472 |
-
|
473 |
is_share = args.is_share
|
474 |
|
475 |
-
|
476 |
inbrowser=True, # 自动打开默认浏览器
|
477 |
-
show_tips=True, # 自动显示gradio最新功能
|
478 |
share=is_share, # 项目共享,其他设备可以访问
|
479 |
favicon_path="./icon/logo.ico", # 网页图标
|
480 |
show_error=True, # 在浏览器控制台中显示错误信息
|
|
|
1 |
+
# Gradio YOLOv8 Det v1.0.0
|
2 |
# 创建人:曾逸夫
|
3 |
+
# 创建时间:2023-10-28
|
4 |
+
# pip install gradio>=4.0.2
|
5 |
|
6 |
import argparse
|
7 |
import csv
|
|
|
|
|
|
|
8 |
import random
|
|
|
9 |
import sys
|
10 |
from collections import Counter
|
11 |
from pathlib import Path
|
12 |
|
13 |
import cv2
|
14 |
import gradio as gr
|
|
|
15 |
import numpy as np
|
|
|
|
|
16 |
from matplotlib import font_manager
|
17 |
from ultralytics import YOLO
|
18 |
|
|
|
|
|
|
|
|
|
19 |
ROOT_PATH = sys.path[0] # 项目根目录
|
20 |
|
21 |
# --------------------- 字体库 ---------------------
|
|
|
26 |
# 新罗马字体
|
27 |
TimesNesRoman = font_manager.FontProperties(fname=TimesNesRoman_path, size=12)
|
28 |
|
|
|
29 |
import yaml
|
30 |
from PIL import Image, ImageDraw, ImageFont
|
31 |
|
|
|
34 |
ROOT_PATH = sys.path[0] # 根目录
|
35 |
|
36 |
# Gradio YOLOv8 Det版本
|
37 |
+
GYD_VERSION = "Gradio YOLOv8 Det v1.0.0"
|
38 |
|
39 |
# 文件后缀
|
40 |
suffix_list = [".csv", ".yaml"]
|
|
|
47 |
|
48 |
|
49 |
def parse_args(known=False):
|
50 |
+
parser = argparse.ArgumentParser(description="Gradio YOLOv8 Det v1.0.0")
|
51 |
parser.add_argument("--model_type", "-mt", default="online", type=str, help="model type")
|
52 |
parser.add_argument("--source", "-src", default="upload", type=str, help="image input source")
|
|
|
|
|
53 |
parser.add_argument("--model_name", "-mn", default="yolov8s", type=str, help="model name")
|
54 |
parser.add_argument(
|
55 |
"--model_cfg",
|
|
|
73 |
help="model NMS confidence threshold",
|
74 |
)
|
75 |
parser.add_argument("--nms_iou", "-iou", default=0.45, type=float, help="model NMS IoU threshold")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
parser.add_argument("--inference_size", "-isz", default=640, type=int, help="model inference size")
|
77 |
parser.add_argument("--max_detnum", "-mdn", default=50, type=float, help="model max det num")
|
78 |
parser.add_argument("--slider_step", "-ss", default=0.05, type=float, help="slider step")
|
|
|
135 |
return False
|
136 |
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
# 标签和边界框颜色设置
|
139 |
def color_set(cls_num):
|
140 |
color_list = []
|
141 |
for i in range(cls_num):
|
142 |
color = tuple(np.random.choice(range(256), size=3))
|
|
|
143 |
color_list.append(color)
|
144 |
|
145 |
return color_list
|
|
|
165 |
id = 0
|
166 |
|
167 |
for score, (xmin, ymin, xmax, ymax), label, cls_index in zip(score_l, bbox_l, cls_l, cls_index_l):
|
168 |
+
|
169 |
img_pil.rectangle([xmin, ymin, xmax, ymax], fill=None, outline=color_list[cls_index], width=2) # 边界框
|
170 |
countdown_msg = f"{id}-{label} {score:.2f}"
|
171 |
# text_w, text_h = textFont.getsize(countdown_msg) # 标签尺寸 pillow 9.5.0
|
|
|
228 |
return img_mask_merge
|
229 |
|
230 |
|
231 |
+
# 模型加载
|
232 |
+
def model_loading(img_path, device_opt, conf, iou, infer_size, max_det, yolo_model="yolov8n.pt"):
|
233 |
+
model = YOLO(yolo_model)
|
234 |
+
|
235 |
+
results = model(source=img_path, device=device_opt, imgsz=infer_size, conf=conf, iou=iou, max_det=max_det)
|
236 |
+
results = list(results)[0]
|
237 |
+
return results
|
238 |
+
|
239 |
+
|
240 |
# YOLOv8图片检测函数
|
241 |
+
def yolo_det_img(img_path, model_name, device_opt, infer_size, conf, iou, max_det, obj_size):
|
242 |
|
243 |
global model, model_name_tmp, device_tmp
|
244 |
|
|
|
249 |
score_det_stat = [] # 置信度统计
|
250 |
bbox_det_stat = [] # 边界框统计
|
251 |
cls_det_stat = [] # 类别数量统计
|
252 |
+
cls_index_det_stat = [] # 1
|
253 |
|
254 |
# 模型加载
|
255 |
+
predict_results = model_loading(img_path, device_opt, conf, iou, infer_size, max_det, yolo_model=f"{model_name}.pt")
|
256 |
# 检测参数
|
257 |
xyxy_list = predict_results.boxes.xyxy.cpu().numpy().tolist()
|
258 |
conf_list = predict_results.boxes.conf.cpu().numpy().tolist()
|
|
|
288 |
textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/malgun.ttf"), size=FONTSIZE)
|
289 |
|
290 |
for i in range(len(xyxy_list)):
|
|
|
|
|
|
|
|
|
|
|
291 |
|
292 |
# ------------ 边框坐标 ------------
|
293 |
x0 = int(xyxy_list[i][0])
|
|
|
295 |
x1 = int(xyxy_list[i][2])
|
296 |
y1 = int(xyxy_list[i][3])
|
297 |
|
|
|
|
|
|
|
|
|
|
|
298 |
# ---------- 加入目标尺寸 ----------
|
299 |
w_obj = x1 - x0
|
300 |
h_obj = y1 - y0
|
301 |
+
area_obj = w_obj * h_obj # 目标尺寸
|
302 |
+
|
303 |
+
if (obj_size == "小目标" and area_obj > 0 and area_obj <= 32 ** 2):
|
304 |
+
obj_cls_index = int(cls_list[i]) # 类别索引
|
305 |
+
cls_index_det_stat.append(obj_cls_index)
|
306 |
+
|
307 |
+
obj_cls = model_cls_name_cp[obj_cls_index] # 类别
|
308 |
+
cls_det_stat.append(obj_cls)
|
309 |
+
|
310 |
+
bbox_det_stat.append((x0, y0, x1, y1))
|
311 |
+
|
312 |
+
conf = float(conf_list[i]) # 置信度
|
313 |
+
score_det_stat.append(conf)
|
314 |
+
|
315 |
+
area_obj_all.append(area_obj)
|
316 |
+
elif (obj_size == "中目标" and area_obj > 32 ** 2 and area_obj <= 96 ** 2):
|
317 |
+
obj_cls_index = int(cls_list[i]) # 类别索引
|
318 |
+
cls_index_det_stat.append(obj_cls_index)
|
319 |
+
|
320 |
+
obj_cls = model_cls_name_cp[obj_cls_index] # 类别
|
321 |
+
cls_det_stat.append(obj_cls)
|
322 |
+
|
323 |
+
bbox_det_stat.append((x0, y0, x1, y1))
|
324 |
+
|
325 |
+
conf = float(conf_list[i]) # 置信度
|
326 |
+
score_det_stat.append(conf)
|
327 |
+
|
328 |
+
area_obj_all.append(area_obj)
|
329 |
+
elif (obj_size == "大目标" and area_obj > 96 ** 2):
|
330 |
+
obj_cls_index = int(cls_list[i]) # 类别索引
|
331 |
+
cls_index_det_stat.append(obj_cls_index)
|
332 |
+
|
333 |
+
obj_cls = model_cls_name_cp[obj_cls_index] # 类别
|
334 |
+
cls_det_stat.append(obj_cls)
|
335 |
+
|
336 |
+
bbox_det_stat.append((x0, y0, x1, y1))
|
337 |
+
|
338 |
+
conf = float(conf_list[i]) # 置信度
|
339 |
+
score_det_stat.append(conf)
|
340 |
+
|
341 |
+
area_obj_all.append(area_obj)
|
342 |
+
elif (obj_size == "所有尺寸"):
|
343 |
+
obj_cls_index = int(cls_list[i]) # 类别索引
|
344 |
+
cls_index_det_stat.append(obj_cls_index)
|
345 |
+
|
346 |
+
obj_cls = model_cls_name_cp[obj_cls_index] # 类别
|
347 |
+
cls_det_stat.append(obj_cls)
|
348 |
+
|
349 |
+
bbox_det_stat.append((x0, y0, x1, y1))
|
350 |
+
|
351 |
+
conf = float(conf_list[i]) # 置信度
|
352 |
+
score_det_stat.append(conf)
|
353 |
+
|
354 |
+
area_obj_all.append(area_obj)
|
355 |
|
356 |
det_img = pil_draw(img, score_det_stat, bbox_det_stat, cls_det_stat, cls_index_det_stat, textFont, color_list)
|
357 |
|
|
|
375 |
for k, v in clsDet_dict.items():
|
376 |
clsRatio_dict[k] = v / clsDet_dict_sum
|
377 |
|
378 |
+
gr.Info("图片检测成功!")
|
379 |
return det_img, objSize_dict, clsRatio_dict
|
380 |
else:
|
381 |
+
raise gr.Error("图片检测失败!")
|
|
|
382 |
|
383 |
|
384 |
def main(args):
|
|
|
387 |
global model_cls_name_cp, cls_name
|
388 |
|
389 |
source = args.source
|
|
|
390 |
nms_conf = args.nms_conf
|
391 |
nms_iou = args.nms_iou
|
392 |
model_name = args.model_name
|
393 |
model_cfg = args.model_cfg
|
394 |
cls_name = args.cls_name
|
395 |
inference_size = args.inference_size
|
396 |
+
max_detnum = args.max_detnum
|
397 |
slider_step = args.slider_step
|
398 |
|
399 |
is_fonts(f"{ROOT_PATH}/fonts") # 检查字体文件
|
|
|
403 |
|
404 |
model_cls_name_cp = model_cls_name.copy() # 类别名称
|
405 |
|
406 |
+
# ------------ Gradio Blocks ------------
|
407 |
+
with gr.Blocks() as gyd:
|
408 |
+
with gr.Row():
|
409 |
+
# gr.HTML(value="<div align='center' style='font-size:25px'>Gradio YOLOv8 Det<div> \
|
410 |
+
# <div align='center' style='font-size:20px'>基于 YOLOv8 的目标检测与图像分割系统<div>")
|
411 |
+
gr.Markdown(value="<p align='center'><a href='https://gitee.com/CV_Lab/gradio-yolov8-det'>\
|
412 |
+
<img src='https://pycver.gitee.io/ows-pics/imgs/gradio_yolov8_det_logo.png' alt='Simple Icons' ></a>\
|
413 |
+
<p align='center'>基于 Gradio 的 YOLOv8 通用目标检测与图像分割演示系统</p><p align='center'>可自定义检测模型、演示便捷、安装简单</p>"
|
414 |
+
)
|
415 |
+
with gr.Row():
|
416 |
+
gr.Markdown(value="作者:曾逸夫,Gitee:https://gitee.com/PyCVer ,Github:https://github.com/Zengyf-CVer")
|
417 |
+
with gr.Row():
|
418 |
+
with gr.Column(scale=1):
|
419 |
+
with gr.Row():
|
420 |
+
inputs_img = gr.Image(image_mode="RGB", sources=source, type="filepath", label="原始图片")
|
421 |
+
|
422 |
+
with gr.Row():
|
423 |
+
device_opt = gr.Radio(choices=["cpu", "0", "1", "2", "3"], value="cpu", label="设备")
|
424 |
+
with gr.Row():
|
425 |
+
inputs_model = gr.Dropdown(choices=model_names, value=model_name, type="value", label="模型")
|
426 |
+
with gr.Row():
|
427 |
+
inputs_size = gr.Slider(320, 1600, step=1, value=inference_size, label="推理尺寸")
|
428 |
+
max_det = gr.Slider(1, 1000, step=1, value=max_detnum, label="最大检测数")
|
429 |
+
with gr.Row():
|
430 |
+
input_conf = gr.Slider(0, 1, step=slider_step, value=nms_conf, label="置信度阈值")
|
431 |
+
inputs_iou = gr.Slider(0, 1, step=slider_step, value=nms_iou, label="IoU 阈值")
|
432 |
+
with gr.Row():
|
433 |
+
obj_size = gr.Radio(choices=["所有尺寸", "小目标", "中目标", "大目标"], value="所有尺寸", label="目标尺寸")
|
434 |
+
with gr.Row():
|
435 |
+
gr.ClearButton(inputs_img, value="清除")
|
436 |
+
det_btn_img = gr.Button(value='检测', variant="primary")
|
437 |
+
|
438 |
+
with gr.Column(scale=1):
|
439 |
+
with gr.Row():
|
440 |
+
outputs_img = gr.Image(type="pil", label="检测图片")
|
441 |
+
with gr.Row():
|
442 |
+
outputs_objSize = gr.Label(label="目标尺寸占比统计")
|
443 |
+
with gr.Row():
|
444 |
+
outputs_clsSize = gr.Label(label="类别检测占比统计")
|
445 |
+
|
446 |
+
with gr.Row():
|
447 |
+
example_list = [
|
448 |
+
["./img_examples/bus.jpg", "yolov8s", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
449 |
+
["./img_examples/giraffe.jpg", "yolov8l", "cpu", 320, 0.5, 0.45, 100, "所有尺寸"],
|
450 |
+
["./img_examples/zidane.jpg", "yolov8m", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
451 |
+
["./img_examples/Millenial-at-work.jpg", "yolov8x", "cpu", 1280, 0.5, 0.5, 100, "所有尺寸"],
|
452 |
+
["./img_examples/bus.jpg", "yolov8s-seg", "cpu", 640, 0.6, 0.5, 100, "所有尺寸"],
|
453 |
+
["./img_examples/Millenial-at-work.jpg", "yolov8x-seg", "cpu", 1280, 0.5, 0.5, 100, "所有尺寸"],]
|
454 |
+
gr.Examples(example_list,
|
455 |
+
[inputs_img, inputs_model, device_opt, inputs_size, input_conf, inputs_iou, max_det, obj_size],
|
456 |
+
[outputs_img, outputs_objSize, outputs_clsSize],
|
457 |
+
yolo_det_img,
|
458 |
+
cache_examples=False)
|
459 |
+
|
460 |
+
det_btn_img.click(fn=yolo_det_img,
|
461 |
+
inputs=[
|
462 |
+
inputs_img, inputs_model, device_opt, inputs_size, input_conf, inputs_iou, max_det,
|
463 |
+
obj_size],
|
464 |
+
outputs=[outputs_img, outputs_objSize, outputs_clsSize])
|
465 |
+
|
466 |
+
return gyd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
467 |
|
468 |
|
469 |
if __name__ == "__main__":
|
470 |
args = parse_args()
|
471 |
+
gyd = main(args)
|
472 |
is_share = args.is_share
|
473 |
|
474 |
+
gyd.queue().launch(
|
475 |
inbrowser=True, # 自动打开默认浏览器
|
|
|
476 |
share=is_share, # 项目共享,其他设备可以访问
|
477 |
favicon_path="./icon/logo.ico", # 网页图标
|
478 |
show_error=True, # 在浏览器控制台中显示错误信息
|