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.

363 lines
17KB

  1. import cv2,os,time,json
  2. from models.experimental import attempt_load
  3. from segutils.segmodel import SegModel,get_largest_contours
  4. from segutils.trtUtils import segtrtEval,yolov5Trtforward,OcrTrtForward
  5. from segutils.trafficUtils import trafficPostProcessing,colour_code_segmentation,get_label_info,trafficPostProcessingV2
  6. from utils.torch_utils import select_device
  7. from utilsK.queRiver import get_labelnames,get_label_arrays,post_process_,img_pad,draw_painting_joint
  8. from utils.datasets import letterbox
  9. import numpy as np
  10. import torch
  11. import math
  12. from PIL import Image
  13. import torch.nn.functional as F
  14. from copy import deepcopy
  15. def xywh2xyxy(box,iW=None,iH=None):
  16. xc,yc,w,h = box[0:4]
  17. x0 =max(0, xc-w/2.0)
  18. x1 =min(1, xc+w/2.0)
  19. y0=max(0, yc-h/2.0)
  20. y1=min(1,yc+h/2.0)
  21. if iW: x0,x1 = x0*iW,x1*iW
  22. if iH: y0,y1 = y0*iH,y1*iH
  23. return [x0,y0,x1,y1]
  24. def get_ms(t2,t1):
  25. return (t2-t1)*1000.0
  26. def get_postProcess_para(parfile):
  27. with open(parfile) as fp:
  28. par = json.load(fp)
  29. assert 'post_process' in par.keys(), ' parfile has not key word:post_process'
  30. parPost=par['post_process']
  31. return parPost["conf_thres"],parPost["iou_thres"],parPost["classes"],parPost["rainbows"]
  32. def AI_process(im0s,model,segmodel,names,label_arraylist,rainbows,objectPar={ 'half':True,'device':'cuda:0' ,'conf_thres':0.25,'iou_thres':0.45,'allowedList':[0,1,2,3],'slopeIndex':[5,6,7],'segRegionCnt':1, 'trtFlag_det':False,'trtFlag_seg':False }, font={ 'line_thickness':None, 'fontSize':None,'boxLine_thickness':None,'waterLineColor':(0,255,255),'waterLineWidth':3} ,segPar={'modelSize':(640,360),'mean':(0.485, 0.456, 0.406),'std' :(0.229, 0.224, 0.225),'numpy':False, 'RGB_convert_first':True},mode='others',postPar=None):
  33. #输入参数
  34. # im0s---原始图像列表
  35. # model---检测模型,segmodel---分割模型(如若没有用到,则为None)
  36. #
  37. #输出:两个元素(列表,字符)构成的元组,[im0s[0],im0,det_xywh,iframe],strout
  38. # [im0s[0],im0,det_xywh,iframe]中,
  39. # im0s[0]--原始图像,im0--AI处理后的图像,iframe--帧号/暂时不需用到。
  40. # det_xywh--检测结果,是一个列表。
  41. # 其中每一个元素表示一个目标构成如:[float(cls_c), xc,yc,w,h, float(conf_c)]
  42. # #cls_c--类别,如0,1,2,3; xc,yc,w,h--中心点坐标及宽;conf_c--得分, 取值范围在0-1之间
  43. # #strout---统计AI处理个环节的时间
  44. # Letterbox
  45. half,device,conf_thres,iou_thres,allowedList = objectPar['half'],objectPar['device'],objectPar['conf_thres'],objectPar['iou_thres'],objectPar['allowedList']
  46. slopeIndex, trtFlag_det,trtFlag_seg,segRegionCnt = objectPar['slopeIndex'],objectPar['trtFlag_det'],objectPar['trtFlag_seg'],objectPar['segRegionCnt']
  47. time0=time.time()
  48. if trtFlag_det:
  49. img, padInfos = img_pad(im0s[0], size=(640,640,3)) ;img = [img]
  50. else:
  51. img = [letterbox(x, 640, auto=True, stride=32)[0] for x in im0s];padInfos=None
  52. # Stack
  53. img = np.stack(img, 0)
  54. # Convert
  55. img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
  56. img = np.ascontiguousarray(img)
  57. img = torch.from_numpy(img).to(device)
  58. img = img.half() if half else img.float() # uint8 to fp16/32
  59. time01=time.time()
  60. img /= 255.0 # 0 - 255 to 0.0 - 1.0
  61. if segmodel:
  62. if trtFlag_seg:
  63. seg_pred,segstr = segtrtEval(segmodel,im0s[0],par=segPar)
  64. else:
  65. seg_pred,segstr = segmodel.eval(im0s[0] )
  66. segFlag=True
  67. else:
  68. seg_pred = None;segFlag=False;segstr='Not implemented'
  69. if mode=='highWay3.0':
  70. seg_pred_mulcls = seg_pred.copy()
  71. seg_pred = (seg_pred==1).astype(np.uint8) ###把路提取出来,路的类别是1
  72. time1=time.time()
  73. if trtFlag_det:
  74. pred = yolov5Trtforward(model,img)
  75. else:
  76. pred = model(img,augment=False)[0]
  77. time2=time.time()
  78. datas = [[''], img, im0s, None,pred,seg_pred,10]
  79. ObjectPar={ 'object_config':allowedList, 'slopeIndex':slopeIndex ,'segmodel':segFlag,'segRegionCnt':segRegionCnt }
  80. p_result,timeOut = post_process_(datas,conf_thres, iou_thres,names,label_arraylist,rainbows,10,ObjectPar=ObjectPar,font=font,padInfos=padInfos)
  81. if mode=='highWay3.0':
  82. assert postPar , ' postPar not implemented'
  83. label_info = get_label_info(postPar['label_csv'])
  84. seg_pred=cv2.resize(seg_pred_mulcls,( segPar['modelSize'][0] , segPar['modelSize'] [1]) )
  85. imH,imW = im0s[0].shape[0:2]
  86. mmH,mmW = seg_pred.shape[0:2]
  87. fx=mmW/imW;fy=mmH/imH
  88. det_coords=[]
  89. det_coords_original=[]
  90. for box in p_result[2]:
  91. b_0 = box[1:5]
  92. b_0.insert(0,box[0]);b_0.append(box[5] )
  93. det_coords_original.append( b_0 )
  94. if int(box[0]) != 1: continue
  95. det_coords.append(b_0)
  96. postPar['ZoomFactor']={'x':mmW/imW ,'y':mmH/imH}
  97. postPar['mask']=seg_pred;postPar['det']=deepcopy(det_coords)
  98. postPar['label_info']=label_info
  99. tlist = list(postPar.keys()); tlist.sort()
  100. if len(det_coords)> 0:
  101. list8, image,time_infos = trafficPostProcessingV2(postPar)
  102. Accident_results = np.array(list8,dtype=object)
  103. acc_det=[]
  104. for bpoints in list8:
  105. if bpoints[9]>conf_thres:
  106. xyxy=bpoints[1:5];xyxy=[int(x) for x in xyxy]
  107. cls=9;conf=bpoints[9];
  108. box_acc = [cls,*xyxy,conf]
  109. acc_det.append(box_acc)
  110. if cls in allowedList:
  111. p_result[1] = draw_painting_joint(xyxy,p_result[1],label_arraylist[int(cls)],score=conf,color=rainbows[int(cls)%20],font=font,socre_location="leftBottom")
  112. det_coords_original.extend(acc_det)
  113. p_result[2]= deepcopy(det_coords_original)
  114. #print( ' time:',time_infos,' results.shape:',Accident_results.shape, p_result[2])
  115. time_info = 'letterbox:%.1f, seg:%.1f , infer:%.1f,%s, seginfo:%s'%( (time01-time0)*1000, (time1-time01)*1000 ,(time2-time1)*1000,timeOut , segstr )
  116. if mode=='highWay3.0':
  117. p_result.append(seg_pred_mulcls)
  118. return p_result,time_info
  119. def AI_Seg_process(im0s,segmodel,digitWordFont,trtFlag_seg=True,segPar={'modelSize':(640,360),'mean':(0.485, 0.456, 0.406),'std' :(0.229, 0.224, 0.225),'numpy':False, 'RGB_convert_first':True},postPar= {'label_csv': './AIlib2/weights/conf/trafficAccident/class_dict.csv', 'speedRoadArea': 5100, 'vehicleArea': 100, 'speedRoadVehicleAngleMin': 15, 'speedRoadVehicleAngleMax': 75, 'vehicleLengthWidthThreshold': 4, 'vehicleSafeDistance': 7}):
  120. '''
  121. 输入参数
  122. im0s---原始图像列表
  123. segmodel---分割模型,segmodel---分割模型(如若没有用到,则为None)
  124. digitWordFont--显示字体,数字等参数
  125. trtFlag_seg--模型是否是TRT格式
  126. segPar--分割模型的参数
  127. postPar--后处理参数
  128. 输出
  129. seg_pred--返回语义分割的结果图(0,1,2...表示)
  130. img_draw--原图上带有矩形框的图
  131. segstr-----文本数据包括时间信息
  132. list1-----返回目标的坐标结果,每一个目标用[ cls, x0,y0,x1,y1,conf ]
  133. '''
  134. time1=time.time()
  135. H,W=im0s[0].shape[0:2]
  136. img_draw=im0s[0].copy()
  137. if trtFlag_seg:
  138. seg_pred,segstr = segtrtEval(segmodel,im0s[0],par=segPar)
  139. else:
  140. seg_pred,segstr = segmodel.eval(im0s[0] )
  141. time2 = time.time()
  142. label_info = get_label_info(postPar['label_csv'])
  143. postPar['CCS']=colour_code_segmentation(seg_pred.copy(), label_info)
  144. postPar['sourceImageSize'] = im0s[0].shape[0:2]
  145. postPar['seg_pred_size'] = seg_pred.shape[0:2]
  146. list1,post_time_infos = trafficPostProcessing(postPar)
  147. list2=[]
  148. cls=0
  149. label_arraylist=digitWordFont['label_arraylist']
  150. rainbows=digitWordFont['rainbows']
  151. for bpoints in list1:
  152. #print('###line104:',bpoints)
  153. bpoints=np.array(bpoints)
  154. x0=np.min( bpoints[:,0] )
  155. y0=np.min( bpoints[:,1] )
  156. x1=np.max( bpoints[:,0] )
  157. y1=np.max( bpoints[:,1] )
  158. conf= ((x0+x1)/W + (y0+y1)/H)/4.0;
  159. conf=1.0 - math.fabs((conf-0.5)/0.5)
  160. xyxy=[x0,y0,x1,y1]
  161. xyxy=[int(x+0.5) for x in xyxy]
  162. #float(cls_c), *xywh, float(conf_c)]
  163. list2.append( [ cls, x0,y0,x1,y1,conf ] )
  164. img_draw = draw_painting_joint(xyxy,img_draw,label_arraylist[int(cls)],score=conf,color=rainbows[int(cls)%20],font=digitWordFont)
  165. segstr = 'segInfer:%.2f %s '%( (time2-time1)*1000.0,post_time_infos )
  166. return seg_pred,img_draw,segstr,list2
  167. def AI_process_v2(im0s,model,segmodel,names,label_arraylist,rainbows,half=True,device=' cuda:0',conf_thres=0.25, iou_thres=0.45,allowedList=[0,1,2,3], font={ 'line_thickness':None, 'fontSize':None,'boxLine_thickness':None,'waterLineColor':(0,255,255),'waterLineWidth':3} ):
  168. #输入参数
  169. # im0s---原始图像列表
  170. # model---检测模型,segmodel---分割模型(如若没有用到,则为None)
  171. #输出:两个元素(列表,字符)构成的元组,[im0s[0],im0,det_xywh,iframe],strout
  172. # [im0s[0],im0,det_xywh,iframe]中,
  173. # im0s[0]--原始图像,im0--AI处理后的图像,iframe--帧号/暂时不需用到。
  174. # det_xywh--检测结果,是一个列表。
  175. # 其中每一个元素表示一个目标构成如:[float(cls_c), xc,yc,w,h, float(conf_c)]
  176. # #cls_c--类别,如0,1,2,3; xc,yc,w,h--中心点坐标及宽;conf_c--得分, 取值范围在0-1之间
  177. # #strout---统计AI处理个环节的时间
  178. # Letterbox
  179. time0=time.time()
  180. #img = [letterbox(x, 640, auto=True, stride=32)[0] for x in im0s]
  181. img, padInfos = img_pad(im0s[0], size=(640,640,3)) ;img = [img]
  182. # Stack
  183. img = np.stack(img, 0)
  184. # Convert
  185. img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
  186. img = np.ascontiguousarray(img)
  187. img = torch.from_numpy(img).to(device)
  188. img = img.half() if half else img.float() # uint8 to fp16/32
  189. time01=time.time()
  190. img /= 255.0 # 0 - 255 to 0.0 - 1.0
  191. if segmodel:
  192. seg_pred,segstr = segmodel.eval(im0s[0] )
  193. segFlag=True
  194. else:
  195. seg_pred = None;segFlag=False
  196. time1=time.time()
  197. pred = model(img,augment=False)
  198. time2=time.time()
  199. datas = [[''], img, im0s, None,pred,seg_pred,10]
  200. p_result,timeOut = post_process_(datas,conf_thres, iou_thres,names,label_arraylist,rainbows,10,object_config=allowedList,segmodel=segFlag,font=font,padInfos=padInfos)
  201. time_info = 'letterbox:%.1f, seg:%.1f , infer:%.1f,%s, seginfo:%s'%( (time01-time0)*1000, (time1-time01)*1000 ,(time2-time1)*1000,timeOut , segstr )
  202. return p_result,time_info
  203. def AI_process_forest(im0s,model,segmodel,names,label_arraylist,rainbows,half=True,device=' cuda:0',conf_thres=0.25, iou_thres=0.45,allowedList=[0,1,2,3], font={ 'line_thickness':None, 'fontSize':None,'boxLine_thickness':None,'waterLineColor':(0,255,255),'waterLineWidth':3} ,trtFlag_det=False):
  204. #输入参数
  205. # im0s---原始图像列表
  206. # model---检测模型,segmodel---分割模型(如若没有用到,则为None)
  207. #输出:两个元素(列表,字符)构成的元组,[im0s[0],im0,det_xywh,iframe],strout
  208. # [im0s[0],im0,det_xywh,iframe]中,
  209. # im0s[0]--原始图像,im0--AI处理后的图像,iframe--帧号/暂时不需用到。
  210. # det_xywh--检测结果,是一个列表。
  211. # 其中每一个元素表示一个目标构成如:[float(cls_c), xc,yc,w,h, float(conf_c)]
  212. # #cls_c--类别,如0,1,2,3; xc,yc,w,h--中心点坐标及宽;conf_c--得分, 取值范围在0-1之间
  213. # #strout---统计AI处理个环节的时间
  214. # Letterbox
  215. time0=time.time()
  216. if trtFlag_det:
  217. img, padInfos = img_pad(im0s[0], size=(640,640,3)) ;img = [img]
  218. else:
  219. img = [letterbox(x, 640, auto=True, stride=32)[0] for x in im0s];padInfos=None
  220. #img = [letterbox(x, 640, auto=True, stride=32)[0] for x in im0s]
  221. # Stack
  222. img = np.stack(img, 0)
  223. # Convert
  224. img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
  225. img = np.ascontiguousarray(img)
  226. img = torch.from_numpy(img).to(device)
  227. img = img.half() if half else img.float() # uint8 to fp16/32
  228. img /= 255.0 # 0 - 255 to 0.0 - 1.0
  229. if segmodel:
  230. seg_pred,segstr = segmodel.eval(im0s[0] )
  231. segFlag=True
  232. else:
  233. seg_pred = None;segFlag=False
  234. time1=time.time()
  235. pred = yolov5Trtforward(model,img) if trtFlag_det else model(img,augment=False)[0]
  236. time2=time.time()
  237. datas = [[''], img, im0s, None,pred,seg_pred,10]
  238. ObjectPar={ 'object_config':allowedList, 'slopeIndex':[] ,'segmodel':segFlag,'segRegionCnt':0 }
  239. p_result,timeOut = post_process_(datas,conf_thres, iou_thres,names,label_arraylist,rainbows,10,ObjectPar=ObjectPar,font=font,padInfos=padInfos)
  240. #p_result,timeOut = post_process_(datas,conf_thres, iou_thres,names,label_arraylist,rainbows,10,object_config=allowedList,segmodel=segFlag,font=font,padInfos=padInfos)
  241. time_info = 'letterbox:%.1f, infer:%.1f, '%( (time1-time0)*1000,(time2-time1)*1000 )
  242. return p_result,time_info+timeOut
  243. def ocr_process(pars):
  244. img_patch,engine,context,converter,AlignCollate_normal,device=pars[0:6]
  245. time1 = time.time()
  246. img_tensor = AlignCollate_normal([ Image.fromarray(img_patch,'L') ])
  247. img_input = img_tensor.to('cuda:0')
  248. time2 = time.time()
  249. preds,trtstr=OcrTrtForward(engine,[img_input],context)
  250. time3 = time.time()
  251. batch_size = preds.size(0)
  252. preds_size = torch.IntTensor([preds.size(1)] * batch_size)
  253. ######## filter ignore_char, rebalance
  254. preds_prob = F.softmax(preds, dim=2)
  255. preds_prob = preds_prob.cpu().detach().numpy()
  256. pred_norm = preds_prob.sum(axis=2)
  257. preds_prob = preds_prob/np.expand_dims(pred_norm, axis=-1)
  258. preds_prob = torch.from_numpy(preds_prob).float().to(device)
  259. _, preds_index = preds_prob.max(2)
  260. preds_index = preds_index.view(-1)
  261. time4 = time.time()
  262. preds_str = converter.decode_greedy(preds_index.data.cpu().detach().numpy(), preds_size.data)
  263. time5 = time.time()
  264. info_str= ('pre-process:%.2f TRTforward:%.2f (%s) postProcess:%2.f decoder:%.2f, Total:%.2f , pred:%s'%(get_ms(time2,time1 ),get_ms(time3,time2 ),trtstr, get_ms(time4,time3 ), get_ms(time5,time4 ), get_ms(time5,time1 ), preds_str ) )
  265. return preds_str,info_str
  266. def main():
  267. ##预先设置的参数
  268. device_='1' ##选定模型,可选 cpu,'0','1'
  269. ##以下参数目前不可改
  270. Detweights = "weights/yolov5/class5/best_5classes.pt"
  271. seg_nclass = 2
  272. Segweights = "weights/BiSeNet/checkpoint.pth"
  273. conf_thres,iou_thres,classes= 0.25,0.45,5
  274. labelnames = "weights/yolov5/class5/labelnames.json"
  275. rainbows = [ [0,0,255],[0,255,0],[255,0,0],[255,0,255],[255,255,0],[255,129,0],[255,0,127],[127,255,0],[0,255,127],[0,127,255],[127,0,255],[255,127,255],[255,255,127],[127,255,255],[0,255,255],[255,127,255],[127,255,255], [0,127,0],[0,0,127],[0,255,255]]
  276. allowedList=[0,1,2,3]
  277. ##加载模型,准备好显示字符
  278. device = select_device(device_)
  279. names=get_labelnames(labelnames)
  280. label_arraylist = get_label_arrays(names,rainbows,outfontsize=40,fontpath="conf/platech.ttf")
  281. half = device.type != 'cpu' # half precision only supported on CUDA
  282. model = attempt_load(Detweights, map_location=device) # load FP32 model
  283. if half: model.half()
  284. segmodel = SegModel(nclass=seg_nclass,weights=Segweights,device=device)
  285. ##图像测试
  286. #url='images/examples/20220624_响水河_12300_1621.jpg'
  287. impth = 'images/examples/'
  288. outpth = 'images/results/'
  289. folders = os.listdir(impth)
  290. for i in range(len(folders)):
  291. imgpath = os.path.join(impth, folders[i])
  292. im0s=[cv2.imread(imgpath)]
  293. time00 = time.time()
  294. p_result,timeOut = AI_process(im0s,model,segmodel,names,label_arraylist,rainbows,half,device,conf_thres, iou_thres,allowedList,fontSize=1.0)
  295. time11 = time.time()
  296. image_array = p_result[1]
  297. cv2.imwrite( os.path.join( outpth,folders[i] ) ,image_array )
  298. print('----process:%s'%(folders[i]), (time.time() - time11) * 1000)
  299. if __name__=="__main__":
  300. main()