File size: 14,716 Bytes
0e4f466
579aa53
 
 
 
0e4f466
 
 
 
 
 
12da3ca
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fb23cc
 
 
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64c4b90
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
 
579aa53
0e4f466
 
 
 
23aa64f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23aa64f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23aa64f
0e4f466
 
 
3fb23cc
 
 
 
12da3ca
 
3fb23cc
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
12da3ca
 
0e4f466
23aa64f
0e4f466
12da3ca
0e4f466
 
 
 
 
 
64c4b90
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
 
65a0495
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fb23cc
 
 
 
 
0e4f466
23aa64f
840bf4d
3fb23cc
23aa64f
3fb23cc
 
0e4f466
23aa64f
0e4f466
 
 
 
 
 
3fb23cc
 
 
 
 
 
 
 
 
 
 
 
0e4f466
12da3ca
 
 
 
 
 
 
 
 
 
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23aa64f
 
 
 
0e4f466
 
 
 
 
 
 
 
 
 
 
 
3801039
65fff86
 
 
 
 
8c68295
65fff86
3801039
65fff86
0e4f466
f78a2dd
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3801039
 
 
 
 
 
0e4f466
12da3ca
0e4f466
 
f78a2dd
0e4f466
 
 
23aa64f
0e4f466
 
 
 
 
 
 
 
 
 
 
 
 
 
3c609fb
0e4f466
 
 
 
 
 
3c609fb
0e4f466
 
 
 
 
 
8c62491
0e4f466
 
 
 
 
3c609fb
0e4f466
 
 
 
 
 
3c609fb
0e4f466
 
 
23aa64f
0e4f466
 
 
 
 
23aa64f
fb94c33
3801039
 
0e4f466
 
23aa64f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e4f466
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# Gradio YOLOv5 Det v0.3
# author: Zeng Yifu(曾逸夫)
# creation time: 2022-05-09
# email: zyfiy1314@163.com
# project homepage: https://gitee.com/CV_Lab/gradio_yolov5_det


import argparse
import csv
import json
import sys
from collections import Counter
from pathlib import Path
import pandas as pd

import gradio as gr
import torch
import yaml
from PIL import Image, ImageDraw, ImageFont

from util.fonts_opt import is_fonts
from util.pdf_opt import pdf_generate

ROOT_PATH = sys.path[0] # root directory

# model path
model_path = "ultralytics/yolov5"

# Gradio YOLOv5 Det version
GYD_VERSION = "Gradio YOLOv5 Det v0.3"

# model name temporary variable
model_name_tmp = ""

# Device temporary variables
device_tmp = ""

# File extension
suffix_list = [".csv", ".yaml"]

# font size
FONTSIZE = 25

# object style
obj_style = ["Small Object", "Medium Object", "Large Object"]


def parse_args(known=False):
    parser = argparse.ArgumentParser(description="Gradio YOLOv5 Det v0.3")
    parser.add_argument("--source", "-src", default="upload", type=str, help="input source")
    parser.add_argument("--img_tool", "-it", default="editor", type=str, help="input image tool")
    parser.add_argument("--model_name", "-mn", default="yolov5s", type=str, help="model name")
    parser.add_argument(
        "--model_cfg",
        "-mc",
        default="./model_config/model_name_p5_p6_all.yaml",
        type=str,
        help="model config",
    )
    parser.add_argument(
        "--cls_name",
        "-cls",
        default="./cls_name/cls_name_en.yaml",
        type=str,
        help="cls name",
    )
    parser.add_argument(
        "--nms_conf",
        "-conf",
        default=0.5,
        type=float,
        help="model NMS confidence threshold",
    )
    parser.add_argument("--nms_iou", "-iou", default=0.45, type=float, help="model NMS IoU threshold")
    parser.add_argument(
        "--device",
        "-dev",
        default="cpu",
        type=str,
        help="cuda or cpu",
    )
    parser.add_argument("--inference_size", "-isz", default=640, type=int, help="model inference size")
    parser.add_argument("--max_detnum", "-mdn", default=50, type=float, help="model max det num")
    parser.add_argument("--slider_step", "-ss", default=0.05, type=float, help="slider step")
    parser.add_argument(
        "--is_login",
        "-isl",
        action="store_true",
        default=False,
        help="is login",
    )
    parser.add_argument('--usr_pwd',
                        "-up",
                        nargs='+',
                        type=str,
                        default=["admin", "admin"],
                        help="user & password for login")
    parser.add_argument(
        "--is_share",
        "-is",
        action="store_true",
        default=False,
        help="is login",
    )

    args = parser.parse_known_args()[0] if known else parser.parse_args()
    return args


# yaml file parsing
def yaml_parse(file_path):
    return yaml.safe_load(open(file_path, encoding="utf-8").read())


# yaml csv file parsing
def yaml_csv(file_path, file_tag):
    file_suffix = Path(file_path).suffix
    if file_suffix == suffix_list[0]:
        # model name
        file_names = [i[0] for i in list(csv.reader(open(file_path)))] # csv version
    elif file_suffix == suffix_list[1]:
        # model name
        file_names = yaml_parse(file_path).get(file_tag) # yaml version
    else:
        print(f"{file_path} is not in the correct format! Program exits!")
        sys.exit()

    return file_names


# model loading
def model_loading(model_name, device):

    # load model
    model = torch.hub.load(
        model_path, model_name, force_reload=True, device=device, _verbose=False
    )

    return model


# check information
def export_json(results, img_size):

    return [[{
        "ID": i,
        "CLASS": int(result[i][5]),
        "CLASS_NAME": model_cls_name_cp[int(result[i][5])],
        "BOUNDING_BOX": {
            "XMIN": round(result[i][:4].tolist()[0], 6),
            "YMIN": round(result[i][:4].tolist()[1], 6),
            "XMAX": round(result[i][:4].tolist()[2], 6),
            "YMAX": round(result[i][:4].tolist()[3], 6),},
        "CONF": round(float(result[i][4]), 2),
        "FPS": round(1000 / float(results.t[1]), 2),
        "IMG_WIDTH": img_size[0],
        "IMG_HEIGHT": img_size[1],} for i in range(len(result))] for result in results.xyxyn]


# frame conversion
def pil_draw(img, countdown_msg, textFont, xyxy, font_size, opt):

    img_pil = ImageDraw.Draw(img)

    img_pil.rectangle(xyxy, fill=None, outline="green") # bounding box

    if "label" in opt:
        text_w, text_h = textFont.getsize(countdown_msg) # Label size
        img_pil.rectangle(
            (xyxy[0], xyxy[1], xyxy[0] + text_w, xyxy[1] + text_h),
            fill="green",
            outline="green",
        ) # label background
        img_pil.multiline_text(
            (xyxy[0], xyxy[1]),
            countdown_msg,
            fill=(205, 250, 255),
            font=textFont,
            align="center",
        )

    return img


# YOLOv5 image detection function
def yolo_det(img, device, model_name, infer_size, conf, iou, max_num, model_cls, opt):

    global model, model_name_tmp, device_tmp

    # object size num
    s_obj, m_obj, l_obj = 0, 0, 0
    # object area list
    area_obj_all = []
    # cls num stat
    cls_det_stat = []

    if model_name_tmp != model_name:
        # Model judgment to avoid repeated loading
        model_name_tmp = model_name
        model = model_loading(model_name_tmp, device)
    elif device_tmp != device:
        device_tmp = device
        model = model_loading(model_name_tmp, device)

    # -------------Model tuning -------------
    model.conf = conf # NMS confidence threshold
    model.iou = iou # NMS IoU threshold
    model.max_det = int(max_num) # Maximum number of detection frames
    model.classes = model_cls # model classes
    
    img_size = img.size # frame size

    results = model(img, size=infer_size) # detection
    
    # Data Frame
    dataframe = results.pandas().xyxy[0].round(2)

    # ----------------Load fonts----------------
    yaml_index = cls_name.index(".yaml")
    cls_name_lang = cls_name[yaml_index - 2:yaml_index]

    if cls_name_lang == "zh":
        # Chinese
        textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/SimSun.ttf"), size=FONTSIZE)
    elif cls_name_lang in ["en", "ru", "es", "ar"]:
        # English, Russian, Spanish, Arabic
        textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/TimesNewRoman.ttf"), size=FONTSIZE)
    elif cls_name_lang == "ko":
        # Korean
        textFont = ImageFont.truetype(str(f"{ROOT_PATH}/fonts/malgun.ttf"), size=FONTSIZE)

    for result in results.xyxyn:
        for i in range(len(result)):
            id = int(i) # instance ID
            obj_cls_index = int(result[i][5]) # category index
            obj_cls = model_cls_name_cp[obj_cls_index] # category
            cls_det_stat.append(obj_cls)

            # ------------ border coordinates ------------
            x0 = float(result[i][:4].tolist()[0])
            y0 = float(result[i][:4].tolist()[1])
            x1 = float(result[i][:4].tolist()[2])
            y1 = float(result[i][:4].tolist()[3])

            # ------------ Actual coordinates of the border ------------
            x0 = int(img_size[0] * x0)
            y0 = int(img_size[1] * y0)
            x1 = int(img_size[0] * x1)
            y1 = int(img_size[1] * y1)

            conf = float(result[i][4]) # confidence
            # fps = f"{(1000 / float(results.t[1])):.2f}" # FPS

            det_img = pil_draw(
                img,
                f"{id}-{obj_cls}:{conf:.2f}",
                textFont,
                [x0, y0, x1, y1],
                FONTSIZE,
                opt,
            )

            # ----------add object size----------
            w_obj = x1 - x0
            h_obj = y1 - y0
            area_obj = w_obj * h_obj
            area_obj_all.append(area_obj)

    # ------------JSON generate------------
    det_json = export_json(results, img.size)[0] # Detection information
    det_json_format = json.dumps(det_json, sort_keys=False, indent=4, separators=(",", ":"), ensure_ascii=False) # JSON formatting
    
    if "json" not in opt:
        det_json = None

    # -------PDF generate-------
    report = "./Det_Report.pdf"
    if "pdf" in opt:
        pdf_generate(f"{det_json_format}", report, GYD_VERSION)
    else:
        report = None

    # --------------object size compute--------------
    for i in range(len(area_obj_all)):
        if (0 < area_obj_all[i] <= 32 ** 2):
            s_obj = s_obj + 1
        elif (32 ** 2 < area_obj_all[i] <= 96 ** 2):
            m_obj = m_obj + 1
        elif (area_obj_all[i] > 96 ** 2):
            l_obj = l_obj + 1

    sml_obj_total = s_obj + m_obj + l_obj

    objSize_dict = {obj_style[i]: [s_obj, m_obj, l_obj][i] / sml_obj_total for i in range(3)}

    # ------------cls stat------------
    clsRatio_dict = {}
    clsDet_dict = Counter(cls_det_stat)
    clsDet_dict_sum = sum(clsDet_dict.values())

    for k, v in clsDet_dict.items():
        clsRatio_dict[k] = v / clsDet_dict_sum


    return det_img, objSize_dict, clsRatio_dict, det_json, report, dataframe


def main(args):
    gr.close_all()

    global model, model_cls_name_cp, cls_name

    source = args.source
    img_tool = args.img_tool
    nms_conf = args.nms_conf
    nms_iou = args.nms_iou
    model_name = args.model_name
    model_cfg = args.model_cfg
    cls_name = args.cls_name
    device = args.device
    inference_size = args.inference_size
    max_detnum = args.max_detnum
    slider_step = args.slider_step
    is_login = args.is_login
    usr_pwd = args.usr_pwd
    is_share = args.is_share

    is_fonts(f"{ROOT_PATH}/fonts") # Check font files

    # model loading
    model = model_loading(model_name, device)

    model_names = yaml_csv(model_cfg, "model_names") # model names
    model_cls_name = yaml_csv(cls_name, "model_cls_name") # class name

    model_cls_name_cp = model_cls_name.copy() # class name

    # ------------------- Input Components -------------------
    inputs_img = gr.Image(image_mode="RGB", source=source, tool=img_tool, type="pil", label="original image")
    inputs_device = gr.Radio(choices=["cuda:0", "cpu"], value=device, label="device")
    inputs_model = gr.Dropdown(choices=model_names, value=model_name, type="value", label="model")
    inputs_size = gr.Radio(choices=[320, 640, 1280], value=inference_size, label="inference size")
    input_conf = gr.Slider(0, 1, step=slider_step, value=nms_conf, label="confidence threshold")
    inputs_iou = gr.Slider(0, 1, step=slider_step, value=nms_iou, label="IoU threshold")
    inputs_maxnum = gr.Number(value=max_detnum, label="Maximum number of detections")
    inputs_clsName = gr.CheckboxGroup(choices=model_cls_name, value=model_cls_name, type="index", label="category")
    inputs_opt = gr.CheckboxGroup(choices=["label", "pdf", "json"],
                                         value=["label", "pdf"],
                                         type="value",
                                         label="operate")

    # Input parameters
    inputs = [
        inputs_img, # input image
        inputs_device, # device
        inputs_model, # model
        inputs_size, # inference size
        input_conf, # confidence threshold
        inputs_iou, # IoU threshold
        inputs_maxnum, # maximum number of detections
        inputs_clsName, # category
        inputs_opt, # detect operations
    ]

    # Output parameters
    outputs_img = gr.Image(type="pil", label="Detection image")
    outputs_json = gr.JSON(label="Detection information")
    outputs_pdf = gr.File(label="Download test report")
    outputs_df = gr.Dataframe(max_rows=5, overflow_row_behaviour="paginate", type="pandas", label="List of detection information")
    outputs_objSize = gr.Label(label="Object size ratio statistics")
    outputs_clsSize = gr.Label(label="Category detection proportion statistics")

    outputs = [outputs_img, outputs_objSize, outputs_clsSize, outputs_json, outputs_pdf, outputs_df]

    # title
    title = "Gradio YOLOv5 Det v0.3"

    # describe
    description = "<div align='center'>Customizable target detection model, easy to install, easy to use</div>"
    # article="https://gitee.com/CV_Lab/gradio_yolov5_det"

    # example image
    examples = [
        [
            "./img_example/bus.jpg",
            "cpu",
            "yolov5s",
            640,
            0.6,
            0.5,
            10,
            ["person", "bus"],
            ["label", "pdf"],],
        [
            "./img_example/giraffe.jpg",
            "cpu",
            "yolov5l",
            320,
            0.5,
            0.45,
            12,
            ["giraffe"],
            ["label", "pdf"],],
        [
            "./img_example/zidane.jpg",
            "cpu",
            "yolov5m",
            640,
            0.6,
            0.5,
            15,
            ["person", "tie"],
            ["pdf", "json"],],
        [
            "./img_example/Millenial-at-work.jpg",
            "cpu",
            "yolov5s6",
            1280,
            0.5,
            0.5,
            20,
            ["person", "chair", "cup", "laptop"],
            ["label", "pdf"],],]

    # interface
    gyd = gr.Interface(
        fn=yolo_det,
        inputs=inputs,
        outputs=outputs,
        title=title,
        description=description,
        # article=article,
        # examples=examples,
        # theme="seafoam",
        # flagging_dir="run", # output directory
    )

    if not is_login:
        gyd.launch(
            inbrowser=True,  # Automatically open default browser
            show_tips=True,  # Automatically display the latest features of gradio
            share=is_share,  # Project sharing, other devices can access
            favicon_path="./icon/logo.ico",  # web icon
            show_error=True,  # Display error message in browser console
            quiet=True,  # Suppress most print statements
        )
    else:
        gyd.launch(
            inbrowser=True,  # Automatically open default browser
            show_tips=True,  # Automatically display the latest features of gradio
            auth=usr_pwd,  # login interface
            share=is_share,  # Project sharing, other devices can access
            favicon_path="./icon/logo.ico",  # web icon
            show_error=True,  # Display error message in browser console
            quiet=True,  # Suppress most print statements
        )


if __name__ == "__main__":
    args = parse_args()
    main(args)