高速公路违停检测
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

486 行
23KB

  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) # y值是lane的contours的最小y值坐标
  177. index1 = index1[0].tolist()
  178. index1.sort()
  179. x_1 = laneInfo[i][5][index1[0]][0] # 找出第i号lane的contours的最小y值所对应的最小的x坐标,即x_1
  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] # 找出第m号lane的contours的最小y值所对应的最小的x坐标,即x_2
  184. if x_1 < x_2: # 比较x_1和x_2的大小关系,将其划分到delLeftLane或delRightLane中
  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. # sorted(), 默认是升序排序(reverse=False)
  213. laneInfo = sorted(mergList, key=(lambda x: x[4])) # [[contours[i], cnt_y, i, arr_y, len(cnt_y)],...]
  214. delRightLane = [] # 求最左侧lane
  215. delLeftLane = [] # 求最右侧lane
  216. laneInfoNew = []
  217. for i in range(len(laneInfo)):
  218. laneInfoNew.append([laneInfo[i][1][0], laneInfo[i][1][-1], i]) # [[y_min, y_max, i],...]
  219. laneInfoNew = np.array(laneInfoNew)
  220. # print("line241", laneInfoNew)
  221. new1 = laneInfoNew[:, np.newaxis, :].repeat(laneInfoNew.shape[0], 1)
  222. """
  223. new1: 将每条lane的信息即[y_min, y_max, i]重复n次,然后作为一个整体,n是分割图中lane的总条数。
  224. [[[ 1 3 5],...](即:重复n次), [[ 2 6 6],...], [[ 8 9 10],...], [[ 11 12 13],...]
  225. 数据格式为:(n, n, 3),第一个n表示有n个大块,即有n条lane信息。第2个n表示将每条lane信息重复n次。3表示每条lane有3个信息值.
  226. """
  227. new2 = laneInfoNew[np.newaxis, ...].repeat(laneInfoNew.shape[0], 0)
  228. """
  229. new2: 将所有的lane的信息存储在一个list中(即:[[ 1 3 5], [ 2 4 6], [ 8 9 10], [11 12 13]]),作为一个大块。共有n个这种大块,n是lane的总数。
  230. 然后将上述list存放在一个list中,数据格式为:(n, n, 3)。第1个n表示大块的个数,对应lane的总个数。第2个n表示lane的总个数。3表示每条lane的信息数。
  231. """
  232. new3 = np.concatenate((new1, new2), axis=2) # 将new1和new2在axis = 2, 维度上拼接起来,数据格式为(n, n, 6)
  233. # print("line245", new1)
  234. # print("line246", new2)
  235. # print("line247", new3)
  236. y_i_min, y_i_max, y_m_min, y_m_max = new3[..., 0], new3[..., 1], new3[..., 3], new3[..., 4]
  237. # print("line250", y_i_min)
  238. # print("line251", y_i_max)
  239. # print("line252", y_m_min)
  240. # print("line253", y_m_max)
  241. mask1 = (y_i_min >= y_m_min) & (y_i_min <= y_m_max) & (y_i_max > y_m_max)
  242. mask2 = (y_i_max >= y_m_min) & (y_i_max <= y_m_max) & (y_i_min < y_m_min)
  243. mask3 = (y_i_min >= y_m_min) & (y_i_max <= y_m_max)
  244. mask4 = (y_i_min < y_m_min) & (y_i_max > y_m_max)
  245. # print("line257", mask1, mask1.shape)
  246. # print("line258", mask2)
  247. # print("line259", mask3)
  248. # print("line260", mask4)
  249. if len(np.nonzero(mask1)[0]) != 0: # 长度不为0,说明存在mask1类型的车道线位置关系
  250. mask1 = np.triu(mask1, k=1) # 用np.triu函数来获取矩阵的上三角形部分。该函数的作用是返回一个数组的上三角形部分
  251. serial_i = new3[mask1][..., 2]
  252. serial_m = new3[mask1][..., 5]
  253. # print("line265", serial_i)
  254. # print("line266", serial_m)
  255. # print("line267", new3.shape, mask1.shape)
  256. for k in range(len(serial_i)):
  257. 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):
  258. delRightLane, delLeftLane = devideLane(laneInfo, serial_i[k], serial_m[k], delRightLane, delLeftLane, laneInfo[serial_i[k]][1][0])
  259. if len(np.nonzero(mask2)[0]) != 0: # 长度不为0,说明存在mask2类型的车道线位置关系
  260. mask2 = np.triu(mask2, k=1)
  261. serial_i = new3[mask2][..., 2]
  262. serial_m = new3[mask2][..., 5]
  263. for k in range(len(serial_i)):
  264. 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):
  265. delRightLane, delLeftLane = devideLane(laneInfo, serial_i[k], serial_m[k], delRightLane, delLeftLane, laneInfo[serial_i[k]][1][-1])
  266. if len(np.nonzero(mask3)[0]) != 0: # 长度不为0,说明存在mask3类型的车道线位置关系
  267. mask3 = np.triu(mask3, k=1)
  268. serial_i = new3[mask3][..., 2]
  269. serial_m = new3[mask3][..., 5]
  270. for k in range(len(serial_i)):
  271. 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):
  272. delRightLane, delLeftLane = devideLane(laneInfo, serial_i[k], serial_m[k], delRightLane, delLeftLane, laneInfo[serial_i[k]][1][0])
  273. if len(np.nonzero(mask4)[0]) != 0: # 长度不为0,说明存在mask4类型的车道线位置关系
  274. mask4 = np.triu(mask4, k=1)
  275. serial_i = new3[mask4][..., 2]
  276. serial_m = new3[mask4][..., 5]
  277. for k in range(len(serial_i)):
  278. 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):
  279. delRightLane, delLeftLane = devideLane(laneInfo, serial_i[k], serial_m[k], delRightLane, delLeftLane, laneInfo[serial_m[k]][1][0])
  280. return laneInfo, delRightLane, delLeftLane
  281. # 对lane中的y值坐标进行下采样
  282. def downSample(cnt_y):
  283. # number = len(cnt_y) * 0.0125
  284. # cnt_y = np.random.choice(cnt_y, size=number, replace=False)
  285. if len(cnt_y) >= 1000:
  286. cnt_y = cnt_y[1::80]
  287. elif len(cnt_y) >= 900 and len(cnt_y) < 1000:
  288. cnt_y = cnt_y[1::75]
  289. elif len(cnt_y) >= 800 and len(cnt_y) < 900:
  290. cnt_y = cnt_y[1::70]
  291. elif len(cnt_y) >= 700 and len(cnt_y) < 800:
  292. cnt_y = cnt_y[1::65]
  293. elif len(cnt_y) >= 600 and len(cnt_y) < 700:
  294. cnt_y = cnt_y[1::60]
  295. elif len(cnt_y) >= 500 and len(cnt_y) < 600:
  296. cnt_y = cnt_y[1::55]
  297. elif len(cnt_y) >= 400 and len(cnt_y) < 500:
  298. cnt_y = cnt_y[1::40]
  299. elif len(cnt_y) >= 300 and len(cnt_y) < 400:
  300. cnt_y = cnt_y[1::45]
  301. elif len(cnt_y) >= 200 and len(cnt_y) < 300:
  302. cnt_y = cnt_y[1::40]
  303. elif len(cnt_y) >= 100 and len(cnt_y) < 200:
  304. cnt_y = cnt_y[1::35]
  305. elif len(cnt_y) >= 50 and len(cnt_y) < 100:
  306. cnt_y = cnt_y[1::20]
  307. elif len(cnt_y) >= 20 and len(cnt_y) < 50:
  308. cnt_y = cnt_y[1::6]
  309. else:
  310. cnt_y = cnt_y[1::5]
  311. return cnt_y
  312. # 求最左侧lane或最右侧lane中的各点坐标
  313. def targetCOOR(laneInfo, delLane):
  314. """
  315. 输入
  316. laneInfo:存储各lane的信息,每条lane的信息为:[contours, y坐标范围, lane序号, arr_y, y坐标范围的长度, cnt]
  317. delLane:在确定最左侧lane或最右侧lane时,其存储需要删除的lane的序号。
  318. 输出
  319. laneCOOR:存储最左侧或最右侧lane簇中各点的坐标
  320. """
  321. laneCOOR = [] # 存储lane中各点的坐标
  322. centerSort = [] # 存储各lane按照中心点的y坐标排序后的结果
  323. for j in range(len(laneInfo)):
  324. if j not in delLane:
  325. cnt = laneInfo[j][0]
  326. rect = cv2.minAreaRect(cnt)
  327. cnt = np.squeeze(cnt, 1)
  328. cnt_y = laneInfo[j][1]
  329. cnt_y = downSample(cnt_y)
  330. centerSort.append([rect[0][1], cnt_y, laneInfo[j][3], cnt, j])
  331. centerSort = sorted(centerSort, key=(lambda x: x[0])) # 默认是升序排序(reverse=False)
  332. for i in range(len(centerSort)):
  333. centerCoordinate = []
  334. for j in range(len(centerSort[i][1])):
  335. index = np.where(centerSort[i][2] == centerSort[i][1][j])
  336. indexList = index[0].tolist()
  337. indexList.sort() # 默认是升序排序
  338. x = (centerSort[i][3][indexList[0]][0] + centerSort[i][3][indexList[-1]][0]) / 2
  339. y = (centerSort[i][3][indexList[0]][1] + centerSort[i][3][indexList[-1]][1]) / 2
  340. centerCoordinate.append([x, y])
  341. laneCOOR = laneCOOR + centerCoordinate
  342. return laneCOOR
  343. # 二次函数曲线表达式:x = a*(y**2) + b*y + c,根据图像中一点的y坐标求二次曲线中的x坐标
  344. def predict(a, b, c, y):
  345. x = a * (y**2) + b * y + c
  346. return x
  347. def mixNoParking_road_postprocess(dets, mask, pars):
  348. """
  349. 对于字典traffic_dict中的各个键,说明如下:
  350. RoadArea:speedRoad的最小外接矩形的面积
  351. vehicleCOOR:是一个列表,用于存储被检测出的vehicle的坐标(vehicle检测模型)
  352. roundness:圆度 ,lane的长与宽的比率,作为判定是否为车道线的标准之一
  353. laneArea:车道线的最小外接矩形的面积
  354. ZoomFactor:图像在H和W方向上的缩放因子,其值小于1
  355. fitOrder:多点拟合曲线的阶数
  356. 最终输出格式:[[x0, y0, x1, y1, 车辆得分, cls, 违章停车得分, 违章类别], ...]
  357. 违章类别:0表示正常车辆,1表示违章车辆
  358. """
  359. det_cors = []
  360. for bb in dets:
  361. det_cors.append((int(bb[0]), int(bb[1])))
  362. det_cors.append((int(bb[2]), int(bb[3])))
  363. # print('###line341:', det_cors)
  364. pars['vehicleCOOR'] = det_cors
  365. H, W = mask.shape[0:2] # mask的分辨率为360x640
  366. scaleH = pars['modelSize'][1] / H # 自适应调整缩放比例
  367. scaleW = pars['modelSize'][0] / W
  368. pars['ZoomFactor'] = {'x': scaleW, 'y': scaleH}
  369. new_hw = [int(H * scaleH), int(W * scaleW)]
  370. mask = cv2.resize(mask, (new_hw[1], new_hw[0]))
  371. if len(mask.shape) == 3:
  372. mask = mask[:, :, 0]
  373. t1 = time.time()
  374. imgRoad = mask.copy()
  375. imgVehicle = mask.copy()
  376. lane_line = mask.copy()
  377. # 将vehicle和lane过滤掉,只包含背景和speedRoad
  378. imgRoad[imgRoad == 2] = 0
  379. imgRoad[imgRoad == 3] = 1
  380. # 将speedRoad和lane过滤掉,只保留vehicle和背景
  381. imgVehicle[imgVehicle != 2] = 0
  382. # 将speedRoad和vehicle过滤掉,只保留lane和背景
  383. lane_line[lane_line < 3] = 0
  384. imgRoad = cv2.cvtColor(np.uint8(imgRoad), cv2.COLOR_RGB2BGR) # 道路
  385. imgVehicle = cv2.cvtColor(np.uint8(imgVehicle), cv2.COLOR_RGB2BGR) # 车辆
  386. lane_line = cv2.cvtColor(np.uint8(lane_line), cv2.COLOR_RGB2BGR)
  387. # 对车道线进行膨胀操作
  388. # kernel = np.ones((3, 3), np.uint8) # 膨胀范围
  389. # lane_line = cv2.dilate(lane_line, kernel, iterations=2) # 迭代次数为2
  390. t2 = time.time()
  391. img1 = cv2.cvtColor(imgRoad, cv2.COLOR_BGR2GRAY)
  392. roadContours, hierarchy = cv2.findContours(img1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  393. t3 = time.time()
  394. # 存储所有speedRoad的contours信息
  395. allRoadCnt = storageRoad(roadContours, pars)
  396. t4 = time.time()
  397. img3 = cv2.cvtColor(lane_line, cv2.COLOR_BGR2GRAY)
  398. laneContours, hierarchy = cv2.findContours(img3, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  399. # 存储所有lane的contours信息
  400. laneNumber, newLaneContours = storageLane(laneContours, pars)
  401. t5 = time.time()
  402. if laneNumber >= 2:
  403. laneInfo, delRightLane, delLeftLane = detLine(newLaneContours)
  404. t6 = time.time()
  405. # 存储所有vehicle的信息, dets中存储的是正常车辆的信息
  406. dets, vehicleBD, unnormVehicle, normVehicleCOOR = storageVehicle(pars, imgVehicle, dets)
  407. t7 = time.time()
  408. leftLaneCOOR = targetCOOR(laneInfo, delRightLane)
  409. rightLaneCOOR = targetCOOR(laneInfo, delLeftLane)
  410. rightLaneCOOR = np.array(rightLaneCOOR)
  411. rightX = rightLaneCOOR[:, 0]
  412. rightY = rightLaneCOOR[:, 1]
  413. leftLaneCOOR = np.array(leftLaneCOOR)
  414. leftX = leftLaneCOOR[:, 0]
  415. leftY = leftLaneCOOR[:, 1]
  416. # a_r,b_r,c_r分别是:最右侧车道线簇拟合的二次函数的二次项系数,一次项系数,和常数项
  417. 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]
  418. # a_l,b_l,c_l分别是:最左侧车道线簇拟合的二次函数的二次项系数,一次项系数,和常数项
  419. 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]
  420. # 以下四行代码用于在后处理函数外画图
  421. finalLane = []
  422. abc = [a_l, b_l, c_l, a_r, b_r, c_r] # abc中存储的是最左侧和最右侧二次函数的各项系数
  423. finalLane.append(rightLaneCOOR)
  424. finalLane.append(leftLaneCOOR)
  425. # 计算违停得分
  426. t8 = time.time()
  427. targetList = IllegalParkScore1(vehicleBD, allRoadCnt, dets, unnormVehicle, normVehicleCOOR, a_l, b_l, c_l, a_r, b_r, c_r)
  428. t9 = time.time()
  429. time_infos = 'postTime:%.2f(分割时间:%.2f, findContours:%.2f, ruleJudge:%.2f, storageRoad:%.2f, detLane:%.2f, storageLane:%.2f, storageVehicle:%.2f, fitLine:%.2f, IllegalParkScore1:%.2f)' % (
  430. get_ms(t9, t1), get_ms(t2, t1), get_ms(t3, t2), get_ms(t9, t3), get_ms(t4, t3), get_ms(t6, t5),
  431. get_ms(t5, t4), get_ms(t7, t6), get_ms(t8, t7), get_ms(t9, t8))
  432. # print('####line445:', targetList)
  433. # return targetList, time_infos, finalLane, lane_line, abc
  434. return targetList, time_infos
  435. else:
  436. dets, vehicleBD, unnormVehicle, normVehicleCOOR = storageVehicle(pars, imgVehicle, dets)
  437. t6 = time.time()
  438. targetList = IllegalParkScore2(vehicleBD, dets, unnormVehicle)
  439. t7 = time.time()
  440. time_infos = 'postTime:%.2f(分割时间:%.2f, findContours:%.2f, ruleJudge:%.2f, storageRoad:%.2f, storageLane:%.2f, storageVehicle:%.2f, IllegalParkScore2:%.2f)' % (
  441. 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))
  442. # print('####line456:', targetList)
  443. return targetList, time_infos