Zengyf-CVer commited on
Commit
23aa64f
β€’
1 Parent(s): 8c68295

v03 update

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +69 -29
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: πŸš€
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 3.0.2
8
  app_file: app.py
9
  pinned: true
10
  license: gpl-3.0
 
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 3.0.6
8
  app_file: app.py
9
  pinned: true
10
  license: gpl-3.0
app.py CHANGED
@@ -83,7 +83,28 @@ def parse_args(known=False):
83
  help="cuda or cpu",
84
  )
85
  parser.add_argument("--inference_size", "-isz", default=640, type=int, help="model inference size")
86
- parser.add_argument("--max_detnum", "-mdn", default="50", type=str, help="model max det num")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  args = parser.parse_known_args()[0] if known else parser.parse_args()
89
  return args
@@ -122,24 +143,21 @@ def model_loading(model_name, device):
122
 
123
 
124
  # check information
125
- def export_json(results, model, img_size):
126
-
127
- return [
128
- [
129
- {
130
- "id": i,
131
- "class": int(result[i][5]),
132
- # "class_name": model.model.names[int(result[i][5])],
133
- "class_name": model_cls_name_cp[int(result[i][5])],
134
- "normalized_box": {
135
- "x0": round(result[i][:4].tolist()[0], 6),
136
- "y0": round(result[i][:4].tolist()[1], 6),
137
- "x1": round(result[i][:4].tolist()[2], 6),
138
- "y1": round(result[i][:4].tolist()[3], 6),},
139
- "confidence": round(float(result[i][4]), 2),
140
- "fps": round(1000 / float(results.t[1]), 2),
141
- "width": img_size[0],
142
- "height": img_size[1],} for i in range(len(result))] for result in results.xyxyn]
143
 
144
 
145
  # frame conversion
@@ -168,7 +186,7 @@ def pil_draw(img, countdown_msg, textFont, xyxy, font_size, opt):
168
 
169
 
170
  # YOLOv5 image detection function
171
- def yolo_det(img, device, model_name, inference_size, conf, iou, max_num, model_cls, opt):
172
 
173
  global model, model_name_tmp, device_tmp
174
 
@@ -195,7 +213,7 @@ def yolo_det(img, device, model_name, inference_size, conf, iou, max_num, model_
195
 
196
  img_size = img.size # frame size
197
 
198
- results = model(img, size=inference_size) # detection
199
 
200
  # Data Frame
201
  dataframe = results.pandas().xyxy[0].round(2)
@@ -251,12 +269,14 @@ def yolo_det(img, device, model_name, inference_size, conf, iou, max_num, model_
251
  area_obj = w_obj * h_obj
252
  area_obj_all.append(area_obj)
253
 
 
254
  det_json = export_json(results, model, img.size)[0] # Detection information
255
  det_json_format = json.dumps(det_json, sort_keys=False, indent=4, separators=(",", ":"), ensure_ascii=False) # JSON formatting
 
256
  if "json" not in opt:
257
  det_json = None
258
 
259
- # -------pdf-------
260
  report = "./Det_Report.pdf"
261
  if "pdf" in opt:
262
  pdf_generate(f"{det_json_format}", report, GYD_VERSION)
@@ -293,8 +313,6 @@ def main(args):
293
 
294
  global model, model_cls_name_cp, cls_name
295
 
296
- slider_step = 0.05 # sliding step
297
-
298
  source = args.source
299
  img_tool = args.img_tool
300
  nms_conf = args.nms_conf
@@ -305,6 +323,10 @@ def main(args):
305
  device = args.device
306
  inference_size = args.inference_size
307
  max_detnum = args.max_detnum
 
 
 
 
308
 
309
  is_fonts(f"{ROOT_PATH}/fonts") # Check font files
310
 
@@ -358,6 +380,7 @@ def main(args):
358
 
359
  # describe
360
  description = "<div align='center'>Customizable target detection model, easy to install, easy to use</div>"
 
361
 
362
  # example image
363
  examples = [
@@ -403,21 +426,38 @@ def main(args):
403
  ["label", "pdf"],],]
404
 
405
  # interface
406
- gr.Interface(
407
  fn=yolo_det,
408
  inputs=inputs,
409
  outputs=outputs,
410
  title=title,
411
  description=description,
412
- article="",
413
  # examples=examples,
414
  # theme="seafoam",
415
  # flagging_dir="run", # output directory
416
- ).launch(
417
- inbrowser=True, # Automatically open default browser
418
- show_tips=True, # Automatically display the latest features of gradio
419
  )
420
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421
 
422
  if __name__ == "__main__":
423
  args = parse_args()
 
83
  help="cuda or cpu",
84
  )
85
  parser.add_argument("--inference_size", "-isz", default=640, type=int, help="model inference size")
86
+ parser.add_argument("--max_detnum", "-mdn", default=50, type=float, help="model max det num")
87
+ parser.add_argument("--slider_step", "-ss", default=0.05, type=float, help="slider step")
88
+ parser.add_argument(
89
+ "--is_login",
90
+ "-isl",
91
+ action="store_true",
92
+ default=False,
93
+ help="is login",
94
+ )
95
+ parser.add_argument('--usr_pwd',
96
+ "-up",
97
+ nargs='+',
98
+ type=str,
99
+ default=["admin", "admin"],
100
+ help="user & password for login")
101
+ parser.add_argument(
102
+ "--is_share",
103
+ "-is",
104
+ action="store_true",
105
+ default=False,
106
+ help="is login",
107
+ )
108
 
109
  args = parser.parse_known_args()[0] if known else parser.parse_args()
110
  return args
 
143
 
144
 
145
  # check information
146
+ def export_json(results, img_size):
147
+
148
+ return [[{
149
+ "ID": i,
150
+ "CLASS": int(result[i][5]),
151
+ "CLASS_NAME": model_cls_name_cp[int(result[i][5])],
152
+ "BOUNDING_BOX": {
153
+ "XMIN": round(result[i][:4].tolist()[0], 6),
154
+ "YMIN": round(result[i][:4].tolist()[1], 6),
155
+ "XMAX": round(result[i][:4].tolist()[2], 6),
156
+ "YMAX": round(result[i][:4].tolist()[3], 6),},
157
+ "CONF": round(float(result[i][4]), 2),
158
+ "FPS": round(1000 / float(results.t[1]), 2),
159
+ "IMG_WIDTH": img_size[0],
160
+ "IMG_HEIGHT": img_size[1],} for i in range(len(result))] for result in results.xyxyn]
 
 
 
161
 
162
 
163
  # frame conversion
 
186
 
187
 
188
  # YOLOv5 image detection function
189
+ def yolo_det(img, device, model_name, infer_size, conf, iou, max_num, model_cls, opt):
190
 
191
  global model, model_name_tmp, device_tmp
192
 
 
213
 
214
  img_size = img.size # frame size
215
 
216
+ results = model(img, size=infer_size) # detection
217
 
218
  # Data Frame
219
  dataframe = results.pandas().xyxy[0].round(2)
 
269
  area_obj = w_obj * h_obj
270
  area_obj_all.append(area_obj)
271
 
272
+ # ------------JSON generate------------
273
  det_json = export_json(results, model, img.size)[0] # Detection information
274
  det_json_format = json.dumps(det_json, sort_keys=False, indent=4, separators=(",", ":"), 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)
 
313
 
314
  global model, model_cls_name_cp, cls_name
315
 
 
 
316
  source = args.source
317
  img_tool = args.img_tool
318
  nms_conf = args.nms_conf
 
323
  device = args.device
324
  inference_size = args.inference_size
325
  max_detnum = args.max_detnum
326
+ slider_step = args.slider_step
327
+ is_login = args.is_login
328
+ usr_pwd = args.usr_pwd
329
+ is_share = args.is_share
330
 
331
  is_fonts(f"{ROOT_PATH}/fonts") # Check font files
332
 
 
380
 
381
  # describe
382
  description = "<div align='center'>Customizable target detection model, easy to install, easy to use</div>"
383
+ # article="https://gitee.com/CV_Lab/gradio_yolov5_det"
384
 
385
  # example image
386
  examples = [
 
426
  ["label", "pdf"],],]
427
 
428
  # interface
429
+ gyd = gr.Interface(
430
  fn=yolo_det,
431
  inputs=inputs,
432
  outputs=outputs,
433
  title=title,
434
  description=description,
435
+ # article=article,
436
  # examples=examples,
437
  # theme="seafoam",
438
  # flagging_dir="run", # output directory
 
 
 
439
  )
440
 
441
+ if not is_login:
442
+ gyd.launch(
443
+ inbrowser=True, # Automatically open default browser
444
+ show_tips=True, # Automatically display the latest features of gradio
445
+ share=is_share, # Project sharing, other devices can access
446
+ favicon_path="./icon/logo.ico", # web icon
447
+ show_error=True, # Display error message in browser console
448
+ quiet=True, # Suppress most print statements
449
+ )
450
+ else:
451
+ gyd.launch(
452
+ inbrowser=True, # Automatically open default browser
453
+ show_tips=True, # Automatically display the latest features of gradio
454
+ auth=usr_pwd, # login interface
455
+ share=is_share, # Project sharing, other devices can access
456
+ favicon_path="./icon/logo.ico", # web icon
457
+ show_error=True, # Display error message in browser console
458
+ quiet=True, # Suppress most print statements
459
+ )
460
+
461
 
462
  if __name__ == "__main__":
463
  args = parse_args()