城管三模型代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.9KB

  1. import os
  2. import time
  3. import cv2
  4. import torch
  5. from DMPRUtils.DMPR_process import DMPR_process, plot_points
  6. from DMPRUtils.model.detector import DirectionalPointDetector
  7. from DMPRUtils.yolo_net import Model
  8. from DMPR_YOLO.jointUtil import dmpr_yolo
  9. from STDCUtils.STDC_process import STDC_process
  10. from STDCUtils.models.model_stages import BiSeNet
  11. from STDC_YOLO.yolo_stdc_joint import stdc_yolo
  12. from conf import config
  13. from models.experimental import attempt_load
  14. from models.yolo_process import yolo_process
  15. from utils.plots import plot_one_box
  16. from utils.torch_utils import select_device
  17. def main():
  18. ##预先设置的参数
  19. device_ = '0' ##选定模型,可选 cpu,'0','1'
  20. ##以下参数目前不可改
  21. Detweights = 'weights/urbanManagement/yolo/best1023.pt'
  22. seg_nclass = 2
  23. DMPRweights = "weights/urbanManagement/DMPR/dp_detector_299_1023.pth"
  24. conf_thres, iou_thres, classes = 0.25, 0.45, 3
  25. labelnames = "weights/yolov5/class5/labelnames.json"
  26. rainbows = [[0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 0, 255], [255, 255, 0], [255, 129, 0], [255, 0, 127],
  27. [127, 255, 0], [0, 255, 127], [0, 127, 255], [127, 0, 255], [255, 127, 255], [255, 255, 127],
  28. [127, 255, 255], [0, 255, 255], [255, 127, 255], [127, 255, 255], [0, 127, 0], [0, 0, 127],
  29. [0, 255, 255]]
  30. allowedList = [0, 1, 2, 3]
  31. ##加载模型,准备好显示字符
  32. device = select_device(device_)
  33. half = device.type != 'cpu' # half precision only supported on CUDA
  34. # yolov5 model
  35. model = attempt_load(Detweights, map_location=device)
  36. if half:
  37. model.half()
  38. # load args
  39. args = config.get_parser_for_inference().parse_args()
  40. # STDC model
  41. STDC_model = BiSeNet(backbone=args.backbone, n_classes=args.n_classes,
  42. use_boundary_2=args.use_boundary_2, use_boundary_4=args.use_boundary_4,
  43. use_boundary_8=args.use_boundary_8, use_boundary_16=args.use_boundary_16,
  44. use_conv_last=args.use_conv_last).to(device)
  45. STDC_model.load_state_dict(torch.load(args.respth))
  46. STDC_model.eval()
  47. # DMPR model
  48. # DMPRmodel = DirectionalPointDetector(3, args.depth_factor, config.NUM_FEATURE_MAP_CHANNEL).to(device)
  49. # DMPRmodel.load_state_dict(torch.load(DMPRweights))
  50. DMPRmodel = Model(args.cfg, ch=3).to(device)
  51. DMPRmodel.load_state_dict(torch.load(DMPRweights))
  52. # 图像测试
  53. # impth = 'images/input'
  54. impth = 'images/debug'
  55. # impth = '/home/thsw/WJ/zjc/AI_old/images/input_0'
  56. # outpth = 'images/output'
  57. outpth = 'images/debug_out'
  58. folders = os.listdir(impth)
  59. for file in folders:
  60. imgpath = os.path.join(impth, file)
  61. img0 = cv2.imread(imgpath)
  62. assert img0 is not None, 'Image Not Found ' + imgpath
  63. t_start = time.time()
  64. # yolo process
  65. det0 = yolo_process(img0, model, device, args, half)
  66. det0 = det0.cpu().detach().numpy()
  67. t_yolo = time.time()
  68. print(f't_yolo. ({t_yolo - t_start:.3f}s)')
  69. t_stdc = time.time()
  70. # STDC process
  71. det2 = STDC_process(img0, STDC_model, device, args.stdc_new_hw)
  72. # det2[det2 == 1] = 255
  73. t_stdc_inf = time.time()
  74. print(f't_stdc_inf. ({t_stdc_inf - t_stdc:.3f}s)')
  75. # STDC joint yolo
  76. det0 = stdc_yolo(det2, det0)
  77. t_stdc_yolo = time.time()
  78. print(f't_stdc_joint. ({t_stdc_yolo - t_stdc_inf:.3f}s)')
  79. # plot所有box
  80. # for *xyxy, conf, cls in reversed(det0):
  81. # label = f'{int(cls)} {conf:.2f}'
  82. # plot_one_box(xyxy, img0, label=label, color=rainbows[int(cls)], line_thickness=2)
  83. # DMPR process
  84. det1 = DMPR_process(img0, DMPRmodel, device, args)
  85. det1 = det1.cpu().detach().numpy()
  86. #
  87. t_dmpr = time.time()
  88. print(f't_dmpr. ({t_dmpr - t_yolo:.3f}s)')
  89. # 绘制角点
  90. # plot_points(img0, det1)
  91. # yolo joint DMPR
  92. cls = 0 #需要过滤的box类别
  93. joint_det, dilate_box = dmpr_yolo(det1, det0, img0.shape, cls, args.scale_ratio, args.border)
  94. #
  95. t_joint = time.time()
  96. print(f't_joint. ({t_joint - t_dmpr:.3f}s)')
  97. t_end = time.time()
  98. print(f'Done. ({t_end - t_start:.3f}s)')
  99. # 绘制膨胀box
  100. # for *xyxy, flag in dilate_box:
  101. # plot_one_box(xyxy, img0, color=rainbows[int(cls)], line_thickness=2)
  102. # #
  103. # # 绘制删除满足 在膨胀框内 && 角度差小于90度 的box
  104. # for *xyxy, conf, cls, flag in reversed(joint_det):
  105. # if flag == 0:
  106. # # label = f'{int(cls)} {conf:.2f}'
  107. # label = None
  108. # plot_one_box(xyxy, img0, label=label, color=rainbows[int(cls)], line_thickness=2)
  109. # save
  110. # save_path = os.path.join(outpth, file)
  111. # cv2.imwrite(save_path, file)
  112. if __name__ == '__main__':
  113. main()