Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

464 lines
22KB

  1. # 耗时最短代码
  2. import numpy as np
  3. import cv2,time,math
  4. import matplotlib.pyplot as plt
  5. def get_ms(time2, time1):
  6. return (time2-time1)*1000.0
  7. # 计算一点到二次函数曲线的距离,二次函数的表达式为x = a*(y**2) + b*y + c
  8. def point2QF(a, b, c, y, x): # 坐标点(y, x)
  9. distance = abs(x - a*(y**2) - b*y - c) / math.sqrt(1 + ((2*a*y + b)**2))
  10. return distance
  11. # 存储所有speedRoad的contours
  12. def storageRoad(contours, pars):
  13. allRoadCnt = [] # 存储所有speedRoad的contours
  14. for cnt in contours: # 道路
  15. if len(cnt) >= 6:
  16. rect = cv2.minAreaRect(cnt)
  17. if rect[1][0] * rect[1][1] > pars['RoadArea']: # 过滤掉面积小于阈值的speedRoad
  18. allRoadCnt.append(cnt)
  19. return allRoadCnt
  20. # 返回符合标准的lane的个数及contours
  21. def storageLane(contours, pars):
  22. """
  23. contours:lane分割后的原始contours
  24. newLaneContours:符合标准的lane的contours
  25. laneNumber:符合标准的lane的个数
  26. 符合标准的lane定义如下:
  27. (1)contours中的坐标点个数不小于6
  28. (2)lane最小外接矩形的面积大于阈值laneArea
  29. (3)lane最小外接矩形的最短边与最长边的比值小于等于阈值roundness
  30. """
  31. laneNumber = 0
  32. newLaneContours = ()
  33. for cnt in contours:
  34. if len(cnt) >= 6:
  35. rect = cv2.minAreaRect(cnt)
  36. if rect[1][0] * rect[1][1] > pars['laneArea'] and min(rect[1]) / max(rect[1]) <= pars['roundness']:
  37. laneNumber += 1
  38. newLaneContours = newLaneContours + (cnt, )
  39. return laneNumber, newLaneContours
  40. # 将contours中顶点数大于等于6的车辆信息(合格vehicle)和顶点数小于6的车辆信息(不合格vehicle)分别保存起来
  41. def vehicleDivide(contours, vehicleBD, normVehicle, dets, count, i, unnormVehicle, normVehicleCOOR, centerCOOR):
  42. if len(contours) >= 6:
  43. vehicleBD.append(contours)
  44. normVehicle.append(dets[count])
  45. normVehicleCOOR.append(centerCOOR)
  46. else:
  47. dets[int(i / 2)].append(0)
  48. dets[int(i / 2)].append(0)
  49. unnormVehicle.append(dets[int(i / 2)])
  50. return vehicleBD, normVehicle, unnormVehicle, normVehicleCOOR
  51. # 存储所有vehicle的信息
  52. def storageVehicle(pars, imgVehicle, dets):
  53. """
  54. 输入
  55. pars:字典名
  56. imgVehicle:分割图,只包含vehicle和背景
  57. dets:是一个list,其中存储检测得到的各vehicle的信息,即[[x0, y0, x1, y1, 车辆得分, cls], ...]
  58. 输出
  59. dets:存储合格vehicle的信息,即[x0, y0, x1, y1, 车辆得分, cls]
  60. vehicleBD:存储合格vehicle的contours
  61. unnormVehicle:存储不合格vehicle的信息,即[x0, y0, x1, y1, 车辆得分, cls]
  62. normVehicleCOOR:存储合格vehicle的中心点坐标
  63. 说明
  64. 合格vehicle:contours中的顶点数大于等于6
  65. 不合格vehicle:contours中的顶点数小于6
  66. """
  67. vehicleBD = [] # 存储一副图像中vehicles的contours
  68. normVehicle = [] # 将合格vehicle的信息存储在normVehicle中
  69. unnormVehicle = [] # 将不合格vehicle的信息存储在unnormVehicle中
  70. normVehicleCOOR = [] # 存储合格vehicle的中心点坐标
  71. img = cv2.cvtColor(imgVehicle, cv2.COLOR_BGR2GRAY)
  72. count = 0
  73. for i in range(0, len(pars['vehicleCOOR']), 2):
  74. y1 = int(pars['vehicleCOOR'][i][1] * pars['ZoomFactor']['y'])
  75. y2 = int(pars['vehicleCOOR'][i + 1][1] * pars['ZoomFactor']['y'])
  76. x1 = int(pars['vehicleCOOR'][i][0] * pars['ZoomFactor']['x'])
  77. x2 = int(pars['vehicleCOOR'][i + 1][0] * pars['ZoomFactor']['x'])
  78. if y1 >= 2:
  79. y1 = y1 - 2
  80. if y2 <= (pars['modelSize'][1] - 2):
  81. y2 = y2 + 2
  82. if x1 >= 2:
  83. x1 = x1 - 2
  84. if x2 <= (pars['modelSize'][0] - 2):
  85. x2 = x2 + 2
  86. centerCOOR = (int((x1 + x2) / 2), int((y1 + y2) / 2))
  87. img1 = img[y1:y2, x1:x2]
  88. up = np.zeros((20, (x2 - x1)), dtype='uint8')
  89. left = np.zeros(((40 + y2 - y1), 20), dtype='uint8')
  90. img1 = np.concatenate((up, img1), axis=0)
  91. img1 = np.concatenate((img1, up), axis=0)
  92. img1 = np.concatenate((left, img1), axis=1)
  93. img2 = np.concatenate((img1, left), axis=1)
  94. contours2, hierarchy = cv2.findContours(img2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  95. if len(contours2) != 0:
  96. if len(contours2) > 1:
  97. vehicleArea = [] # 存储vehicle的最小外接矩形的面积
  98. for j in range(len(contours2)):
  99. rect = cv2.minAreaRect(contours2[j])
  100. vehicleArea.append(rect[1][0] * rect[1][1])
  101. maxAreaIndex = vehicleArea.index(max(vehicleArea))
  102. maxAreaContours = contours2[maxAreaIndex]
  103. vehicleBD, normVehicle, unnormVehicle, normVehicleCOOR = vehicleDivide(maxAreaContours, vehicleBD, normVehicle, dets, count, i, unnormVehicle, normVehicleCOOR, centerCOOR)
  104. elif len(contours2) == 1:
  105. vehicleBD, normVehicle, unnormVehicle, normVehicleCOOR = vehicleDivide(contours2[0], vehicleBD, normVehicle, dets, count, i, unnormVehicle, normVehicleCOOR, centerCOOR)
  106. else:
  107. dets[int(i / 2)].append(0)
  108. dets[int(i / 2)].append(0)
  109. unnormVehicle.append(dets[int(i / 2)])
  110. count += 1
  111. dets = normVehicle
  112. return dets, vehicleBD, unnormVehicle, normVehicleCOOR
  113. # 计算违停得分
  114. def IllegalParkScore1(vehicleBD, allRoadCnt, dets, unnormVehicle, normVehicleCOOR, a_l, b_l, c_l, a_r, b_r, c_r):
  115. """
  116. 对vehicle是否在speedRoad上进行判断,并计算违章得分
  117. 输出targetList 其格式为:[[cls, x0, y0, x1, y1, score, 违章得分, 违章类别], ...]
  118. """
  119. if len(vehicleBD) != 0:
  120. for i in range(len(vehicleBD)):
  121. rect = cv2.minAreaRect(vehicleBD[i])
  122. center = normVehicleCOOR[i] # vehicle的中心点坐标
  123. if len(allRoadCnt) != 0: # 当车道线个数至少有两条时,才计算违章得分
  124. for j in range(len(allRoadCnt)):
  125. # 判断车辆矩形框的中心点坐标是否在道路矩形框的范围内
  126. flag = cv2.pointPolygonTest(allRoadCnt[j], center, False)
  127. if flag >= 0:
  128. dets[i].append(0) # 给违章得分占位
  129. dets[i].append(0) # 给违章类别占位
  130. if center[0] < predict(a_l, b_l, c_l, center[1]):
  131. distance = point2QF(a_l, b_l, c_l, center[1], center[0])
  132. if distance >= min(rect[1]) / 2:
  133. dets[i][6], dets[i][7] = 1, 1
  134. else:
  135. dets[i][6], dets[i][7] = distance / (min(rect[1]) / 2), 1
  136. elif center[0] > predict(a_r, b_r, c_r, center[1]):
  137. distance = point2QF(a_r, b_r, c_r, center[1], center[0])
  138. if distance >= min(rect[1]) / 2:
  139. dets[i][6], dets[i][7] = 1, 1
  140. else:
  141. dets[i][6], dets[i][7] = distance / (min(rect[1]) / 2), 1
  142. else:
  143. dets[i][6], dets[i][7] = 0, 0
  144. break
  145. # 如果分割图像中不存在speedRoad,则无法进行违章判定,将所有车辆的违章类别设为0,即没有违章
  146. if len(dets[i]) < 8:
  147. dets[i].append(0) # 违章得分为0
  148. dets[i].append(0) # 0表示没有违章
  149. targetList = dets
  150. if len(unnormVehicle) != 0:
  151. for i in range(len(unnormVehicle)):
  152. targetList.append(unnormVehicle[i]) # 将所有车辆的信息合并到一起
  153. else:
  154. targetList = unnormVehicle
  155. return targetList
  156. # 计算违停得分
  157. def IllegalParkScore2(vehicleBD, dets, unnormVehicle):
  158. """
  159. 计算违章得分
  160. 输出targetList 其格式为:[[cls, x0, y0, x1, y1, score, 违章得分, 违章类别], ...]
  161. """
  162. if len(vehicleBD) != 0:
  163. for i in range(len(vehicleBD)):
  164. if len(dets[i]) < 8:
  165. dets[i].append(0) # 违章得分为0
  166. dets[i].append(0) # 0表示没有违章
  167. targetList = dets
  168. if len(unnormVehicle) != 0:
  169. for i in range(len(unnormVehicle)):
  170. targetList.append(unnormVehicle[i]) # 将所有车辆的信息合并到一起
  171. else:
  172. targetList = unnormVehicle
  173. return targetList
  174. # 找最左侧lane时,将要删除的右侧lane的序号存储在delRightLane。找最右侧lane时,将要删除的左侧lane的序号存储在delLeftLane。
  175. def devideLane(laneInfo, i, m, delRightLane, delLeftLane, y):
  176. index1 = np.where(laneInfo[i][3] == y)
  177. index1 = index1[0].tolist()
  178. index1.sort()
  179. x_1 = laneInfo[i][5][index1[0]][0]
  180. index2 = np.where(laneInfo[m][3] == y)
  181. index2 = index2[0].tolist()
  182. index2.sort()
  183. x_2 = laneInfo[m][5][index2[0]][0]
  184. if x_1 < x_2:
  185. if i not in delLeftLane:
  186. delLeftLane.append(i) # 保留右侧lane
  187. if m not in delRightLane:
  188. delRightLane.append(m)
  189. else:
  190. if m not in delLeftLane:
  191. delLeftLane.append(m)
  192. if i not in delRightLane:
  193. delRightLane.append(i)
  194. return delRightLane, delLeftLane
  195. # 确定最左侧和最右侧的lane簇
  196. def detLine(contours):
  197. """
  198. 输入
  199. contours:各lane的contours
  200. 输出
  201. laneInfo:存储各lane的信息,每条lane的信息为:[contours, y坐标范围, lane序号, arr_y, y坐标范围的长度, cnt]
  202. delRightLane:在确定最左侧lane时,其存储需要删除的lane的序号
  203. delLeftLane:在确定最右侧lane时,其存储需要删除的lane的序号
  204. """
  205. mergList = []
  206. for i in range(len(contours)):
  207. cnt = np.squeeze(contours[i], 1)
  208. arr_y = cnt[:, 1]
  209. arrList = list(set(arr_y))
  210. cnt_y = np.sort(np.array(arrList))
  211. mergList.append([contours[i], cnt_y, i, arr_y, len(cnt_y), cnt])
  212. laneInfo = sorted(mergList, key=(lambda x: x[4])) # [[contours[i], cnt_y, i, arr_y, len(cnt_y)],...]
  213. delRightLane = [] # 求最左侧lane
  214. delLeftLane = [] # 求最右侧lane
  215. laneInfoNew = []
  216. for i in range(len(laneInfo)):
  217. laneInfoNew.append([laneInfo[i][1][0], laneInfo[i][1][-1], i]) # [[y_min, y_max, i],...]
  218. laneInfoNew = np.array(laneInfoNew)
  219. new1 = laneInfoNew[:, np.newaxis, :].repeat(laneInfoNew.shape[0], 1)
  220. new2 = laneInfoNew[np.newaxis, ...].repeat(laneInfoNew.shape[0], 0)
  221. new3 = np.concatenate((new1, new2), axis=2)
  222. y_i_min, y_i_max, y_m_min, y_m_max = new3[..., 0], new3[..., 1], new3[..., 3], new3[..., 4]
  223. mask1 = (y_i_min >= y_m_min) & (y_i_min <= y_m_max) & (y_i_max > y_m_max)
  224. mask2 = (y_i_max >= y_m_min) & (y_i_max <= y_m_max) & (y_i_min < y_m_min)
  225. mask3 = (y_i_min >= y_m_min) & (y_i_max <= y_m_max)
  226. mask4 = (y_i_min < y_m_min) & (y_i_max > y_m_max)
  227. if len(np.nonzero(mask1)[0]) != 0:
  228. mask1 = np.triu(mask1, k=1)
  229. serial_i = new3[mask1][..., 2]
  230. serial_m = new3[mask1][..., 5]
  231. for k in range(len(serial_i)):
  232. if (serial_m[k] not in delLeftLane) or (serial_m[k] not in delRightLane) or (serial_i[k] not in delLeftLane) or (serial_i[k] not in delRightLane):
  233. delRightLane, delLeftLane = devideLane(laneInfo, serial_i[k], serial_m[k], delRightLane, delLeftLane, laneInfo[serial_i[k]][1][0])
  234. if len(np.nonzero(mask2)[0]) != 0:
  235. mask2 = np.triu(mask2, k=1)
  236. serial_i = new3[mask2][..., 2]
  237. serial_m = new3[mask2][..., 5]
  238. for k in range(len(serial_i)):
  239. if (serial_m[k] not in delLeftLane) or (serial_m[k] not in delRightLane) or (serial_i[k] not in delLeftLane) or (serial_i[k] not in delRightLane):
  240. delRightLane, delLeftLane = devideLane(laneInfo, serial_i[k], serial_m[k], delRightLane, delLeftLane, laneInfo[serial_i[k]][1][-1])
  241. if len(np.nonzero(mask3)[0]) != 0:
  242. mask3 = np.triu(mask3, k=1)
  243. serial_i = new3[mask3][..., 2]
  244. serial_m = new3[mask3][..., 5]
  245. for k in range(len(serial_i)):
  246. if (serial_m[k] not in delLeftLane) or (serial_m[k] not in delRightLane) or (serial_i[k] not in delLeftLane) or (serial_i[k] not in delRightLane):
  247. delRightLane, delLeftLane = devideLane(laneInfo, serial_i[k], serial_m[k], delRightLane, delLeftLane, laneInfo[serial_i[k]][1][0])
  248. if len(np.nonzero(mask4)[0]) != 0:
  249. mask4 = np.triu(mask4, k=1)
  250. serial_i = new3[mask4][..., 2]
  251. serial_m = new3[mask4][..., 5]
  252. for k in range(len(serial_i)):
  253. if (serial_m[k] not in delLeftLane) or (serial_m[k] not in delRightLane) or (serial_i[k] not in delLeftLane) or (serial_i[k] not in delRightLane):
  254. delRightLane, delLeftLane = devideLane(laneInfo, serial_i[k], serial_m[k], delRightLane, delLeftLane, laneInfo[serial_m[k]][1][0])
  255. return laneInfo, delRightLane, delLeftLane
  256. # 对lane中的y值坐标进行下采样
  257. def downSample(cnt_y):
  258. # number = len(cnt_y) * 0.0125
  259. # cnt_y = np.random.choice(cnt_y, size=number, replace=False)
  260. if len(cnt_y) >= 1000:
  261. cnt_y = cnt_y[1::80]
  262. elif len(cnt_y) >= 900 and len(cnt_y) < 1000:
  263. cnt_y = cnt_y[1::75]
  264. elif len(cnt_y) >= 800 and len(cnt_y) < 900:
  265. cnt_y = cnt_y[1::70]
  266. elif len(cnt_y) >= 700 and len(cnt_y) < 800:
  267. cnt_y = cnt_y[1::65]
  268. elif len(cnt_y) >= 600 and len(cnt_y) < 700:
  269. cnt_y = cnt_y[1::60]
  270. elif len(cnt_y) >= 500 and len(cnt_y) < 600:
  271. cnt_y = cnt_y[1::55]
  272. elif len(cnt_y) >= 400 and len(cnt_y) < 500:
  273. cnt_y = cnt_y[1::40]
  274. elif len(cnt_y) >= 300 and len(cnt_y) < 400:
  275. cnt_y = cnt_y[1::45]
  276. elif len(cnt_y) >= 200 and len(cnt_y) < 300:
  277. cnt_y = cnt_y[1::40]
  278. elif len(cnt_y) >= 100 and len(cnt_y) < 200:
  279. cnt_y = cnt_y[1::35]
  280. elif len(cnt_y) >= 50 and len(cnt_y) < 100:
  281. cnt_y = cnt_y[1::20]
  282. elif len(cnt_y) >= 20 and len(cnt_y) < 50:
  283. cnt_y = cnt_y[1::6]
  284. else:
  285. cnt_y = cnt_y[1::5]
  286. return cnt_y
  287. # 求最左侧lane或最右侧lane中的各点坐标
  288. def targetCOOR(laneInfo, delLane):
  289. """
  290. 输入
  291. laneInfo:存储各lane的信息,每条lane的信息为:[contours, y坐标范围, lane序号, arr_y, y坐标范围的长度, cnt]
  292. delLane:在确定最左侧lane或最右侧lane时,其存储需要删除的lane的序号。
  293. 输出
  294. laneCOOR:存储最左侧或最右侧lane簇中各点的坐标
  295. """
  296. laneCOOR = [] # 存储lane中各点的坐标
  297. centerSort = [] # 存储各lane按照中心点的y坐标排序后的结果
  298. for j in range(len(laneInfo)):
  299. if j not in delLane:
  300. cnt = laneInfo[j][0]
  301. rect = cv2.minAreaRect(cnt)
  302. cnt = np.squeeze(cnt, 1)
  303. cnt_y = laneInfo[j][1]
  304. cnt_y = downSample(cnt_y)
  305. centerSort.append([rect[0][1], cnt_y, laneInfo[j][3], cnt, j])
  306. centerSort = sorted(centerSort, key=(lambda x: x[0]))
  307. for i in range(len(centerSort)):
  308. centerCoordinate = []
  309. for j in range(len(centerSort[i][1])):
  310. index = np.where(centerSort[i][2] == centerSort[i][1][j])
  311. indexList = index[0].tolist()
  312. indexList.sort()
  313. x = (centerSort[i][3][indexList[0]][0] + centerSort[i][3][indexList[-1]][0]) / 2
  314. y = (centerSort[i][3][indexList[0]][1] + centerSort[i][3][indexList[-1]][1]) / 2
  315. centerCoordinate.append([x, y])
  316. laneCOOR = laneCOOR + centerCoordinate
  317. return laneCOOR
  318. # 二次函数曲线表达式:x = a*(y**2) + b*y + c,根据图像中一点的y坐标求二次曲线中的x坐标
  319. def predict(a, b, c, y):
  320. x = a * (y**2) + b * y + c
  321. return x
  322. def mixNoParking_road_postprocess(dets, mask, pars):
  323. """
  324. 对于字典traffic_dict中的各个键,说明如下:
  325. RoadArea:speedRoad的最小外接矩形的面积
  326. vehicleCOOR:是一个列表,用于存储被检测出的vehicle的坐标(vehicle检测模型)
  327. roundness:圆度 ,lane的长与宽的比率,作为判定是否为车道线的标准之一
  328. laneArea:车道线的最小外接矩形的面积
  329. ZoomFactor:图像在H和W方向上的缩放因子,其值小于1
  330. fitOrder:多点拟合曲线的阶数
  331. 最终输出格式:[[x0, y0, x1, y1, 车辆得分, cls, 违章停车得分, 违章类别], ...]
  332. 违章类别:0表示正常车辆,1表示违章车辆
  333. """
  334. det_cors = []
  335. for bb in dets:
  336. det_cors.append((int(bb[0]), int(bb[1])))
  337. det_cors.append((int(bb[2]), int(bb[3])))
  338. print('###line341:', det_cors)
  339. pars['vehicleCOOR'] = det_cors
  340. H, W = mask.shape[0:2] # mask的分辨率为360x640
  341. scaleH = pars['modelSize'][1] / H # 自适应调整缩放比例
  342. scaleW = pars['modelSize'][0] / W
  343. pars['ZoomFactor'] = {'x': scaleW, 'y': scaleH}
  344. new_hw = [int(H * scaleH), int(W * scaleW)]
  345. mask = cv2.resize(mask, (new_hw[1], new_hw[0]))
  346. if len(mask.shape) == 3:
  347. mask = mask[:, :, 0]
  348. t1 = time.time()
  349. imgRoad = mask.copy()
  350. imgVehicle = mask.copy()
  351. lane_line = mask.copy()
  352. # 将vehicle和lane过滤掉,只包含背景和speedRoad
  353. imgRoad[imgRoad == 2] = 1
  354. imgRoad[imgRoad == 3] = 1
  355. # 将speedRoad和lane过滤掉,只保留vehicle和背景
  356. imgVehicle[imgVehicle != 2] = 0
  357. # 将speedRoad和vehicle过滤掉,只保留lane和背景
  358. lane_line[lane_line < 3] = 0
  359. imgRoad = cv2.cvtColor(np.uint8(imgRoad), cv2.COLOR_RGB2BGR) # 道路
  360. imgVehicle = cv2.cvtColor(np.uint8(imgVehicle), cv2.COLOR_RGB2BGR) # 车辆
  361. lane_line = cv2.cvtColor(np.uint8(lane_line), cv2.COLOR_RGB2BGR)
  362. # 对车道线进行膨胀操作
  363. # kernel = np.ones((3, 3), np.uint8) # 膨胀范围
  364. # lane_line = cv2.dilate(lane_line, kernel, iterations=2) # 迭代次数为2
  365. t2 = time.time()
  366. img1 = cv2.cvtColor(imgRoad, cv2.COLOR_BGR2GRAY)
  367. roadContours, hierarchy = cv2.findContours(img1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  368. t3 = time.time()
  369. # 存储所有speedRoad的信息
  370. allRoadCnt = storageRoad(roadContours, pars)
  371. t4 = time.time()
  372. img3 = cv2.cvtColor(lane_line, cv2.COLOR_BGR2GRAY)
  373. laneContours, hierarchy = cv2.findContours(img3, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  374. # 存储所有lane的信息
  375. laneNumber, newLaneContours = storageLane(laneContours, pars)
  376. t5 = time.time()
  377. if laneNumber >= 2:
  378. laneInfo, delRightLane, delLeftLane = detLine(newLaneContours)
  379. t6 = time.time()
  380. # 存储所有vehicle的信息
  381. dets, vehicleBD, unnormVehicle, normVehicleCOOR = storageVehicle(pars, imgVehicle, dets)
  382. t7 = time.time()
  383. leftLaneCOOR = targetCOOR(laneInfo, delRightLane)
  384. rightLaneCOOR = targetCOOR(laneInfo, delLeftLane)
  385. rightLaneCOOR = np.array(rightLaneCOOR)
  386. rightX = rightLaneCOOR[:, 0]
  387. rightY = rightLaneCOOR[:, 1]
  388. leftLaneCOOR = np.array(leftLaneCOOR)
  389. leftX = leftLaneCOOR[:, 0]
  390. leftY = leftLaneCOOR[:, 1]
  391. # a_r,b_r,c_r分别是:最右侧车道线簇拟合的二次函数的二次项系数一次项系数,和常数项
  392. a_r, b_r, c_r = np.polyfit(rightY, rightX, pars['fitOrder'])[0], np.polyfit(rightY, rightX, pars['fitOrder'])[1], np.polyfit(rightY, rightX, pars['fitOrder'])[2]
  393. # a_l,b_l,c_l分别是:最左侧车道线簇拟合的二次函数的二次项系数,一次项系数,和常数项
  394. a_l, b_l, c_l = np.polyfit(leftY, leftX, pars['fitOrder'])[0], np.polyfit(leftY, leftX, pars['fitOrder'])[1], np.polyfit(leftY, leftX, pars['fitOrder'])[2]
  395. # """以下四行代码用于在后处理函数外画图"""
  396. # finalLane = []
  397. # abc = [a_l, b_l, c_l, a_r, b_r, c_r] # abc中存储的是最左侧和最右侧二次函数的各项系数
  398. # finalLane.append(rightLaneCOOR)
  399. # finalLane.append(leftLaneCOOR)
  400. # 计算违停得分
  401. t8 = time.time()
  402. targetList = IllegalParkScore1(vehicleBD, allRoadCnt, dets, unnormVehicle, normVehicleCOOR, a_l, b_l, c_l, a_r, b_r, c_r)
  403. t9 = time.time()
  404. time_infos = 'postTime:%.2f(分割时间:%.2f, findContours:%.2f, ruleJudge:%.2f, storageRoad:%.2f, detLane:%.2f, storageLane:%.2f, storageVehicle:%.2f, fitLine:%.2f, IllegalParkScore1:%.2f)' % (
  405. get_ms(t9, t1), get_ms(t2, t1), get_ms(t3, t2), get_ms(t9, t3), get_ms(t4, t3), get_ms(t6, t5),
  406. get_ms(t5, t4), get_ms(t7, t6), get_ms(t8, t7), get_ms(t9, t8))
  407. # print('####line445:', targetList)
  408. # return targetList, time_infos, finalLane, lane_line, abc
  409. targetList = [ [ *b[0:4],b[6] if b[6]>0 else b[4], b[7] ] for b in targetList ]
  410. return targetList, time_infos
  411. else:
  412. dets, vehicleBD, unnormVehicle, normVehicleCOOR = storageVehicle(pars, imgVehicle, dets)
  413. t6 = time.time()
  414. targetList = IllegalParkScore2(vehicleBD, dets, unnormVehicle)
  415. t7 = time.time()
  416. time_infos = 'postTime:%.2f(分割时间:%.2f, findContours:%.2f, ruleJudge:%.2f, storageRoad:%.2f, storageLane:%.2f, storageVehicle:%.2f, IllegalParkScore2:%.2f)' % (
  417. get_ms(t7, t1), get_ms(t2, t1), get_ms(t3, t2), get_ms(t7, t3), get_ms(t4, t3), get_ms(t5, t4), get_ms(t6, t5), get_ms(t7, t6))
  418. # print('####line456:', targetList)
  419. targetList = [ [ *b[0:4],b[6] if b[6]>0 else b[4], b[7] ] for b in targetList ]
  420. return targetList, time_infos
  421. def mixNoParking_road_postprocess_N(predList, pars):
  422. dets, mask =predList[0:2]
  423. return mixNoParking_road_postprocess(dets, mask, pars)