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.

223 lines
8.8KB

  1. import numpy as np
  2. import time,cv2
  3. def ms(t1,t0):
  4. return (t1-t0)*1000.0
  5. def center_coordinate(boundbxs):
  6. '''
  7. 输入:两个对角坐标xyxy
  8. 输出:矩形框重点坐标xy
  9. '''
  10. boundbxs_x1=boundbxs[0]
  11. boundbxs_y1=boundbxs[1]
  12. boundbxs_x2=boundbxs[2]
  13. boundbxs_y2=boundbxs[3]
  14. center_x=0.5*(boundbxs_x1+boundbxs_x2)
  15. center_y=0.5*(boundbxs_y1+boundbxs_y2)
  16. return center_x,center_y
  17. def fourcorner_coordinate(boundbxs):
  18. '''
  19. 输入:两个对角坐标xyxy
  20. 输出:矩形框四个角点坐标,以contours顺序。
  21. '''
  22. boundbxs_x1=boundbxs[0]
  23. boundbxs_y1=boundbxs[1]
  24. boundbxs_x2=boundbxs[2]
  25. boundbxs_y2=boundbxs[3]
  26. wid=boundbxs_x2-boundbxs_x1
  27. hei=boundbxs_y2-boundbxs_y1
  28. boundbxs_x3=boundbxs_x1+wid
  29. boundbxs_y3=boundbxs_y1
  30. boundbxs_x4=boundbxs_x1
  31. boundbxs_y4 = boundbxs_y1+hei
  32. contours_rec=[[boundbxs_x1,boundbxs_y1],[boundbxs_x3,boundbxs_y3],[boundbxs_x2,boundbxs_y2],[boundbxs_x4,boundbxs_y4]]
  33. return contours_rec
  34. def remove_simivalue(list1,list2):
  35. '''
  36. 将list1中属于list2的元素都删除。
  37. 输入:两个嵌套列表
  38. 返回:嵌套列表
  39. '''
  40. list33=list1.copy()
  41. for i in range(len(list1)):
  42. for j in range(len(list2)):
  43. if list2[j] == list1[i]:
  44. # list33.pop(list1[i])
  45. list33.remove(list1[i])
  46. return list33
  47. def remove_sameeleme_inalist(list3):
  48. '''
  49. 将list3中重复嵌套列表元素删除。
  50. 输入:嵌套列表
  51. 返回:嵌套列表
  52. '''
  53. list3=list3
  54. list4=[]
  55. list4.append(list3[0])
  56. for dict in list3:
  57. k=0
  58. for item in list4:
  59. if dict!=item:
  60. k=k+1
  61. else:
  62. break
  63. if k==len(list4):
  64. list4.append(dict)
  65. return list4
  66. def order_points(pts):
  67. ''' sort rectangle points by clockwise '''
  68. sort_x = pts[np.argsort(pts[:, 0]), :]
  69. Left = sort_x[:2, :]
  70. Right = sort_x[2:, :]
  71. # Left sort
  72. Left = Left[np.argsort(Left[:, 1])[::-1], :]
  73. # Right sort
  74. Right = Right[np.argsort(Right[:, 1]), :]
  75. return np.concatenate((Left, Right), axis=0)
  76. def mixDrowing_water_postprocess(preds,_mask_cv,pars ):
  77. '''考虑船上人过滤'''
  78. '''输入:落水人员的结果(类别+坐标)、原图、mask图像
  79. 过程:获得mask的轮廓,判断人员是否在轮廓内。
  80. 在,则保留且绘制;不在,舍弃。
  81. 返回:最终绘制的结果图、最终落水人员(坐标、类别、置信度),
  82. '''
  83. '''1、最大分割水域作为判断依据'''
  84. #zoom_factor=4 #缩小因子设置为4,考虑到numpy中分别遍历xy进行缩放耗时大。
  85. original_height = _mask_cv.shape[0]
  86. original_width=_mask_cv.shape[1]
  87. zoom_factor = original_width/480.0
  88. zoom_height=int(original_height/zoom_factor)
  89. zoom_width=int(original_width/zoom_factor)
  90. _mask_cv = cv2.resize(_mask_cv, (zoom_width,zoom_height)) #缩小原图,宽在前,高在后
  91. t4 = time.time()
  92. img_gray = cv2.cvtColor(_mask_cv, cv2.COLOR_BGR2GRAY) if len(_mask_cv.shape)==3 else _mask_cv #
  93. t5 = time.time()
  94. contours, thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  95. # 寻找轮廓(多边界)
  96. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, 2)
  97. contour_info = []
  98. for c in contours:
  99. contour_info.append((
  100. c,
  101. cv2.isContourConvex(c),
  102. cv2.contourArea(c),
  103. ))
  104. contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
  105. t6 = time.time()
  106. '''新增模块::如果水域为空,则返回原图、无落水人员等。'''
  107. if contour_info==[]:
  108. # final_img=_img_cv
  109. final_head_person_filterwater=[]
  110. timeInfos=0
  111. # return final_img, final_head_person_filterwater
  112. return final_head_person_filterwater,timeInfos
  113. else:
  114. max_contour = contour_info[0]
  115. max_contour=max_contour[0]*zoom_factor# contours恢复原图尺寸
  116. print(max_contour)
  117. t7 = time.time()
  118. '''2.1、preds中head+person取出,boat取出。'''
  119. init_head_person=[]
  120. init_boat = []
  121. for i in range(len(preds)):
  122. if preds[i][4]=='head' or preds[i][4]=='person':
  123. init_head_person.append(preds[i])
  124. else:
  125. init_boat.append(preds[i])
  126. t8 = time.time()
  127. '''新增模块:2.2、preds中head+person取出,过滤掉head与person中指向同一人的部分,保留同一人的person标签。'''
  128. init_head=[]
  129. init_person=[]
  130. #head与person标签分开
  131. for i in range(len(init_head_person)):
  132. if init_head_person[i][4]=='head':
  133. init_head.append(init_head_person[i])
  134. else:
  135. init_person.append(init_head_person[i])
  136. # person的框形成contours
  137. person_contour=[]
  138. for i in range(len(init_person)):
  139. boundbxs_temp=[init_person[i][0],init_person[i][1],init_person[i][2],init_person[i][3]]
  140. contour_temp_person=fourcorner_coordinate(boundbxs_temp) #得到person预测框的顺序contour
  141. contour_temp_person=np.array(contour_temp_person)
  142. contour_temp_person=np.float32(contour_temp_person)
  143. person_contour.append(np.array(contour_temp_person))
  144. # head是否在person的contours内,在说明是同一人,过滤掉。
  145. list_head=[]
  146. for i in range(len(init_head)):
  147. for j in range(len(person_contour)):
  148. center_x, center_y=center_coordinate(init_head[i])
  149. flag = cv2.pointPolygonTest(person_contour[j], (center_x, center_y), False) #若为False,会找点是否在内,外,或轮廓上(相应返回+1, -1, 0)。
  150. if flag==1:
  151. pass
  152. else:
  153. list_head.append(init_head[i])
  154. # person和最终head合并起来
  155. init_head_person_temp=init_person+list_head
  156. '''3、preds中head+person,通过1中水域过滤'''
  157. init_head_person_filterwater=init_head_person_temp
  158. final_head_person_filterwater=[]
  159. for i in range(len(init_head_person_filterwater)):
  160. center_x, center_y=center_coordinate(init_head_person_filterwater[i])
  161. flag = cv2.pointPolygonTest(max_contour, (center_x, center_y), False) #若为False,会找点是否在内,外,或轮廓上(相应返回+1, -1, 0)。
  162. if flag==1:
  163. final_head_person_filterwater.append(init_head_person_filterwater[i])
  164. else:
  165. pass
  166. t9 = time.time()
  167. '''4、水域过滤后的head+person,再通过船舶范围过滤'''
  168. init_head_person_filterboat=final_head_person_filterwater
  169. # final_head_person_filterboat=[]
  170. #获取船舶范围
  171. boat_contour=[]
  172. for i in range(len(init_boat)):
  173. boundbxs1=[init_boat[i][0],init_boat[i][1],init_boat[i][2],init_boat[i][3]]
  174. contour_temp=fourcorner_coordinate(boundbxs1) #得到boat预测框的顺序contour
  175. contour_temp_=np.array(contour_temp)
  176. contour_temp_=np.float32(contour_temp_)
  177. boat_contour.append(np.array(contour_temp_))
  178. t10 = time.time()
  179. # 遍历船舶范围,取出在船舶范围内的head和person(可能有重复元素)
  180. list_headperson_inboat=[]
  181. for i in range(len(init_head_person_filterboat)):
  182. for j in range(len(boat_contour)):
  183. center_x, center_y=center_coordinate(init_head_person_filterboat[i])
  184. # yyyyyyyy=boat_contour[j]
  185. flag = cv2.pointPolygonTest(boat_contour[j], (center_x, center_y), False) #若为False,会找点是否在内,外,或轮廓上(相应返回+1, -1, 0)。
  186. if flag==1:
  187. list_headperson_inboat.append(init_head_person_filterboat[i])
  188. else:
  189. pass
  190. # print('list_headperson_inboat',list_headperson_inboat)
  191. if len(list_headperson_inboat)==0:
  192. pass
  193. else:
  194. list_headperson_inboat=remove_sameeleme_inalist(list_headperson_inboat) #将重复嵌套列表元素删除
  195. # 过滤船舶范围内的head和person
  196. final_head_person_filterboat=remove_simivalue(init_head_person_filterboat,list_headperson_inboat)
  197. final_output_luoshui=final_head_person_filterboat
  198. t11 = time.time()
  199. timeInfos=('存图:%s, 过滤标签:%s ,遍历船舶范围:%s,水域过滤后的head+person:%s,水域过滤:%s,head+person、boat取出:%s,新增如果水域为空:%s,找contours:%s,图像改变:%s'
  200. %((t11-t10) * 1000,(t10-t9) * 1000,(t9-t8) * 1000,(t8-t7) * 1000,(t7-t6) * 1000,(t6-t5) * 1000,(t5-t4) * 1000 ) )
  201. return final_output_luoshui,timeInfos #返回最终绘制的结果图、最终落水人员(坐标、类别、置信度)