Spaces:
Runtime error
Runtime error
Commit
·
e021555
1
Parent(s):
321feeb
v04 update
Browse files- app.py +222 -68
- requirements.txt +11 -5
app.py
CHANGED
|
@@ -1,19 +1,22 @@
|
|
| 1 |
-
# Gradio YOLOv5 Det v0.
|
| 2 |
# author: Zeng Yifu(曾逸夫)
|
| 3 |
-
# creation time: 2022-05-
|
| 4 |
# email: [email protected]
|
| 5 |
# project homepage: https://gitee.com/CV_Lab/gradio_yolov5_det
|
| 6 |
|
| 7 |
-
|
| 8 |
import argparse
|
| 9 |
import csv
|
|
|
|
| 10 |
import json
|
|
|
|
| 11 |
import sys
|
| 12 |
from collections import Counter
|
| 13 |
from pathlib import Path
|
| 14 |
-
import pandas as pd
|
| 15 |
|
|
|
|
| 16 |
import gradio as gr
|
|
|
|
|
|
|
| 17 |
import torch
|
| 18 |
import yaml
|
| 19 |
from PIL import Image, ImageDraw, ImageFont
|
|
@@ -21,13 +24,13 @@ from PIL import Image, ImageDraw, ImageFont
|
|
| 21 |
from util.fonts_opt import is_fonts
|
| 22 |
from util.pdf_opt import pdf_generate
|
| 23 |
|
| 24 |
-
ROOT_PATH = sys.path[0]
|
| 25 |
|
| 26 |
# model path
|
| 27 |
model_path = "ultralytics/yolov5"
|
| 28 |
|
| 29 |
# Gradio YOLOv5 Det version
|
| 30 |
-
GYD_VERSION = "Gradio YOLOv5 Det v0.
|
| 31 |
|
| 32 |
# model name temporary variable
|
| 33 |
model_name_tmp = ""
|
|
@@ -46,8 +49,9 @@ obj_style = ["Small Object", "Medium Object", "Large Object"]
|
|
| 46 |
|
| 47 |
|
| 48 |
def parse_args(known=False):
|
| 49 |
-
parser = argparse.ArgumentParser(description="Gradio YOLOv5 Det v0.
|
| 50 |
parser.add_argument("--source", "-src", default="upload", type=str, help="input source")
|
|
|
|
| 51 |
parser.add_argument("--img_tool", "-it", default="editor", type=str, help="input image tool")
|
| 52 |
parser.add_argument("--model_name", "-mn", default="yolov5s", type=str, help="model name")
|
| 53 |
parser.add_argument(
|
|
@@ -117,10 +121,10 @@ def yaml_csv(file_path, file_tag):
|
|
| 117 |
file_suffix = Path(file_path).suffix
|
| 118 |
if file_suffix == suffix_list[0]:
|
| 119 |
# model name
|
| 120 |
-
file_names = [i[0] for i in list(csv.reader(open(file_path)))]
|
| 121 |
elif file_suffix == suffix_list[1]:
|
| 122 |
# model name
|
| 123 |
-
file_names = yaml_parse(file_path).get(file_tag)
|
| 124 |
else:
|
| 125 |
print(f"{file_path} is not in the correct format! Program exits!")
|
| 126 |
sys.exit()
|
|
@@ -132,9 +136,7 @@ def yaml_csv(file_path, file_tag):
|
|
| 132 |
def model_loading(model_name, device):
|
| 133 |
|
| 134 |
# load model
|
| 135 |
-
model = torch.hub.load(
|
| 136 |
-
model_path, model_name, force_reload=True, device=device, _verbose=False
|
| 137 |
-
)
|
| 138 |
|
| 139 |
return model
|
| 140 |
|
|
@@ -162,15 +164,15 @@ def pil_draw(img, countdown_msg, textFont, xyxy, font_size, opt):
|
|
| 162 |
|
| 163 |
img_pil = ImageDraw.Draw(img)
|
| 164 |
|
| 165 |
-
img_pil.rectangle(xyxy, fill=None, outline="green")
|
| 166 |
|
| 167 |
if "label" in opt:
|
| 168 |
-
text_w, text_h = textFont.getsize(countdown_msg)
|
| 169 |
img_pil.rectangle(
|
| 170 |
(xyxy[0], xyxy[1], xyxy[0] + text_w, xyxy[1] + text_h),
|
| 171 |
fill="green",
|
| 172 |
outline="green",
|
| 173 |
-
)
|
| 174 |
img_pil.multiline_text(
|
| 175 |
(xyxy[0], xyxy[1]),
|
| 176 |
countdown_msg,
|
|
@@ -183,7 +185,7 @@ def pil_draw(img, countdown_msg, textFont, xyxy, font_size, opt):
|
|
| 183 |
|
| 184 |
|
| 185 |
# YOLOv5 image detection function
|
| 186 |
-
def
|
| 187 |
|
| 188 |
global model, model_name_tmp, device_tmp
|
| 189 |
|
|
@@ -203,15 +205,15 @@ def yolo_det(img, device, model_name, infer_size, conf, iou, max_num, model_cls,
|
|
| 203 |
model = model_loading(model_name_tmp, device)
|
| 204 |
|
| 205 |
# -------------Model tuning -------------
|
| 206 |
-
model.conf = conf
|
| 207 |
-
model.iou = iou
|
| 208 |
-
model.max_det = int(max_num)
|
| 209 |
-
model.classes = model_cls
|
| 210 |
-
|
| 211 |
-
img_size = img.size
|
| 212 |
-
|
| 213 |
-
results = model(img, size=infer_size)
|
| 214 |
-
|
| 215 |
# Data Frame
|
| 216 |
dataframe = results.pandas().xyxy[0].round(2)
|
| 217 |
|
|
@@ -231,9 +233,9 @@ def yolo_det(img, device, model_name, infer_size, conf, iou, max_num, model_cls,
|
|
| 231 |
|
| 232 |
for result in results.xyxyn:
|
| 233 |
for i in range(len(result)):
|
| 234 |
-
id = int(i)
|
| 235 |
-
obj_cls_index = int(result[i][5])
|
| 236 |
-
obj_cls = model_cls_name_cp[obj_cls_index]
|
| 237 |
cls_det_stat.append(obj_cls)
|
| 238 |
|
| 239 |
# ------------ border coordinates ------------
|
|
@@ -248,7 +250,7 @@ def yolo_det(img, device, model_name, infer_size, conf, iou, max_num, model_cls,
|
|
| 248 |
x1 = int(img_size[0] * x1)
|
| 249 |
y1 = int(img_size[1] * y1)
|
| 250 |
|
| 251 |
-
conf = float(result[i][4])
|
| 252 |
# fps = f"{(1000 / float(results.t[1])):.2f}" # FPS
|
| 253 |
|
| 254 |
det_img = pil_draw(
|
|
@@ -267,9 +269,10 @@ def yolo_det(img, device, model_name, infer_size, conf, iou, max_num, model_cls,
|
|
| 267 |
area_obj_all.append(area_obj)
|
| 268 |
|
| 269 |
# ------------JSON generate------------
|
| 270 |
-
det_json = export_json(results, img.size)[0]
|
| 271 |
-
det_json_format = json.dumps(det_json, sort_keys=False, indent=4, separators=(",", ":"),
|
| 272 |
-
|
|
|
|
| 273 |
if "json" not in opt:
|
| 274 |
det_json = None
|
| 275 |
|
|
@@ -301,16 +304,115 @@ def yolo_det(img, device, model_name, infer_size, conf, iou, max_num, model_cls,
|
|
| 301 |
for k, v in clsDet_dict.items():
|
| 302 |
clsRatio_dict[k] = v / clsDet_dict_sum
|
| 303 |
|
| 304 |
-
|
| 305 |
return det_img, objSize_dict, clsRatio_dict, det_json, report, dataframe
|
| 306 |
|
| 307 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
def main(args):
|
| 309 |
gr.close_all()
|
| 310 |
|
| 311 |
global model, model_cls_name_cp, cls_name
|
| 312 |
|
| 313 |
source = args.source
|
|
|
|
| 314 |
img_tool = args.img_tool
|
| 315 |
nms_conf = args.nms_conf
|
| 316 |
nms_iou = args.nms_iou
|
|
@@ -325,55 +427,86 @@ def main(args):
|
|
| 325 |
usr_pwd = args.usr_pwd
|
| 326 |
is_share = args.is_share
|
| 327 |
|
| 328 |
-
is_fonts(f"{ROOT_PATH}/fonts")
|
| 329 |
|
| 330 |
# model loading
|
| 331 |
model = model_loading(model_name, device)
|
| 332 |
|
| 333 |
-
model_names = yaml_csv(model_cfg, "model_names")
|
| 334 |
-
model_cls_name = yaml_csv(cls_name, "model_cls_name")
|
| 335 |
|
| 336 |
-
model_cls_name_cp = model_cls_name.copy()
|
| 337 |
|
| 338 |
# ------------------- Input Components -------------------
|
| 339 |
inputs_img = gr.Image(image_mode="RGB", source=source, tool=img_tool, type="pil", label="original image")
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
# Input parameters
|
| 353 |
-
|
| 354 |
-
inputs_img,
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
]
|
| 364 |
|
| 365 |
-
#
|
| 366 |
outputs_img = gr.Image(type="pil", label="Detection image")
|
| 367 |
outputs_json = gr.JSON(label="Detection information")
|
| 368 |
outputs_pdf = gr.File(label="Download test report")
|
| 369 |
-
outputs_df = gr.Dataframe(max_rows=5,
|
|
|
|
|
|
|
|
|
|
| 370 |
outputs_objSize = gr.Label(label="Object size ratio statistics")
|
| 371 |
outputs_clsSize = gr.Label(label="Category detection proportion statistics")
|
| 372 |
|
| 373 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
# title
|
| 376 |
-
title = "Gradio YOLOv5 Det v0.
|
| 377 |
|
| 378 |
# describe
|
| 379 |
description = "<div align='center'>Customizable target detection model, easy to install, easy to use</div>"
|
|
@@ -423,18 +556,39 @@ def main(args):
|
|
| 423 |
["label", "pdf"],],]
|
| 424 |
|
| 425 |
# interface
|
| 426 |
-
|
| 427 |
-
fn=
|
| 428 |
-
inputs=
|
| 429 |
-
outputs=
|
| 430 |
title=title,
|
| 431 |
description=description,
|
| 432 |
# article=article,
|
| 433 |
# examples=examples,
|
| 434 |
# theme="seafoam",
|
| 435 |
-
#
|
|
|
|
|
|
|
|
|
|
| 436 |
)
|
| 437 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
if not is_login:
|
| 439 |
gyd.launch(
|
| 440 |
inbrowser=True, # Automatically open default browser
|
|
@@ -458,4 +612,4 @@ def main(args):
|
|
| 458 |
|
| 459 |
if __name__ == "__main__":
|
| 460 |
args = parse_args()
|
| 461 |
-
main(args)
|
|
|
|
| 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
|
|
|
|
| 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 = ""
|
|
|
|
| 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(
|
|
|
|
| 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()
|
|
|
|
| 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 |
|
|
|
|
| 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,
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
|
|
|
| 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 ------------
|
|
|
|
| 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(
|
|
|
|
| 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 |
|
|
|
|
| 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
|
|
|
|
| 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>"
|
|
|
|
| 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
|
|
|
|
| 612 |
|
| 613 |
if __name__ == "__main__":
|
| 614 |
args = parse_args()
|
| 615 |
+
main(args)
|
requirements.txt
CHANGED
|
@@ -1,17 +1,22 @@
|
|
| 1 |
# Base ----------------------------------------
|
| 2 |
matplotlib>=3.2.2
|
| 3 |
-
numpy>=1.
|
| 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
|
|
@@ -31,8 +36,9 @@ seaborn>=0.11.0
|
|
| 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
|
|
|
|
| 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 |
|
| 21 |
# Logging -------------------------------------
|
| 22 |
tensorboard>=2.4.1
|
|
|
|
| 36 |
# openvino-dev # OpenVINO export
|
| 37 |
|
| 38 |
# Extras --------------------------------------
|
| 39 |
+
ipython # interactive notebook
|
| 40 |
+
psutil # system utilization
|
| 41 |
+
thop # FLOPs computation
|
| 42 |
# albumentations>=1.0.3
|
|
|
|
| 43 |
# pycocotools>=2.0 # COCO mAP
|
| 44 |
+
# roboflow
|
|
|