Zengyf-CVer commited on
Commit
ad2ecf0
1 Parent(s): d25fdfc

v04 update

Browse files
.gitignore ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ *.data
17
+ *.txt
18
+ *.csv
19
+
20
+ # 参数文件
21
+ *.yaml
22
+ *.json
23
+
24
+ # 压缩文件格式
25
+ *.zip
26
+ *.tar
27
+ *.tar.gz
28
+ *.rar
29
+
30
+ # 字体格式
31
+ *.ttc
32
+ *.ttf
33
+ *.otf
34
+
35
+ *.pt
36
+ *.db
37
+
38
+ /flagged
39
+ /run
40
+ !requirements.txt
41
+ !cls_name/*
42
+ !model_config/*
43
+ !img_example/*
44
+
45
+ app copy.py
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Gradio_YOLOv5_Det_v4
3
- emoji: 🌖
4
  colorFrom: yellow
5
  colorTo: indigo
6
  sdk: gradio
@@ -11,3 +11,5 @@ license: gpl-3.0
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
 
 
1
  ---
2
  title: Gradio_YOLOv5_Det_v4
3
+ emoji: 🚀
4
  colorFrom: yellow
5
  colorTo: indigo
6
  sdk: gradio
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
14
+
15
+ 🚀 Project homepage:https://gitee.com/CV_Lab/gradio_yolov5_det
__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
1
+ __author__ = "曾逸夫(Zeng Yifu)"
2
+ __email__ = "zyfiy1314@163.com"
app.py ADDED
@@ -0,0 +1,615 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gradio YOLOv5 Det v0.4
2
+ # author: Zeng Yifu(曾逸夫)
3
+ # creation time: 2022-05-28
4
+ # email: zyfiy1314@163.com
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="upload", 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):
137
+
138
+ # load model
139
+ model = torch.hub.load(model_path, model_name, force_reload=True, device=device, _verbose=False)
140
+
141
+ return model
142
+
143
+
144
+ # check information
145
+ def export_json(results, img_size):
146
+
147
+ return [[{
148
+ "ID": i,
149
+ "CLASS": int(result[i][5]),
150
+ "CLASS_NAME": model_cls_name_cp[int(result[i][5])],
151
+ "BOUNDING_BOX": {
152
+ "XMIN": round(result[i][:4].tolist()[0], 6),
153
+ "YMIN": round(result[i][:4].tolist()[1], 6),
154
+ "XMAX": round(result[i][:4].tolist()[2], 6),
155
+ "YMAX": round(result[i][:4].tolist()[3], 6),},
156
+ "CONF": round(float(result[i][4]), 2),
157
+ "FPS": round(1000 / float(results.t[1]), 2),
158
+ "IMG_WIDTH": img_size[0],
159
+ "IMG_HEIGHT": img_size[1],} for i in range(len(result))] for result in results.xyxyn]
160
+
161
+
162
+ # frame conversion
163
+ def pil_draw(img, countdown_msg, textFont, xyxy, font_size, opt):
164
+
165
+ img_pil = ImageDraw.Draw(img)
166
+
167
+ img_pil.rectangle(xyxy, fill=None, outline="green") # bounding box
168
+
169
+ if "label" in opt:
170
+ text_w, text_h = textFont.getsize(countdown_msg) # Label size
171
+ img_pil.rectangle(
172
+ (xyxy[0], xyxy[1], xyxy[0] + text_w, xyxy[1] + text_h),
173
+ fill="green",
174
+ outline="green",
175
+ ) # label background
176
+ img_pil.multiline_text(
177
+ (xyxy[0], xyxy[1]),
178
+ countdown_msg,
179
+ fill=(205, 250, 255),
180
+ font=textFont,
181
+ align="center",
182
+ )
183
+
184
+ return img
185
+
186
+
187
+ # YOLOv5 image detection function
188
+ def yolo_det_img(img, device, model_name, infer_size, conf, iou, max_num, model_cls, opt):
189
+
190
+ global model, model_name_tmp, device_tmp
191
+
192
+ # object size num
193
+ s_obj, m_obj, l_obj = 0, 0, 0
194
+ # object area list
195
+ area_obj_all = []
196
+ # cls num stat
197
+ cls_det_stat = []
198
+
199
+ if model_name_tmp != model_name:
200
+ # Model judgment to avoid repeated loading
201
+ model_name_tmp = model_name
202
+ model = model_loading(model_name_tmp, device)
203
+ elif device_tmp != device:
204
+ device_tmp = device
205
+ model = model_loading(model_name_tmp, device)
206
+
207
+ # -------------Model tuning -------------
208
+ model.conf = conf # NMS confidence threshold
209
+ model.iou = iou # NMS IoU threshold
210
+ model.max_det = int(max_num) # Maximum number of detection frames
211
+ model.classes = model_cls # model classes
212
+
213
+ img_size = img.size # frame size
214
+
215
+ results = model(img, size=infer_size) # detection
216
+
217
+ # Data Frame
218
+ dataframe = results.pandas().xyxy[0].round(2)
219
+
220
+ # ----------------Load fonts----------------
221
+ yaml_index = cls_name.index(".yaml")
222
+ cls_name_lang = cls_name[yaml_index - 2:yaml_index]
223
+
224
+ if cls_name_lang == "zh":
225
+ # Chinese
226
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/SimSun.ttf"), size=FONTSIZE)
227
+ elif cls_name_lang in ["en", "ru", "es", "ar"]:
228
+ # English, Russian, Spanish, Arabic
229
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/TimesNewRoman.ttf"), size=FONTSIZE)
230
+ elif cls_name_lang == "ko":
231
+ # Korean
232
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/malgun.ttf"), size=FONTSIZE)
233
+
234
+ for result in results.xyxyn:
235
+ for i in range(len(result)):
236
+ id = int(i) # instance ID
237
+ obj_cls_index = int(result[i][5]) # category index
238
+ obj_cls = model_cls_name_cp[obj_cls_index] # category
239
+ cls_det_stat.append(obj_cls)
240
+
241
+ # ------------ border coordinates ------------
242
+ x0 = float(result[i][:4].tolist()[0])
243
+ y0 = float(result[i][:4].tolist()[1])
244
+ x1 = float(result[i][:4].tolist()[2])
245
+ y1 = float(result[i][:4].tolist()[3])
246
+
247
+ # ------------ Actual coordinates of the border ------------
248
+ x0 = int(img_size[0] * x0)
249
+ y0 = int(img_size[1] * y0)
250
+ x1 = int(img_size[0] * x1)
251
+ y1 = int(img_size[1] * y1)
252
+
253
+ conf = float(result[i][4]) # confidence
254
+ # fps = f"{(1000 / float(results.t[1])):.2f}" # FPS
255
+
256
+ det_img = pil_draw(
257
+ img,
258
+ f"{id}-{obj_cls}:{conf:.2f}",
259
+ textFont,
260
+ [x0, y0, x1, y1],
261
+ FONTSIZE,
262
+ opt,
263
+ )
264
+
265
+ # ----------add object size----------
266
+ w_obj = x1 - x0
267
+ h_obj = y1 - y0
268
+ area_obj = w_obj * h_obj
269
+ area_obj_all.append(area_obj)
270
+
271
+ # ------------JSON generate------------
272
+ det_json = export_json(results, img.size)[0] # Detection information
273
+ det_json_format = json.dumps(det_json, sort_keys=False, indent=4, separators=(",", ":"),
274
+ ensure_ascii=False) # JSON formatting
275
+
276
+ if "json" not in opt:
277
+ det_json = None
278
+
279
+ # -------PDF generate-------
280
+ report = "./Det_Report.pdf"
281
+ if "pdf" in opt:
282
+ pdf_generate(f"{det_json_format}", report, GYD_VERSION)
283
+ else:
284
+ report = None
285
+
286
+ # --------------object size compute--------------
287
+ for i in range(len(area_obj_all)):
288
+ if (0 < area_obj_all[i] <= 32 ** 2):
289
+ s_obj = s_obj + 1
290
+ elif (32 ** 2 < area_obj_all[i] <= 96 ** 2):
291
+ m_obj = m_obj + 1
292
+ elif (area_obj_all[i] > 96 ** 2):
293
+ l_obj = l_obj + 1
294
+
295
+ sml_obj_total = s_obj + m_obj + l_obj
296
+
297
+ objSize_dict = {obj_style[i]: [s_obj, m_obj, l_obj][i] / sml_obj_total for i in range(3)}
298
+
299
+ # ------------cls stat------------
300
+ clsRatio_dict = {}
301
+ clsDet_dict = Counter(cls_det_stat)
302
+ clsDet_dict_sum = sum(clsDet_dict.values())
303
+
304
+ for k, v in clsDet_dict.items():
305
+ clsRatio_dict[k] = v / clsDet_dict_sum
306
+
307
+ return det_img, objSize_dict, clsRatio_dict, det_json, report, dataframe
308
+
309
+
310
+ # YOLOv5 video detection function
311
+ def yolo_det_video(video, device, model_name, infer_size, conf, iou, max_num, model_cls, opt):
312
+
313
+ global model, model_name_tmp, device_tmp
314
+
315
+ os.system("""
316
+ if [ -e './output.mp4' ]; then
317
+ rm ./output.mp4
318
+ fi
319
+ """)
320
+
321
+ if model_name_tmp != model_name:
322
+ # Model judgment to avoid repeated loading
323
+ model_name_tmp = model_name
324
+ model = model_loading(model_name_tmp, device)
325
+ elif device_tmp != device:
326
+ device_tmp = device
327
+ model = model_loading(model_name_tmp, device)
328
+
329
+ # -------------Model tuning -------------
330
+ model.conf = conf # NMS confidence threshold
331
+ model.iou = iou # NMS IOU threshold
332
+ model.max_det = int(max_num) # Maximum number of detection frames
333
+ model.classes = model_cls # model classes
334
+
335
+ # ----------------Load fonts----------------
336
+ yaml_index = cls_name.index(".yaml")
337
+ cls_name_lang = cls_name[yaml_index - 2:yaml_index]
338
+
339
+ if cls_name_lang == "zh":
340
+ # Chinese
341
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/SimSun.ttf"), size=FONTSIZE)
342
+ elif cls_name_lang in ["en", "ru", "es", "ar"]:
343
+ # English, Russian, Spanish, Arabic
344
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/TimesNewRoman.ttf"), size=FONTSIZE)
345
+ elif cls_name_lang == "ko":
346
+ # Korean
347
+ textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/malgun.ttf"), size=FONTSIZE)
348
+
349
+ # video->frame
350
+ gc.collect()
351
+ output_video_path = "./output.avi"
352
+ cap = cv2.VideoCapture(video)
353
+ fourcc = cv2.VideoWriter_fourcc(*"I420") # encoder
354
+
355
+ out = cv2.VideoWriter(output_video_path, fourcc, 30.0, (int(cap.get(3)), int(cap.get(4))))
356
+ while cap.isOpened():
357
+ ret, frame = cap.read()
358
+ # Determine empty frame
359
+ if not ret:
360
+ break
361
+
362
+ frame2 = frame.copy()
363
+ results = model(frame2, size=infer_size) # detection
364
+ h, w, _ = frame.shape # frame size
365
+ img_size = (w, h) # frame size
366
+
367
+ for result in results.xyxyn:
368
+ for i in range(len(result)):
369
+ id = int(i) # instance ID
370
+ obj_cls_index = int(result[i][5]) # category index
371
+ obj_cls = model_cls_name_cp[obj_cls_index] # category
372
+
373
+ # ------------ border coordinates ------------
374
+ x0 = float(result[i][:4].tolist()[0])
375
+ y0 = float(result[i][:4].tolist()[1])
376
+ x1 = float(result[i][:4].tolist()[2])
377
+ y1 = float(result[i][:4].tolist()[3])
378
+
379
+ # ------------ Actual coordinates of the border ------------
380
+ x0 = int(img_size[0] * x0)
381
+ y0 = int(img_size[1] * y0)
382
+ x1 = int(img_size[0] * x1)
383
+ y1 = int(img_size[1] * y1)
384
+
385
+ conf = float(result[i][4]) # confidence
386
+ # fps = f"{(1000 / float(results.t[1])):.2f}" # FPS
387
+
388
+ frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
389
+ frame = pil_draw(
390
+ frame,
391
+ f"{id}-{obj_cls}:{conf:.2f}",
392
+ textFont,
393
+ [x0, y0, x1, y1],
394
+ FONTSIZE,
395
+ opt,
396
+ )
397
+
398
+ frame = cv2.cvtColor(np.asarray(frame), cv2.COLOR_RGB2BGR)
399
+
400
+ # frame->video
401
+ out.write(frame)
402
+ out.release()
403
+ cap.release()
404
+ cv2.destroyAllWindows()
405
+
406
+ return output_video_path
407
+
408
+
409
+ def main(args):
410
+ gr.close_all()
411
+
412
+ global model, model_cls_name_cp, cls_name
413
+
414
+ source = args.source
415
+ source_video = args.source_video
416
+ img_tool = args.img_tool
417
+ nms_conf = args.nms_conf
418
+ nms_iou = args.nms_iou
419
+ model_name = args.model_name
420
+ model_cfg = args.model_cfg
421
+ cls_name = args.cls_name
422
+ device = args.device
423
+ inference_size = args.inference_size
424
+ max_detnum = args.max_detnum
425
+ slider_step = args.slider_step
426
+ is_login = args.is_login
427
+ usr_pwd = args.usr_pwd
428
+ is_share = args.is_share
429
+
430
+ is_fonts(f"{ROOT_PATH}/fonts") # Check font files
431
+
432
+ # model loading
433
+ model = model_loading(model_name, device)
434
+
435
+ model_names = yaml_csv(model_cfg, "model_names") # model names
436
+ model_cls_name = yaml_csv(cls_name, "model_cls_name") # class name
437
+
438
+ model_cls_name_cp = model_cls_name.copy() # class name
439
+
440
+ # ------------------- Input Components -------------------
441
+ inputs_img = gr.Image(image_mode="RGB", source=source, tool=img_tool, type="pil", label="original image")
442
+ inputs_device01 = gr.Radio(choices=["cuda:0", "cpu"], value=device, label="device")
443
+ inputs_model01 = gr.Dropdown(choices=model_names, value=model_name, type="value", label="model")
444
+ inputs_size01 = gr.Radio(choices=[320, 640, 1280], value=inference_size, label="inference size")
445
+ input_conf01 = gr.Slider(0, 1, step=slider_step, value=nms_conf, label="confidence threshold")
446
+ inputs_iou01 = gr.Slider(0, 1, step=slider_step, value=nms_iou, label="IoU threshold")
447
+ inputs_maxnum01 = gr.Number(value=max_detnum, label="Maximum number of detections")
448
+ inputs_clsName01 = gr.CheckboxGroup(choices=model_cls_name, value=model_cls_name, type="index", label="category")
449
+ inputs_opt01 = gr.CheckboxGroup(choices=["label", "pdf", "json"],
450
+ value=["label", "pdf"],
451
+ type="value",
452
+ label="operate")
453
+
454
+ # ------------------- Input Components -------------------
455
+ inputs_video = gr.Video(format="mp4", source=source_video, label="original video") # webcam
456
+ inputs_device02 = gr.Radio(choices=["cuda:0", "cpu"], value=device, label="device")
457
+ inputs_model02 = gr.Dropdown(choices=model_names, value=model_name, type="value", label="model")
458
+ inputs_size02 = gr.Radio(choices=[320, 640, 1280], value=inference_size, label="inference size")
459
+ input_conf02 = gr.Slider(0, 1, step=slider_step, value=nms_conf, label="confidence threshold")
460
+ inputs_iou02 = gr.Slider(0, 1, step=slider_step, value=nms_iou, label="IoU threshold")
461
+ inputs_maxnum02 = gr.Number(value=max_detnum, label="Maximum number of detections")
462
+ inputs_clsName02 = gr.CheckboxGroup(choices=model_cls_name, value=model_cls_name, type="index", label="category")
463
+ inputs_opt02 = gr.CheckboxGroup(choices=["label"], value=["label"], type="value", label="operate")
464
+
465
+ # Input parameters
466
+ inputs_img_list = [
467
+ inputs_img, # input image
468
+ inputs_device01, # device
469
+ inputs_model01, # model
470
+ inputs_size01, # inference size
471
+ input_conf01, # confidence threshold
472
+ inputs_iou01, # IoU threshold
473
+ inputs_maxnum01, # maximum number of detections
474
+ inputs_clsName01, # category
475
+ inputs_opt01, # detect operations
476
+ ]
477
+
478
+ inputs_video_list = [
479
+ inputs_video, # input image
480
+ inputs_device02, # device
481
+ inputs_model02, # model
482
+ inputs_size02, # inference size
483
+ input_conf02, # confidence threshold
484
+ inputs_iou02, # IoU threshold
485
+ inputs_maxnum02, # maximum number of detections
486
+ inputs_clsName02, # category
487
+ inputs_opt02, # detect operation
488
+ ]
489
+
490
+ # -------------------output component-------------------
491
+ outputs_img = gr.Image(type="pil", label="Detection image")
492
+ outputs_json = gr.JSON(label="Detection information")
493
+ outputs_pdf = gr.File(label="Download test report")
494
+ outputs_df = gr.Dataframe(max_rows=5,
495
+ overflow_row_behaviour="paginate",
496
+ type="pandas",
497
+ label="List of detection information")
498
+ outputs_objSize = gr.Label(label="Object size ratio statistics")
499
+ outputs_clsSize = gr.Label(label="Category detection proportion statistics")
500
+
501
+ # -------------------output component-------------------
502
+ outputs_video = gr.Video(format='mp4', label="Detection video")
503
+
504
+ # output parameters
505
+ outputs_img_list = [outputs_img, outputs_objSize, outputs_clsSize, outputs_json, outputs_pdf, outputs_df]
506
+ outputs_video_list = [outputs_video]
507
+
508
+ # title
509
+ title = "Gradio YOLOv5 Det v0.4"
510
+
511
+ # describe
512
+ description = "<div align='center'>Customizable target detection model, easy to install, easy to use</div>"
513
+ # article="https://gitee.com/CV_Lab/gradio_yolov5_det"
514
+
515
+ # example image
516
+ examples = [
517
+ [
518
+ "./img_example/bus.jpg",
519
+ "cpu",
520
+ "yolov5s",
521
+ 640,
522
+ 0.6,
523
+ 0.5,
524
+ 10,
525
+ ["person", "bus"],
526
+ ["label", "pdf"],],
527
+ [
528
+ "./img_example/giraffe.jpg",
529
+ "cpu",
530
+ "yolov5l",
531
+ 320,
532
+ 0.5,
533
+ 0.45,
534
+ 12,
535
+ ["giraffe"],
536
+ ["label", "pdf"],],
537
+ [
538
+ "./img_example/zidane.jpg",
539
+ "cpu",
540
+ "yolov5m",
541
+ 640,
542
+ 0.25,
543
+ 0.5,
544
+ 15,
545
+ ["person", "tie"],
546
+ ["pdf", "json"],],
547
+ [
548
+ "./img_example/Millenial-at-work.jpg",
549
+ "cpu",
550
+ "yolov5s6",
551
+ 1280,
552
+ 0.5,
553
+ 0.5,
554
+ 20,
555
+ ["person", "chair", "cup", "laptop"],
556
+ ["label", "pdf"],],]
557
+
558
+ # interface
559
+ gyd_img = gr.Interface(
560
+ fn=yolo_det_img,
561
+ inputs=inputs_img_list,
562
+ outputs=outputs_img_list,
563
+ title=title,
564
+ description=description,
565
+ # article=article,
566
+ # examples=examples,
567
+ # theme="seafoam",
568
+ # live=True, # Change output in real time
569
+ flagging_dir="run", # output directory
570
+ # allow_flagging="manual",
571
+ # flagging_options=["good", "generally", "bad"],
572
+ )
573
+
574
+ gyd_video = gr.Interface(
575
+ # fn=yolo_det_video_test,
576
+ fn=yolo_det_video,
577
+ inputs=inputs_video_list,
578
+ outputs=outputs_video_list,
579
+ title=title,
580
+ description=description,
581
+ # article=article,
582
+ # examples=examples,
583
+ # theme="seafoam",
584
+ # live=True, # Change output in real time
585
+ flagging_dir="run", # output directory
586
+ allow_flagging="never",
587
+ # flagging_options=["good", "generally", "bad"],
588
+ )
589
+
590
+ gyd = gr.TabbedInterface(interface_list=[gyd_img, gyd_video], tab_names=["Image Mode", "Video Mode"])
591
+
592
+ if not is_login:
593
+ gyd.launch(
594
+ inbrowser=True, # Automatically open default browser
595
+ show_tips=True, # Automatically display the latest features of gradio
596
+ share=is_share, # Project sharing, other devices can access
597
+ favicon_path="./icon/logo.ico", # web icon
598
+ show_error=True, # Display error message in browser console
599
+ quiet=True, # Suppress most print statements
600
+ )
601
+ else:
602
+ gyd.launch(
603
+ inbrowser=True, # Automatically open default browser
604
+ show_tips=True, # Automatically display the latest features of gradio
605
+ auth=usr_pwd, # login interface
606
+ share=is_share, # Project sharing, other devices can access
607
+ favicon_path="./icon/logo.ico", # web icon
608
+ show_error=True, # Display error message in browser console
609
+ quiet=True, # Suppress most print statements
610
+ )
611
+
612
+
613
+ if __name__ == "__main__":
614
+ args = parse_args()
615
+ 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
requirements.txt ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Base ----------------------------------------
2
+ matplotlib>=3.2.2
3
+ numpy>=1.18.5
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
9
+ torch>=1.7.0
10
+ torchvision>=0.8.1
11
+ tqdm>=4.41.0
12
+ wget>=3.2
13
+ rich>=12.2.0
14
+ fpdf>=1.7.2
15
+
16
+ # Logging -------------------------------------
17
+ tensorboard>=2.4.1
18
+ # wandb
19
+
20
+ # Plotting ------------------------------------
21
+ pandas>=1.1.4
22
+ seaborn>=0.11.0
23
+
24
+ # Export --------------------------------------
25
+ # coremltools>=4.1 # CoreML export
26
+ # onnx>=1.9.0 # ONNX export
27
+ # onnx-simplifier>=0.3.6 # ONNX simplifier
28
+ # scikit-learn==0.19.2 # CoreML quantization
29
+ # tensorflow>=2.4.1 # TFLite export
30
+ # tensorflowjs>=3.9.0 # TF.js export
31
+ # openvino-dev # OpenVINO export
32
+
33
+ # Extras --------------------------------------
34
+ # albumentations>=1.0.3
35
+ # Cython # for pycocotools https://github.com/cocodataset/cocoapi/issues/172
36
+ # pycocotools>=2.0 # COCO mAP
37
+ # roboflow
38
+ thop # FLOPs computation
util/fonts_opt.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # font management
2
+ # author: Zeng Yifu(曾逸夫)
3
+ # creation time: 2022-05-01
4
+ # email: zyfiy1314@163.com
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, "Test 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)