Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

168 rindas
6.0KB

  1. import multiprocessing as mp
  2. import sys
  3. import time
  4. from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, wait, ALL_COMPLETED
  5. from multiprocessing import Queue
  6. import cv2
  7. import tensorrt as trt
  8. sys.path.extend(['/home/th/tuo_heng/dev/tuoheng_alg', '/home/th/tuo_heng/dev/tuoheng_alg/util'])
  9. from util.PlotsUtils import get_label_arrays
  10. from util.TorchUtils import select_device
  11. sys.path.extend(['/home/th/tuo_heng/', '/home/th/tuo_heng/dev', '/home/th/tuo_heng/dev/AIlib2', '/home/th/tuo_heng/dev/AIlib2/segutils'])
  12. from segutils.segmodel import SegModel
  13. from models.experimental import attempt_load
  14. from AI import AI_process
  15. from utilsK.queRiver import riverDetSegMixProcess
  16. COLOR = (
  17. [0, 0, 255],
  18. [255, 0, 0],
  19. [211, 0, 148],
  20. [0, 127, 0],
  21. [0, 69, 255],
  22. [0, 255, 0],
  23. [255, 0, 255],
  24. [0, 0, 127],
  25. [127, 0, 255],
  26. [255, 129, 0],
  27. [139, 139, 0],
  28. [255, 255, 0],
  29. [127, 255, 0],
  30. [0, 127, 255],
  31. [0, 255, 127],
  32. [255, 127, 255],
  33. [8, 101, 139],
  34. [171, 130, 255],
  35. [139, 112, 74],
  36. [205, 205, 180])
  37. par = {
  38. 'device': '0',
  39. 'labelnames': ["排口", "水生植被", "其它", "漂浮物", "污染排口", "菜地", "违建", "岸坡垃圾"],
  40. 'seg_nclass': 2,
  41. 'trtFlag_seg': True,
  42. 'trtFlag_det': True,
  43. 'segRegionCnt': 1,
  44. 'segPar': {
  45. 'modelSize': (640, 360),
  46. 'mean': (0.485, 0.456, 0.406),
  47. 'std': (0.229, 0.224, 0.225),
  48. 'numpy': False,
  49. 'RGB_convert_first': True,
  50. 'mixFunction': {
  51. 'function': riverDetSegMixProcess,
  52. 'pars': {
  53. 'slopeIndex': [5, 6, 7],
  54. 'riverIou': 0.1
  55. }
  56. }
  57. },
  58. 'postFile': {
  59. "name": "post_process",
  60. "conf_thres": 0.25,
  61. "iou_thres": 0.45,
  62. "classes": 5,
  63. "rainbows": COLOR
  64. },
  65. 'Detweights': "/home/th/tuo_heng/dev/AIlib2/weights/river/yolov5_2080Ti_fp16.engine",
  66. 'Segweights': '/home/th/tuo_heng/dev/AIlib2/weights/river/stdc_360X640_2080Ti_fp16.engine'
  67. }
  68. mode, postPar, segPar = par.get('mode', 'others'), par.get('postPar'), par.get('segPar')
  69. new_device = select_device(par.get('device'))
  70. names = par['labelnames']
  71. half = new_device.type != 'cpu'
  72. Detweights = par['Detweights']
  73. with open(Detweights, "rb") as f, trt.Runtime(trt.Logger(trt.Logger.ERROR)) as runtime:
  74. model = runtime.deserialize_cuda_engine(f.read())
  75. Segweights = par['Segweights']
  76. if Segweights:
  77. with open(Segweights, "rb") as f, trt.Runtime(trt.Logger(trt.Logger.ERROR)) as runtime:
  78. segmodel = runtime.deserialize_cuda_engine(f.read())
  79. else:
  80. segmodel = None
  81. postFile = par['postFile']
  82. rainbows = postFile["rainbows"]
  83. objectPar = {
  84. 'half': half,
  85. 'device': new_device,
  86. 'conf_thres': postFile["conf_thres"],
  87. 'ovlap_thres_crossCategory': postFile.get("ovlap_thres_crossCategory"),
  88. 'iou_thres': postFile["iou_thres"],
  89. 'allowedList': [],
  90. 'segRegionCnt': par['segRegionCnt'],
  91. 'trtFlag_det': par['trtFlag_det'],
  92. 'trtFlag_seg': par['trtFlag_seg']
  93. }
  94. Detweights = "/home/th/tuo_heng/dev/AIlib2/weights/river2/yolov5_2080Ti_fp16.engine"
  95. with open(Detweights, "rb") as f, trt.Runtime(trt.Logger(trt.Logger.ERROR)) as runtime:
  96. model = runtime.deserialize_cuda_engine(f.read())
  97. Segweights = "/home/th/tuo_heng/dev/AIlib2/weights/river2/stdc_360X640_2080Ti_fp16.engine"
  98. with open(Segweights, "rb") as f, trt.Runtime(trt.Logger(trt.Logger.ERROR)) as runtime:
  99. segmodel = runtime.deserialize_cuda_engine(f.read())
  100. allowedList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  101. def one_label(width, height, model_param):
  102. names = model_param[4]
  103. rainbows = model_param[6]
  104. digitFont, label_arraylist, font_config = get_label_arraylist(width, height, names, rainbows)
  105. """
  106. font_config, frame, model, segmodel, names, label_arraylist, rainbows, objectPar, font, segPar, mode, postPar,
  107. requestId
  108. """
  109. model_param[5] = label_arraylist
  110. model_param[8] = digitFont
  111. model_param[0] = font_config
  112. def get_label_arraylist(*args):
  113. width, height, names, rainbows = args
  114. # line = int(round(0.002 * (height + width) / 2) + 1)
  115. line = int(width / 1920 * 3 - 1)
  116. label = ' 0.95'
  117. tf = max(line, 1)
  118. fontScale = line * 0.33
  119. text_width, text_height = cv2.getTextSize(label, 0, fontScale=fontScale, thickness=tf)[0]
  120. fontsize = int(width / 1920 * 40)
  121. numFontSize = float(format(width / 1920 * 1.1, '.1f'))
  122. digitFont = {'line_thickness': line,
  123. 'boxLine_thickness': line,
  124. 'fontSize': numFontSize,
  125. 'waterLineColor': (0, 255, 255),
  126. 'segLineShow': False,
  127. 'waterLineWidth': line}
  128. label_arraylist = get_label_arrays(names, rainbows, text_height, fontSize=fontsize,
  129. fontPath="/home/th/tuo_heng/dev/AIlib2/conf/platech.ttf")
  130. return digitFont, label_arraylist, (line, text_width, text_height, fontScale, tf)
  131. image = cv2.imread("/home/th/tuo_heng/dev/ompv2fn94m_1687259193110.jpg")
  132. start_time1 = time.time()
  133. with ThreadPoolExecutor(max_workers=3) as t:
  134. rs = []
  135. for i in range(500):
  136. rr = t.submit(AI_process, [image], model, segmodel, names, None, rainbows, objectPar=objectPar,
  137. font={'line_thickness': 1,
  138. 'boxLine_thickness': 1,
  139. 'fontSize': 1.1,
  140. 'waterLineColor': (0, 255, 255),
  141. 'segLineShow': False,
  142. 'waterLineWidth': 1}, segPar=segPar, mode=mode, postPar=postPar)
  143. rs.append(rr)
  144. for i in rs:
  145. i.result()
  146. print(time.time() - start_time1)
  147. start_time = time.time()
  148. for i in range(500):
  149. AI_process([image], model, segmodel, names, None, rainbows, objectPar=objectPar,
  150. font={'line_thickness': 1,
  151. 'boxLine_thickness': 1,
  152. 'fontSize': 1.1,
  153. 'waterLineColor': (0, 255, 255),
  154. 'segLineShow': False,
  155. 'waterLineWidth': 1}, segPar=segPar, mode=mode, postPar=postPar)
  156. print(time.time() - start_time)