whybeyoung Zengyf-CVer commited on
Commit
a882a63
·
0 Parent(s):

Duplicate from Gradio-Blocks/Gradio_YOLOv5_Det

Browse files

Co-authored-by: ZengYifu <[email protected]>

.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.onnx filter=lfs diff=lfs merge=lfs -text
13
+ *.ot filter=lfs diff=lfs merge=lfs -text
14
+ *.parquet filter=lfs diff=lfs merge=lfs -text
15
+ *.pb filter=lfs diff=lfs merge=lfs -text
16
+ *.pt filter=lfs diff=lfs merge=lfs -text
17
+ *.pth filter=lfs diff=lfs merge=lfs -text
18
+ *.rar filter=lfs diff=lfs merge=lfs -text
19
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
20
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
21
+ *.tflite filter=lfs diff=lfs merge=lfs -text
22
+ *.tgz filter=lfs diff=lfs merge=lfs -text
23
+ *.wasm filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 图片格式
2
+ *.jpg
3
+ *.jpeg
4
+ *.png
5
+ *.svg
6
+ *.gif
7
+
8
+ # 视频格式
9
+ *.mp4
10
+ *.avi
11
+ .ipynb_checkpoints
12
+ */__pycache__
13
+
14
+ # 日志格式
15
+ *.log
16
+ *.datas
17
+ *.txt
18
+
19
+ # 生成文件
20
+ *.pdf
21
+ *.xlsx
22
+ *.csv
23
+
24
+ # 参数文件
25
+ *.yaml
26
+ *.json
27
+
28
+ # 压缩文件格式
29
+ *.zip
30
+ *.tar
31
+ *.tar.gz
32
+ *.rar
33
+
34
+ # 字体格式
35
+ *.ttc
36
+ *.ttf
37
+ *.otf
38
+
39
+ # 模型文件
40
+ *.pt
41
+ *.db
42
+
43
+ /flagged
44
+ /run
45
+ !requirements.txt
46
+ !cls_name/*
47
+ !model_config/*
48
+ !img_example/*
49
+ !packages.txt
50
+
51
+ app copy.py
README.md ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Gradio_YOLOv5_Det
3
+ emoji: 🚀
4
+ colorFrom: red
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.0.9
8
+ app_file: app.py
9
+ pinned: true
10
+ license: gpl-3.0
11
+ duplicated_from: Gradio-Blocks/Gradio_YOLOv5_Det
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
15
+
16
+ 🚀 Project homepage:https://gitee.com/CV_Lab/gradio_yolov5_det
__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __author__ = "曾逸夫(Zeng Yifu)"
2
+ __email__ = "[email protected]"
app.py ADDED
@@ -0,0 +1,682 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gradio YOLOv5 Det v0.4
2
+ # author: Zeng Yifu(曾逸夫)
3
+ # creation time: 2022-05-28
4
+ # email: [email protected]
5
+ # project homepage: https://gitee.com/CV_Lab/gradio_yolov5_det
6
+
7
+ import argparse
8
+ import csv
9
+ import gc
10
+ import json
11
+ import os
12
+ import sys
13
+ from collections import Counter
14
+ from pathlib import Path
15
+
16
+ import cv2
17
+ import gradio as gr
18
+ import numpy as np
19
+ import pandas as pd
20
+ import torch
21
+ import yaml
22
+ from PIL import Image, ImageDraw, ImageFont
23
+
24
+ from util.fonts_opt import is_fonts
25
+ from util.pdf_opt import pdf_generate
26
+
27
+ ROOT_PATH = sys.path[0] # root directory
28
+
29
+ # model path
30
+ model_path = "ultralytics/yolov5"
31
+
32
+ # Gradio YOLOv5 Det version
33
+ GYD_VERSION = "Gradio YOLOv5 Det v0.4"
34
+
35
+ # model name temporary variable
36
+ model_name_tmp = ""
37
+
38
+ # Device temporary variables
39
+ device_tmp = ""
40
+
41
+ # File extension
42
+ suffix_list = [".csv", ".yaml"]
43
+
44
+ # font size
45
+ FONTSIZE = 25
46
+
47
+ # object style
48
+ obj_style = ["Small Object", "Medium Object", "Large Object"]
49
+
50
+
51
+ def parse_args(known=False):
52
+ parser = argparse.ArgumentParser(description="Gradio YOLOv5 Det v0.4")
53
+ parser.add_argument("--source", "-src", default="upload", type=str, help="input source")
54
+ parser.add_argument("--source_video", "-src_v", default="webcam", type=str, help="video input source")
55
+ parser.add_argument("--img_tool", "-it", default="editor", type=str, help="input image tool")
56
+ parser.add_argument("--model_name", "-mn", default="yolov5s", type=str, help="model name")
57
+ parser.add_argument(
58
+ "--model_cfg",
59
+ "-mc",
60
+ default="./model_config/model_name_p5_p6_all.yaml",
61
+ type=str,
62
+ help="model config",
63
+ )
64
+ parser.add_argument(
65
+ "--cls_name",
66
+ "-cls",
67
+ default="./cls_name/cls_name_en.yaml",
68
+ type=str,
69
+ help="cls name",
70
+ )
71
+ parser.add_argument(
72
+ "--nms_conf",
73
+ "-conf",
74
+ default=0.5,
75
+ type=float,
76
+ help="model NMS confidence threshold",
77
+ )
78
+ parser.add_argument("--nms_iou", "-iou", default=0.45, type=float, help="model NMS IoU threshold")
79
+ parser.add_argument(
80
+ "--device",
81
+ "-dev",
82
+ default="cpu",
83
+ type=str,
84
+ help="cuda or cpu",
85
+ )
86
+ parser.add_argument("--inference_size", "-isz", default=640, type=int, help="model inference size")
87
+ parser.add_argument("--max_detnum", "-mdn", default=50, type=float, help="model max det num")
88
+ parser.add_argument("--slider_step", "-ss", default=0.05, type=float, help="slider step")
89
+ parser.add_argument(
90
+ "--is_login",
91
+ "-isl",
92
+ action="store_true",
93
+ default=False,
94
+ help="is login",
95
+ )
96
+ parser.add_argument('--usr_pwd',
97
+ "-up",
98
+ nargs='+',
99
+ type=str,
100
+ default=["admin", "admin"],
101
+ help="user & password for login")
102
+ parser.add_argument(
103
+ "--is_share",
104
+ "-is",
105
+ action="store_true",
106
+ default=False,
107
+ help="is login",
108
+ )
109
+
110
+ args = parser.parse_known_args()[0] if known else parser.parse_args()
111
+ return args
112
+
113
+
114
+ # yaml file parsing
115
+ def yaml_parse(file_path):
116
+ return yaml.safe_load(open(file_path, encoding="utf-8").read())
117
+
118
+
119
+ # yaml csv file parsing
120
+ def yaml_csv(file_path, file_tag):
121
+ file_suffix = Path(file_path).suffix
122
+ if file_suffix == suffix_list[0]:
123
+ # model name
124
+ file_names = [i[0] for i in list(csv.reader(open(file_path)))] # csv version
125
+ elif file_suffix == suffix_list[1]:
126
+ # model name
127
+ file_names = yaml_parse(file_path).get(file_tag) # yaml version
128
+ else:
129
+ print(f"{file_path} is not in the correct format! Program exits!")
130
+ sys.exit()
131
+
132
+ return file_names
133
+
134
+
135
+ # model loading
136
+ def model_loading(model_name, device, opt=[]):
137
+
138
+ # 加载本地模型
139
+ try:
140
+ # load model
141
+ model = torch.hub.load(model_path,
142
+ model_name,
143
+ force_reload=[True if "refresh_yolov5" in opt else False][0],
144
+ device=device,
145
+ _verbose=False)
146
+ except Exception as e:
147
+ print(e)
148
+ else:
149
+ print(f"🚀 welcome to {GYD_VERSION},{model_name} loaded successfully!")
150
+
151
+ return model
152
+
153
+
154
+ # check information
155
+ def export_json(results, img_size):
156
+
157
+ return [[{
158
+ "ID": i,
159
+ "CLASS": int(result[i][5]),
160
+ "CLASS_NAME": model_cls_name_cp[int(result[i][5])],
161
+ "BOUNDING_BOX": {
162
+ "XMIN": round(result[i][:4].tolist()[0], 6),
163
+ "YMIN": round(result[i][:4].tolist()[1], 6),
164
+ "XMAX": round(result[i][:4].tolist()[2], 6),
165
+ "YMAX": round(result[i][:4].tolist()[3], 6),},
166
+ "CONF": round(float(result[i][4]), 2),
167
+ "FPS": round(1000 / float(results.t[1]), 2),
168
+ "IMG_WIDTH": img_size[0],
169
+ "IMG_HEIGHT": img_size[1],} for i in range(len(result))] for result in results.xyxyn]
170
+
171
+
172
+ # frame conversion
173
+ def pil_draw(img, countdown_msg, textFont, xyxy, font_size, opt, obj_cls_index, color_list):
174
+
175
+ img_pil = ImageDraw.Draw(img)
176
+
177
+ img_pil.rectangle(xyxy, fill=None, outline=color_list[obj_cls_index]) # bounding box
178
+
179
+ if "label" in opt:
180
+ text_w, text_h = textFont.getsize(countdown_msg) # Label size
181
+
182
+ img_pil.rectangle(
183
+ (xyxy[0], xyxy[1], xyxy[0] + text_w, xyxy[1] + text_h),
184
+ fill=color_list[obj_cls_index],
185
+ outline=color_list[obj_cls_index],
186
+ ) # label background
187
+
188
+ img_pil.multiline_text(
189
+ (xyxy[0], xyxy[1]),
190
+ countdown_msg,
191
+ fill=(255, 255, 255),
192
+ font=textFont,
193
+ align="center",
194
+ )
195
+
196
+ return img
197
+
198
+
199
+ # Label and bounding box color settings
200
+ def color_set(cls_num):
201
+ color_list = []
202
+ for i in range(cls_num):
203
+ color = tuple(np.random.choice(range(256), size=3))
204
+ # color = ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])]
205
+ color_list.append(color)
206
+
207
+ return color_list
208
+
209
+
210
+ # YOLOv5 image detection function
211
+ def yolo_det_img(img, device, model_name, infer_size, conf, iou, max_num, model_cls, opt):
212
+
213
+ global model, model_name_tmp, device_tmp
214
+
215
+ # object size num
216
+ s_obj, m_obj, l_obj = 0, 0, 0
217
+ # object area list
218
+ area_obj_all = []
219
+ # cls num stat
220
+ cls_det_stat = []
221
+
222
+ if model_name_tmp != model_name:
223
+ # Model judgment to avoid repeated loading
224
+ model_name_tmp = model_name
225
+ print(f"Loading model {model_name_tmp}......")
226
+ model = model_loading(model_name_tmp, device, opt)
227
+ elif device_tmp != device:
228
+ # Device judgment to avoid repeated loading
229
+ device_tmp = device
230
+ print(f"Loading model {model_name_tmp}......")
231
+ model = model_loading(model_name_tmp, device, opt)
232
+ else:
233
+ print(f"Loading model {model_name_tmp}......")
234
+ model = model_loading(model_name_tmp, device, opt)
235
+
236
+ # -------------Model tuning -------------
237
+ model.conf = conf # NMS confidence threshold
238
+ model.iou = iou # NMS IoU threshold
239
+ model.max_det = int(max_num) # Maximum number of detection frames
240
+ model.classes = model_cls # model classes
241
+
242
+ color_list = color_set(len(model_cls_name_cp)) # 设置颜色
243
+
244
+ img_size = img.size # frame size
245
+
246
+ results = model(img, size=infer_size) # detection
247
+
248
+ # ----------------目标裁剪----------------
249
+ crops = results.crop(save=False)
250
+ img_crops = []
251
+ for i in range(len(crops)):
252
+ img_crops.append(crops[i]["im"][..., ::-1])
253
+
254
+ # Data Frame
255
+ dataframe = results.pandas().xyxy[0].round(2)
256
+
257
+ det_csv = "./Det_Report.csv"
258
+ det_excel = "./Det_Report.xlsx"
259
+
260
+ if "csv" in opt:
261
+ dataframe.to_csv(det_csv, index=False)
262
+ else:
263
+ det_csv = None
264
+
265
+ if "excel" in opt:
266
+ dataframe.to_excel(det_excel, sheet_name='sheet1', index=False)
267
+ else:
268
+ det_excel = None
269
+
270
+ # ----------------Load fonts----------------
271
+ yaml_index = cls_name.index(".yaml")
272
+ cls_name_lang = cls_name[yaml_index - 2:yaml_index]
273
+
274
+ if cls_name_lang == "zh":
275
+ # Chinese
276
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/SimSun.ttf"), size=FONTSIZE)
277
+ elif cls_name_lang in ["en", "ru", "es", "ar"]:
278
+ # English, Russian, Spanish, Arabic
279
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/TimesNewRoman.ttf"), size=FONTSIZE)
280
+ elif cls_name_lang == "ko":
281
+ # Korean
282
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/malgun.ttf"), size=FONTSIZE)
283
+
284
+ for result in results.xyxyn:
285
+ for i in range(len(result)):
286
+ id = int(i) # instance ID
287
+ obj_cls_index = int(result[i][5]) # category index
288
+ obj_cls = model_cls_name_cp[obj_cls_index] # category
289
+ cls_det_stat.append(obj_cls)
290
+
291
+ # ------------ border coordinates ------------
292
+ x0 = float(result[i][:4].tolist()[0])
293
+ y0 = float(result[i][:4].tolist()[1])
294
+ x1 = float(result[i][:4].tolist()[2])
295
+ y1 = float(result[i][:4].tolist()[3])
296
+
297
+ # ------------ Actual coordinates of the border ------------
298
+ x0 = int(img_size[0] * x0)
299
+ y0 = int(img_size[1] * y0)
300
+ x1 = int(img_size[0] * x1)
301
+ y1 = int(img_size[1] * y1)
302
+
303
+ conf = float(result[i][4]) # confidence
304
+ # fps = f"{(1000 / float(results.t[1])):.2f}" # FPS
305
+
306
+ det_img = pil_draw(
307
+ img,
308
+ f"{id}-{obj_cls}:{conf:.2f}",
309
+ textFont,
310
+ [x0, y0, x1, y1],
311
+ FONTSIZE,
312
+ opt,
313
+ obj_cls_index,
314
+ color_list,
315
+ )
316
+
317
+ # ----------add object size----------
318
+ w_obj = x1 - x0
319
+ h_obj = y1 - y0
320
+ area_obj = w_obj * h_obj
321
+ area_obj_all.append(area_obj)
322
+
323
+ # ------------JSON generate------------
324
+ det_json = export_json(results, img.size)[0] # Detection information
325
+ det_json_format = json.dumps(det_json, sort_keys=False, indent=4, separators=(",", ":"),
326
+ ensure_ascii=False) # JSON formatting
327
+
328
+ if "json" not in opt:
329
+ det_json = None
330
+
331
+ # -------PDF generate-------
332
+ report = "./Det_Report.pdf"
333
+ if "pdf" in opt:
334
+ pdf_generate(f"{det_json_format}", report, GYD_VERSION)
335
+ else:
336
+ report = None
337
+
338
+ # --------------object size compute--------------
339
+ for i in range(len(area_obj_all)):
340
+ if (0 < area_obj_all[i] <= 32 ** 2):
341
+ s_obj = s_obj + 1
342
+ elif (32 ** 2 < area_obj_all[i] <= 96 ** 2):
343
+ m_obj = m_obj + 1
344
+ elif (area_obj_all[i] > 96 ** 2):
345
+ l_obj = l_obj + 1
346
+
347
+ sml_obj_total = s_obj + m_obj + l_obj
348
+
349
+ objSize_dict = {obj_style[i]: [s_obj, m_obj, l_obj][i] / sml_obj_total for i in range(3)}
350
+
351
+ # ------------cls stat------------
352
+ clsRatio_dict = {}
353
+ clsDet_dict = Counter(cls_det_stat)
354
+ clsDet_dict_sum = sum(clsDet_dict.values())
355
+
356
+ for k, v in clsDet_dict.items():
357
+ clsRatio_dict[k] = v / clsDet_dict_sum
358
+
359
+ return det_img, img_crops, objSize_dict, clsRatio_dict, dataframe, det_json, report, det_csv, det_excel
360
+
361
+
362
+ # YOLOv5 video detection function
363
+ def yolo_det_video(video, device, model_name, infer_size, conf, iou, max_num, model_cls, opt):
364
+
365
+ global model, model_name_tmp, device_tmp
366
+
367
+ os.system("""
368
+ if [ -e './output.mp4' ]; then
369
+ rm ./output.mp4
370
+ fi
371
+ """)
372
+
373
+ if model_name_tmp != model_name:
374
+ # Model judgment to avoid repeated loading
375
+ model_name_tmp = model_name
376
+ print(f"Loading model {model_name_tmp}......")
377
+ model = model_loading(model_name_tmp, device, opt)
378
+ elif device_tmp != device:
379
+ # Device judgment to avoid repeated loading
380
+ device_tmp = device
381
+ print(f"Loading model {model_name_tmp}......")
382
+ model = model_loading(model_name_tmp, device, opt)
383
+ else:
384
+ print(f"Loading model {model_name_tmp}......")
385
+ model = model_loading(model_name_tmp, device, opt)
386
+
387
+ # -------------Model tuning -------------
388
+ model.conf = conf # NMS confidence threshold
389
+ model.iou = iou # NMS IOU threshold
390
+ model.max_det = int(max_num) # Maximum number of detection frames
391
+ model.classes = model_cls # model classes
392
+
393
+ color_list = color_set(len(model_cls_name_cp)) # 设置颜色
394
+
395
+ # ----------------Load fonts----------------
396
+ yaml_index = cls_name.index(".yaml")
397
+ cls_name_lang = cls_name[yaml_index - 2:yaml_index]
398
+
399
+ if cls_name_lang == "zh":
400
+ # Chinese
401
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/SimSun.ttf"), size=FONTSIZE)
402
+ elif cls_name_lang in ["en", "ru", "es", "ar"]:
403
+ # English, Russian, Spanish, Arabic
404
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/TimesNewRoman.ttf"), size=FONTSIZE)
405
+ elif cls_name_lang == "ko":
406
+ # Korean
407
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/malgun.ttf"), size=FONTSIZE)
408
+
409
+ # video->frame
410
+ gc.collect()
411
+ output_video_path = "./output.avi"
412
+ cap = cv2.VideoCapture(video)
413
+ fourcc = cv2.VideoWriter_fourcc(*"I420") # encoder
414
+
415
+ out = cv2.VideoWriter(output_video_path, fourcc, 30.0, (int(cap.get(3)), int(cap.get(4))))
416
+ while cap.isOpened():
417
+ ret, frame = cap.read()
418
+ # Determine empty frame
419
+ if not ret:
420
+ break
421
+
422
+ results = model(frame, size=infer_size) # detection
423
+ h, w, _ = frame.shape # frame size
424
+ img_size = (w, h) # frame size
425
+
426
+ for result in results.xyxyn:
427
+ for i in range(len(result)):
428
+ id = int(i) # instance ID
429
+ obj_cls_index = int(result[i][5]) # category index
430
+ obj_cls = model_cls_name_cp[obj_cls_index] # category
431
+
432
+ # ------------ border coordinates ------------
433
+ x0 = float(result[i][:4].tolist()[0])
434
+ y0 = float(result[i][:4].tolist()[1])
435
+ x1 = float(result[i][:4].tolist()[2])
436
+ y1 = float(result[i][:4].tolist()[3])
437
+
438
+ # ------------ Actual coordinates of the border ------------
439
+ x0 = int(img_size[0] * x0)
440
+ y0 = int(img_size[1] * y0)
441
+ x1 = int(img_size[0] * x1)
442
+ y1 = int(img_size[1] * y1)
443
+
444
+ conf = float(result[i][4]) # confidence
445
+ # fps = f"{(1000 / float(results.t[1])):.2f}" # FPS
446
+
447
+ frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
448
+ frame = pil_draw(
449
+ frame,
450
+ f"{id}-{obj_cls}:{conf:.2f}",
451
+ textFont,
452
+ [x0, y0, x1, y1],
453
+ FONTSIZE,
454
+ opt,
455
+ obj_cls_index,
456
+ color_list,
457
+ )
458
+
459
+ frame = cv2.cvtColor(np.asarray(frame), cv2.COLOR_RGB2BGR)
460
+
461
+ # frame->video
462
+ out.write(frame)
463
+ out.release()
464
+ cap.release()
465
+ # cv2.destroyAllWindows()
466
+
467
+ return output_video_path
468
+
469
+
470
+ def main(args):
471
+ gr.close_all()
472
+
473
+ global model, model_cls_name_cp, cls_name
474
+
475
+ source = args.source
476
+ source_video = args.source_video
477
+ img_tool = args.img_tool
478
+ nms_conf = args.nms_conf
479
+ nms_iou = args.nms_iou
480
+ model_name = args.model_name
481
+ model_cfg = args.model_cfg
482
+ cls_name = args.cls_name
483
+ device = args.device
484
+ inference_size = args.inference_size
485
+ max_detnum = args.max_detnum
486
+ slider_step = args.slider_step
487
+ is_login = args.is_login
488
+ usr_pwd = args.usr_pwd
489
+ is_share = args.is_share
490
+
491
+ is_fonts(f"{ROOT_PATH}/fonts") # Check font files
492
+
493
+ # model loading
494
+ model = model_loading(model_name, device)
495
+
496
+ model_names = yaml_csv(model_cfg, "model_names") # model names
497
+ model_cls_name = yaml_csv(cls_name, "model_cls_name") # class name
498
+
499
+ model_cls_name_cp = model_cls_name.copy() # class name
500
+
501
+ # ------------------- Input Components -------------------
502
+ inputs_img = gr.Image(image_mode="RGB", source=source, tool=img_tool, type="pil", label="original image")
503
+ inputs_device01 = gr.Radio(choices=["cuda:0", "cpu"], value=device, label="device")
504
+ inputs_model01 = gr.Dropdown(choices=model_names, value=model_name, type="value", label="model")
505
+ inputs_size01 = gr.Radio(choices=[320, 640, 1280], value=inference_size, label="inference size")
506
+ input_conf01 = gr.Slider(0, 1, step=slider_step, value=nms_conf, label="confidence threshold")
507
+ inputs_iou01 = gr.Slider(0, 1, step=slider_step, value=nms_iou, label="IoU threshold")
508
+ inputs_maxnum01 = gr.Number(value=max_detnum, label="Maximum number of detections")
509
+ inputs_clsName01 = gr.CheckboxGroup(choices=model_cls_name, value=model_cls_name, type="index", label="category")
510
+ inputs_opt01 = gr.CheckboxGroup(choices=["refresh_yolov5", "label", "pdf", "json", "csv", "excel"],
511
+ value=["label", "pdf"],
512
+ type="value",
513
+ label="operate")
514
+
515
+ # ------------------- Input Components -------------------
516
+ inputs_video = gr.Video(format="mp4", source=source_video, label="original video") # webcam
517
+ inputs_device02 = gr.Radio(choices=["cuda:0", "cpu"], value=device, label="device")
518
+ inputs_model02 = gr.Dropdown(choices=model_names, value=model_name, type="value", label="model")
519
+ inputs_size02 = gr.Radio(choices=[320, 640, 1280], value=inference_size, label="inference size")
520
+ input_conf02 = gr.Slider(0, 1, step=slider_step, value=nms_conf, label="confidence threshold")
521
+ inputs_iou02 = gr.Slider(0, 1, step=slider_step, value=nms_iou, label="IoU threshold")
522
+ inputs_maxnum02 = gr.Number(value=max_detnum, label="Maximum number of detections")
523
+ inputs_clsName02 = gr.CheckboxGroup(choices=model_cls_name, value=model_cls_name, type="index", label="category")
524
+ inputs_opt02 = gr.CheckboxGroup(choices=["refresh_yolov5", "label"], value=["label"], type="value", label="operate")
525
+
526
+ # Input parameters
527
+ inputs_img_list = [
528
+ inputs_img, # input image
529
+ inputs_device01, # device
530
+ inputs_model01, # model
531
+ inputs_size01, # inference size
532
+ input_conf01, # confidence threshold
533
+ inputs_iou01, # IoU threshold
534
+ inputs_maxnum01, # maximum number of detections
535
+ inputs_clsName01, # category
536
+ inputs_opt01, # detect operations
537
+ ]
538
+
539
+ inputs_video_list = [
540
+ inputs_video, # input image
541
+ inputs_device02, # device
542
+ inputs_model02, # model
543
+ inputs_size02, # inference size
544
+ input_conf02, # confidence threshold
545
+ inputs_iou02, # IoU threshold
546
+ inputs_maxnum02, # maximum number of detections
547
+ inputs_clsName02, # category
548
+ inputs_opt02, # detect operation
549
+ ]
550
+
551
+ # -------------------output component-------------------
552
+ outputs_img = gr.Image(type="pil", label="Detection image")
553
+ outputs_crops = gr.Gallery(label="Object crop")
554
+ outputs_df = gr.Dataframe(max_rows=5,
555
+ overflow_row_behaviour="paginate",
556
+ type="pandas",
557
+ label="List of detection information")
558
+ outputs_objSize = gr.Label(label="Object size ratio statistics")
559
+ outputs_clsSize = gr.Label(label="Category detection proportion statistics")
560
+ outputs_json = gr.JSON(label="Detection information")
561
+ outputs_pdf = gr.File(label="pdf detection report")
562
+ outputs_csv = gr.File(label="csv detection report")
563
+ outputs_excel = gr.File(label="xlsx detection report")
564
+
565
+ # -------------------output component-------------------
566
+ outputs_video = gr.Video(format='mp4', label="Detection video")
567
+
568
+ # output parameters
569
+ outputs_img_list = [
570
+ outputs_img, outputs_crops, outputs_objSize, outputs_clsSize, outputs_df, outputs_json, outputs_pdf,
571
+ outputs_csv, outputs_excel]
572
+ outputs_video_list = [outputs_video]
573
+
574
+ # title
575
+ title = "Gradio YOLOv5 Det v0.4"
576
+
577
+ # describe
578
+ description = "Author: 曾逸夫(Zeng Yifu), Project Address: https://gitee.com/CV_Lab/gradio_yolov5_det, Github: https://github.com/Zengyf-CVer, thanks to [Gradio](https://github.com/gradio-app/gradio) & [YOLOv5](https://github.com/ultralytics/yolov5)"
579
+ # article="https://gitee.com/CV_Lab/gradio_yolov5_det"
580
+
581
+ # example image
582
+ examples = [
583
+ [
584
+ "./img_example/bus.jpg",
585
+ "cpu",
586
+ "yolov5s",
587
+ 640,
588
+ 0.6,
589
+ 0.5,
590
+ 10,
591
+ ["person", "bus"],
592
+ ["label", "pdf"],],
593
+ [
594
+ "./img_example/giraffe.jpg",
595
+ "cpu",
596
+ "yolov5l",
597
+ 320,
598
+ 0.5,
599
+ 0.45,
600
+ 12,
601
+ ["giraffe"],
602
+ ["label", "pdf"],],
603
+ [
604
+ "./img_example/zidane.jpg",
605
+ "cpu",
606
+ "yolov5m",
607
+ 640,
608
+ 0.6,
609
+ 0.5,
610
+ 15,
611
+ ["person", "tie"],
612
+ ["pdf", "json"],],
613
+ [
614
+ "./img_example/Millenial-at-work.jpg",
615
+ "cpu",
616
+ "yolov5s6",
617
+ 1280,
618
+ 0.5,
619
+ 0.5,
620
+ 20,
621
+ ["person", "chair", "cup", "laptop"],
622
+ ["label", "pdf"],],]
623
+
624
+ # interface
625
+ gyd_img = gr.Interface(
626
+ fn=yolo_det_img,
627
+ inputs=inputs_img_list,
628
+ outputs=outputs_img_list,
629
+ title=title,
630
+ description=description,
631
+ # article=article,
632
+ examples=examples,
633
+ cache_examples=False,
634
+ # theme="seafoam",
635
+ # live=True, # Change output in real time
636
+ flagging_dir="run", # output directory
637
+ # allow_flagging="manual",
638
+ # flagging_options=["good", "generally", "bad"],
639
+ )
640
+
641
+ gyd_video = gr.Interface(
642
+ # fn=yolo_det_video_test,
643
+ fn=yolo_det_video,
644
+ inputs=inputs_video_list,
645
+ outputs=outputs_video_list,
646
+ title=title,
647
+ description=description,
648
+ # article=article,
649
+ # examples=examples,
650
+ # theme="seafoam",
651
+ # live=True, # Change output in real time
652
+ flagging_dir="run", # output directory
653
+ allow_flagging="never",
654
+ # flagging_options=["good", "generally", "bad"],
655
+ )
656
+
657
+ gyd = gr.TabbedInterface(interface_list=[gyd_img, gyd_video], tab_names=["Image Mode", "Video Mode"])
658
+
659
+ if not is_login:
660
+ gyd.launch(
661
+ inbrowser=True, # Automatically open default browser
662
+ show_tips=True, # Automatically display the latest features of gradio
663
+ share=is_share, # Project sharing, other devices can access
664
+ favicon_path="./icon/logo.ico", # web icon
665
+ show_error=True, # Display error message in browser console
666
+ quiet=True, # Suppress most print statements
667
+ )
668
+ else:
669
+ gyd.launch(
670
+ inbrowser=True, # Automatically open default browser
671
+ show_tips=True, # Automatically display the latest features of gradio
672
+ auth=usr_pwd, # login interface
673
+ share=is_share, # Project sharing, other devices can access
674
+ favicon_path="./icon/logo.ico", # web icon
675
+ show_error=True, # Display error message in browser console
676
+ quiet=True, # Suppress most print statements
677
+ )
678
+
679
+
680
+ if __name__ == "__main__":
681
+ args = parse_args()
682
+ main(args)
cls_name/cls_name.csv ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ 自行车
3
+ 汽车
4
+ 摩托车
5
+ 飞机
6
+ 公交车
7
+ 火车
8
+ 卡车
9
+
10
+ 红绿灯
11
+ 消防栓
12
+ 停止标志
13
+ 停车收费表
14
+ 长凳
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+ 斑马
24
+ 长颈鹿
25
+ 背包
26
+ 雨伞
27
+ 手提包
28
+ 领带
29
+ 手提箱
30
+ 飞盘
31
+ 滑雪板
32
+ 单板滑雪
33
+ 运动球
34
+ 风筝
35
+ 棒球棒
36
+ 棒球手套
37
+ 滑板
38
+ 冲浪板
39
+ 网球拍
40
+ 瓶子
41
+ 红酒杯
42
+ 杯子
43
+ 叉子
44
+
45
+
46
+
47
+ 香蕉
48
+ 苹果
49
+ 三明治
50
+ 橙子
51
+ 西兰花
52
+ 胡萝卜
53
+ 热狗
54
+ 比萨
55
+ 甜甜圈
56
+ 蛋糕
57
+ 椅子
58
+ 长椅
59
+ 盆栽
60
+
61
+ 餐桌
62
+ 马桶
63
+ 电视
64
+ 笔记本电脑
65
+ 鼠标
66
+ 遥控器
67
+ 键盘
68
+ 手机
69
+ 微波炉
70
+ 烤箱
71
+ 烤面包机
72
+ 洗碗槽
73
+ 冰箱
74
+
75
+ 时钟
76
+ 花瓶
77
+ 剪刀
78
+ 泰迪熊
79
+ 吹风机
80
+ 牙刷
cls_name/cls_name.yaml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ model_cls_name: ['人', '自行车', '汽车', '摩托车', '飞机', '公交车', '火车', '卡车', '船', '红绿灯', '消防栓', '停止标志',
2
+ '停车收费表', '长凳', '鸟', '猫', '狗', '马', '羊', '牛', '象', '熊', '斑马', '长颈鹿', '背包', '雨伞', '手提包', '领带',
3
+ '手提箱', '飞盘', '滑雪板', '单板滑雪', '运动球', '风筝', '棒球棒', '棒球手套', '滑板', '冲浪板', '网球拍', '瓶子', '红酒杯',
4
+ '杯子', '叉子', '刀', '勺', '碗', '香蕉', '苹果', '三明治', '橙子', '西兰花', '胡萝卜', '热狗', '比萨', '甜甜圈', '蛋糕',
5
+ '椅子', '长椅', '盆栽', '床', '餐桌', '马桶', '电视', '笔记本电脑', '鼠标', '遥控器', '键盘', '手机', '微波炉', '烤箱',
6
+ '烤面包机', '洗碗槽', '冰箱', '书', '时钟', '花瓶', '剪刀', '泰迪熊', '吹风机', '牙刷'
7
+ ]
cls_name/cls_name_ar.yaml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ model_cls_name: [" الناس " , " الدراجات " , " السيارات " , " الدراجات النارية " , " الطائرات " , " الحافلات " , " القطارات " , " الشاحنات " , " السفن " , " إشارات المرور " ,
2
+ " صنبور " , " علامة " , " موقف سيارات " , " الجدول " , " مقعد " , " الطيور " , " القط " , " الكلب " , " الحصان " , " الأغنام " , " الثور " , " الفيل " ,
3
+ " الدب " , " حمار وحشي " , " الزرافة " , " حقيبة " , " مظلة " , " حقيبة يد " , " ربطة عنق " , " حقيبة " , " الفريسبي " , " الزلاجات " , " الزلاجات " ,
4
+ " الكرة الرياضية " , " طائرة ورقية " , " مضرب بيسبول " , " قفازات البيسبول " , " لوح التزلج " , " ركوب الأمواج " , " مضرب تنس " , " زجاجة " ,
5
+ " كأس " , " كأس " , " شوكة " , " سكين " , " ملعقة " , " وعاء " , " الموز " , " التفاح " , " ساندويتش " , " البرتقال " , " القرنبيط " ,
6
+ " الجزر " , " الكلاب الساخنة " , " البيتزا " , " دونات " , " كعكة " , " كرسي " , " أريكة " , " بوعاء " , " السرير " , " طاولة الطعام " , " المرحاض " ,
7
+ التلفزيون , الكمبيوتر المحمول , الفأرة , وحدة تحكم عن بعد , لوحة المفاتيح , الهاتف المحمول , فرن الميكروويف , محمصة خبز كهربائية , بالوعة , ثلاجة ,
8
+ " كتاب " , " ساعة " , " زهرية " , " مقص " , " دمية دب " , " مجفف الشعر " , " فرشاة الأسنان "
9
+ ]
cls_name/cls_name_en.yaml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ model_cls_name: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
2
+ 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant',
3
+ 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
4
+ 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle',
5
+ 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli',
6
+ 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet',
7
+ 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator',
8
+ 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
9
+ ]
cls_name/cls_name_es.yaml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ model_cls_name: ['persona', 'bicicleta', 'coche', 'motocicleta', 'avión', 'autobús', 'tren', 'camión', 'barco', 'semáforo',
2
+ 'boca de incendios', 'señal de alto', 'parquímetro', 'banco', 'pájaro', 'gato', 'perro', 'caballo', 'oveja', 'vaca', 'elefante',
3
+ 'oso', 'cebra', 'jirafa', 'mochila', 'paraguas', 'bolso', 'corbata', 'maleta', 'frisbee', 'esquís', 'snowboard',
4
+ 'pelota deportiva', 'cometa', 'bate de béisbol', 'guante de béisbol', 'monopatín', 'tabla de surf', 'raqueta de tenis', 'botella',
5
+ 'copa de vino', 'taza', 'tenedor', 'cuchillo', 'cuchara', 'tazón', 'plátano', 'manzana', 'sándwich', 'naranja', 'brócoli',
6
+ 'zanahoria', 'perrito caliente', 'pizza', 'rosquilla', 'pastel', 'silla', 'sofá', 'planta en maceta', 'cama', 'mesa de comedor', 'inodoro',
7
+ 'tv', 'laptop', 'ratón', 'control remoto', 'teclado', 'celular', 'microondas', 'horno', 'tostadora', 'fregadero', 'nevera',
8
+ 'libro', 'reloj', 'jarrón', 'tijeras', 'oso de peluche', 'secador de pelo', 'cepillo de dientes'
9
+ ]
cls_name/cls_name_ko.yaml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ model_cls_name: ['사람', '자전거', '자동차', '오토바이', '비행기', '버스', '기차', '트럭', '보트', '신호등',
2
+ '소화전', '정지 신호', '주차 미터기', '벤치', '새', '고양이', '개', '말', '양', '소', '코끼리',
3
+ '곰', '얼룩말', '기린', '배낭', '우산', '핸드백', '타이', '여행가방', '프리스비', '스키', '스노우보드',
4
+ '스포츠 공', '연', '야구 방망이', '야구 글러브', '스케이트보드', '서프보드', '테니스 라켓', '병',
5
+ '와인잔', '컵', '포크', '나이프', '숟가락', '그릇', '바나나', '사과', '샌드위치', '오렌지', '브로콜리',
6
+ '당근', '핫도그', '피자', '도넛', '케이크', '의자', '소파', '화분', '침대', '식탁', '화장실',
7
+ 'tv', '노트북', '마우스', '리모컨', '키보드', '휴대전화', '전자레인지', '오븐', '토스터', '싱크대', '냉장고',
8
+ '책', '시계', '꽃병', '가위', '테디베어', '드라이기', '칫솔'
9
+ ]
cls_name/cls_name_ru.yaml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ model_cls_name: ['человек', 'велосипед', 'автомобиль', 'мотоцикл', 'самолет', 'автобус', 'поезд', 'грузовик', 'лодка', 'светофор',
2
+ 'пожарный гидрант', 'стоп', 'паркомат', 'скамейка', 'птица', 'кошка', 'собака', 'лошадь', 'овца', 'корова', 'слон',
3
+ 'медведь', 'зебра', 'жираф', 'рюкзак', 'зонт', 'сумочка', 'галстук', 'чемодан', 'фрисби', 'лыжи', 'сноуборд',
4
+ 'спортивный мяч', 'воздушный змей', 'бейсбольная бита', 'бейсбольная перчатка', 'скейтборд', 'доска для серфинга', 'теннисная ракетка', 'бутылка',
5
+ 'бокал', 'чашка', 'вилка', 'нож', 'ложка', 'миска', 'банан', 'яблоко', 'бутерброд', 'апельсин', 'брокколи',
6
+ 'морковь', 'хот-дог', 'пицца', 'пончик', 'торт', 'стул', 'диван', 'растение в горшке', 'кровать', 'обеденный стол', 'туалет',
7
+ 'телевизор', 'ноутбук', 'мышь', 'пульт', 'клавиатура', 'мобильный телефон', 'микроволновая печь', 'духовка', 'тостер', 'раковина', 'холодильник',
8
+ 'книга', 'часы', 'ваза', 'ножницы', 'плюшевый мишка', 'фен', 'зубная щетка'
9
+ ]
cls_name/cls_name_zh.yaml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ model_cls_name: ['人', '自行车', '汽车', '摩托车', '飞机', '公交车', '火车', '卡车', '船', '红绿灯', '消防栓', '停止标志',
2
+ '停车收费表', '长凳', '鸟', '猫', '狗', '马', '羊', '牛', '象', '熊', '斑马', '长颈鹿', '背包', '雨伞', '手提包', '领带',
3
+ '手提箱', '飞盘', '滑雪板', '单板滑雪', '运动球', '风筝', '棒球棒', '棒球手套', '滑板', '冲浪板', '网球拍', '瓶子', '红酒杯',
4
+ '杯子', '叉子', '刀', '勺', '碗', '香蕉', '苹果', '三明治', '橙子', '西兰花', '胡萝卜', '热狗', '比萨', '甜甜圈', '蛋糕',
5
+ '椅子', '长椅', '盆栽', '床', '餐桌', '马桶', '电视', '笔记本电脑', '鼠标', '遥控器', '键盘', '手机', '微波炉', '烤箱',
6
+ '烤面包机', '洗碗槽', '冰箱', '书', '时钟', '花瓶', '剪刀', '泰迪熊', '吹风机', '牙刷'
7
+ ]
img_example/Millenial-at-work.jpg ADDED
img_example/bus.jpg ADDED
img_example/giraffe.jpg ADDED
img_example/read.txt ADDED
File without changes
img_example/zidane.jpg ADDED
model_config/model_name_p5_all.csv ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ yolov5n
2
+ yolov5s
3
+ yolov5m
4
+ yolov5l
5
+ yolov5x
model_config/model_name_p5_all.yaml ADDED
@@ -0,0 +1 @@
 
 
1
+ model_names: ["yolov5n", "yolov5s", "yolov5m", "yolov5l", "yolov5x"]
model_config/model_name_p5_n.csv ADDED
@@ -0,0 +1 @@
 
 
1
+ yolov5n
model_config/model_name_p5_n.yaml ADDED
@@ -0,0 +1 @@
 
 
1
+ model_names: ["yolov5n"]
model_config/model_name_p5_p6_all.yaml ADDED
@@ -0,0 +1 @@
 
 
1
+ model_names: ["yolov5n", "yolov5s", "yolov5m", "yolov5l", "yolov5x", "yolov5n6", "yolov5s6", "yolov5m6", "yolov5l6", "yolov5x6"]
model_config/model_name_p6_all.csv ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ yolov5n6
2
+ yolov5s6
3
+ yolov5m6
4
+ yolov5l6
5
+ yolov5x6
model_config/model_name_p6_all.yaml ADDED
@@ -0,0 +1 @@
 
 
1
+ model_names: ["yolov5n6", "yolov5s6", "yolov5m6", "yolov5l6", "yolov5x6"]
model_download/yolov5_model_p5_all.sh ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ cd ./yolov5
2
+
3
+ # 下载YOLOv5模型
4
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5n.pt
5
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5s.pt
6
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5m.pt
7
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5l.pt
8
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5x.pt
model_download/yolov5_model_p5_n.sh ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ cd ./yolov5
2
+
3
+ # 下载YOLOv5模型
4
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5n.pt
model_download/yolov5_model_p6_all.sh ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ cd ./yolov5
2
+
3
+ # 下载YOLOv5模型
4
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5n6.pt
5
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5s6.pt
6
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5m6.pt
7
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5l6.pt
8
+ wget -c -t 0 https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5x6.pt
packages.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ ffmpeg
2
+ x264
3
+ libx264-dev
requirements.txt ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Base ----------------------------------------
2
+ matplotlib>=3.2.2
3
+ numpy>=1.22.3
4
+ opencv-python-headless>=4.5.5.64
5
+ Pillow>=7.1.2
6
+ PyYAML>=5.3.1
7
+ requests>=2.23.0
8
+ scipy>=1.4.1 # Google Colab version
9
+ torch>=1.7.0
10
+ torchvision>=0.8.1
11
+ tqdm>=4.41.0
12
+
13
+ # Gradio YOLOv5 Det ----------------------------------------
14
+ gradio>=3.0.3
15
+ wget>=3.2
16
+ rich>=12.2.0
17
+ fpdf>=1.7.2
18
+ plotly>=5.7.0
19
+ bokeh>=2.4.2
20
+ openpyxl>=3.0.10
21
+
22
+ # Logging -------------------------------------
23
+ tensorboard>=2.4.1
24
+ # wandb
25
+
26
+ # Plotting ------------------------------------
27
+ pandas>=1.1.4
28
+ seaborn>=0.11.0
29
+
30
+ # Export --------------------------------------
31
+ # coremltools>=4.1 # CoreML export
32
+ # onnx>=1.9.0 # ONNX export
33
+ # onnx-simplifier>=0.3.6 # ONNX simplifier
34
+ # scikit-learn==0.19.2 # CoreML quantization
35
+ # tensorflow>=2.4.1 # TFLite export
36
+ # tensorflowjs>=3.9.0 # TF.js export
37
+ # openvino-dev # OpenVINO export
38
+
39
+ # Extras --------------------------------------
40
+ ipython # interactive notebook
41
+ psutil # system utilization
42
+ thop # FLOPs computation
43
+ # albumentations>=1.0.3
44
+ # pycocotools>=2.0 # COCO mAP
45
+ # roboflow
util/fonts_opt.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # font management
2
+ # author: Zeng Yifu(曾逸夫)
3
+ # creation time: 2022-05-01
4
+ # email: [email protected]
5
+ # project homepage: https://gitee.com/CV_Lab/gradio_yolov5_det
6
+
7
+ import os
8
+ import sys
9
+ from pathlib import Path
10
+
11
+ import wget
12
+ from rich.console import Console
13
+
14
+ ROOT_PATH = sys.path[0] # Project root directory
15
+
16
+ # Chinese, English, Russian, Spanish, Arabic, Korean
17
+ fonts_list = ["SimSun.ttf", "TimesNewRoman.ttf", "malgun.ttf"] # font list
18
+ fonts_suffix = ["ttc", "ttf", "otf"] # font suffix
19
+
20
+ data_url_dict = {
21
+ "SimSun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053539/download/SimSun.ttf",
22
+ "TimesNewRoman.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053537/download/TimesNewRoman.ttf",
23
+ "malgun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053538/download/malgun.ttf",}
24
+
25
+ console = Console()
26
+
27
+
28
+ # create font library
29
+ def add_fronts(font_diff):
30
+
31
+ global font_name
32
+
33
+ for k, v in data_url_dict.items():
34
+ if k in font_diff:
35
+ font_name = v.split("/")[-1] # font name
36
+ Path(f"{ROOT_PATH}/fonts").mkdir(parents=True, exist_ok=True) # Create a directory
37
+
38
+ file_path = f"{ROOT_PATH}/fonts/{font_name}" # font path
39
+
40
+ try:
41
+ # Download font file
42
+ wget.download(v, file_path)
43
+ except Exception as e:
44
+ print("Path error! Program ended!")
45
+ print(e)
46
+ sys.exit()
47
+ else:
48
+ print()
49
+ console.print(f"{font_name} [bold green]font file download complete![/bold green] has been saved to: {file_path}")
50
+
51
+
52
+ # Determine the font file
53
+ def is_fonts(fonts_dir):
54
+ if os.path.isdir(fonts_dir):
55
+ # if the font library exists
56
+ f_list = os.listdir(fonts_dir) # local font library
57
+
58
+ font_diff = list(set(fonts_list).difference(set(f_list)))
59
+
60
+ if font_diff != []:
61
+ # font does not exist
62
+ console.print("[bold red] font does not exist, loading...[/bold red]")
63
+ add_fronts(font_diff) # Create a font library
64
+ else:
65
+ console.print(f"{fonts_list}[bold green]font already exists![/bold green]")
66
+ else:
67
+ # The font library does not exist, create a font library
68
+ console.print("[bold red]font library does not exist, creating...[/bold red]")
69
+ add_fronts(fonts_list) # Create a font library
util/pdf_opt.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PDF management
2
+ # author: Zeng Yifu
3
+ # creation time: 2022-05-05
4
+
5
+ from fpdf import FPDF
6
+
7
+
8
+ # PDF generation class
9
+ class PDF(FPDF):
10
+ # Reference: https://pyfpdf.readthedocs.io/en/latest/Tutorial/index.html
11
+ def header(self):
12
+ # Set Chinese font
13
+ self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
14
+ self.set_font("SimSun", "", 16)
15
+ # Calculate width of title and position
16
+ w = self.get_string_width(title) + 6
17
+ self.set_x((210 - w) / 2)
18
+ # Colors of frame, background and text
19
+ self.set_draw_color(255, 255, 255)
20
+ self.set_fill_color(255, 255, 255)
21
+ self.set_text_color(0, 0, 0)
22
+ # Thickness of frame (1 mm)
23
+ # self.set_line_width(1)
24
+ # Title
25
+ self.cell(w, 9, title, 1, 1, "C", 1)
26
+ # Line break
27
+ self.ln(10)
28
+
29
+ def footer(self):
30
+ # Position at 1.5 cm from bottom
31
+ self.set_y(-15)
32
+ # Set Chinese font
33
+ self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
34
+ self.set_font("SimSun", "", 12)
35
+ # Text color in gray
36
+ self.set_text_color(128)
37
+ # Page number
38
+ self.cell(0, 10, "Page " + str(self.page_no()), 0, 0, "C")
39
+
40
+ def chapter_title(self, num, label):
41
+ # Set Chinese font
42
+ self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
43
+ self.set_font("SimSun", "", 12)
44
+ # Background color
45
+ self.set_fill_color(200, 220, 255)
46
+ # Title
47
+ # self.cell(0, 6, 'Chapter %d : %s' % (num, label), 0, 1, 'L', 1)
48
+ self.cell(0, 6, "Detection Result:", 0, 1, "L", 1)
49
+ # Line break
50
+ self.ln(4)
51
+
52
+ def chapter_body(self, name):
53
+
54
+ # Set Chinese font
55
+ self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
56
+ self.set_font("SimSun", "", 12)
57
+ # Output justified text
58
+ self.multi_cell(0, 5, name)
59
+ # Line break
60
+ self.ln()
61
+ self.cell(0, 5, "--------------------------------------")
62
+
63
+ def print_chapter(self, num, title, name):
64
+ self.add_page()
65
+ self.chapter_title(num, title)
66
+ self.chapter_body(name)
67
+
68
+
69
+ # pdf generation function
70
+ def pdf_generate(input_file, output_file, title_):
71
+ global title
72
+
73
+ title = title_
74
+ pdf = PDF()
75
+ pdf.set_title(title)
76
+ pdf.set_author("Zeng Yifu")
77
+ pdf.print_chapter(1, "A RUNAWAY REEF", input_file)
78
+ pdf.output(output_file)