落水人员检测
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.

219 lines
8.5KB

  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_height=int(original_height/zoom_factor)
  88. zoom_width=int(original_width/zoom_factor)
  89. _mask_cv = cv2.resize(_mask_cv, (zoom_width,zoom_height)) #缩小原图,宽在前,高在后
  90. t4 = time.time()
  91. img_gray = cv2.cvtColor(_mask_cv, cv2.COLOR_BGR2GRAY) if len(_mask_cv.shape)==3 else _mask_cv #
  92. t5 = time.time()
  93. contours, thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  94. # 寻找轮廓(多边界)
  95. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, 2)
  96. contour_info = []
  97. for c in contours:
  98. contour_info.append((
  99. c,
  100. cv2.isContourConvex(c),
  101. cv2.contourArea(c),
  102. ))
  103. contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
  104. t6 = time.time()
  105. '''新增模块::如果水域为空,则返回原图、无落水人员等。'''
  106. if contour_info==[]:
  107. # final_img=_img_cv
  108. final_head_person_filterwater=[]
  109. timeInfos=0
  110. # return final_img, final_head_person_filterwater
  111. return final_head_person_filterwater,timeInfos
  112. else:
  113. max_contour = contour_info[0]
  114. max_contour=max_contour[0]*zoom_factor# contours恢复原图尺寸
  115. print(max_contour)
  116. t7 = time.time()
  117. '''2.1、preds中head+person取出,boat取出。'''
  118. init_head_person=[]
  119. init_boat = []
  120. for i in range(len(preds)):
  121. if preds[i][4]=='head' or preds[i][4]=='person':
  122. init_head_person.append(preds[i])
  123. else:
  124. init_boat.append(preds[i])
  125. t8 = time.time()
  126. '''新增模块:2.2、preds中head+person取出,过滤掉head与person中指向同一人的部分,保留同一人的person标签。'''
  127. init_head=[]
  128. init_person=[]
  129. #head与person标签分开
  130. for i in range(len(init_head_person)):
  131. if init_head_person[i][4]=='head':
  132. init_head.append(init_head_person[i])
  133. else:
  134. init_person.append(init_head_person[i])
  135. # person的框形成contours
  136. person_contour=[]
  137. for i in range(len(init_person)):
  138. boundbxs_temp=[init_person[i][0],init_person[i][1],init_person[i][2],init_person[i][3]]
  139. contour_temp_person=fourcorner_coordinate(boundbxs_temp) #得到person预测框的顺序contour
  140. contour_temp_person=np.array(contour_temp_person)
  141. contour_temp_person=np.float32(contour_temp_person)
  142. person_contour.append(np.array(contour_temp_person))
  143. # head是否在person的contours内,在说明是同一人,过滤掉。
  144. list_head=[]
  145. for i in range(len(init_head)):
  146. for j in range(len(person_contour)):
  147. center_x, center_y=center_coordinate(init_head[i])
  148. flag = cv2.pointPolygonTest(person_contour[j], (center_x, center_y), False) #若为False,会找点是否在内,外,或轮廓上(相应返回+1, -1, 0)。
  149. if flag==1:
  150. pass
  151. else:
  152. list_head.append(init_head[i])
  153. # person和最终head合并起来
  154. init_head_person_temp=init_person+list_head
  155. '''3、preds中head+person,通过1中水域过滤'''
  156. init_head_person_filterwater=init_head_person_temp
  157. final_head_person_filterwater=[]
  158. for i in range(len(init_head_person_filterwater)):
  159. center_x, center_y=center_coordinate(init_head_person_filterwater[i])
  160. flag = cv2.pointPolygonTest(max_contour, (center_x, center_y), False) #若为False,会找点是否在内,外,或轮廓上(相应返回+1, -1, 0)。
  161. if flag==1:
  162. final_head_person_filterwater.append(init_head_person_filterwater[i])
  163. else:
  164. pass
  165. t9 = time.time()
  166. '''4、水域过滤后的head+person,再通过船舶范围过滤'''
  167. init_head_person_filterboat=final_head_person_filterwater
  168. # final_head_person_filterboat=[]
  169. #获取船舶范围
  170. boat_contour=[]
  171. for i in range(len(init_boat)):
  172. boundbxs1=[init_boat[i][0],init_boat[i][1],init_boat[i][2],init_boat[i][3]]
  173. contour_temp=fourcorner_coordinate(boundbxs1) #得到boat预测框的顺序contour
  174. contour_temp_=np.array(contour_temp)
  175. contour_temp_=np.float32(contour_temp_)
  176. boat_contour.append(np.array(contour_temp_))
  177. t10 = time.time()
  178. # 遍历船舶范围,取出在船舶范围内的head和person(可能有重复元素)
  179. list_headperson_inboat=[]
  180. for i in range(len(init_head_person_filterboat)):
  181. for j in range(len(boat_contour)):
  182. center_x, center_y=center_coordinate(init_head_person_filterboat[i])
  183. # yyyyyyyy=boat_contour[j]
  184. flag = cv2.pointPolygonTest(boat_contour[j], (center_x, center_y), False) #若为False,会找点是否在内,外,或轮廓上(相应返回+1, -1, 0)。
  185. if flag==1:
  186. list_headperson_inboat.append(init_head_person_filterboat[i])
  187. else:
  188. pass
  189. # print('list_headperson_inboat',list_headperson_inboat)
  190. if len(list_headperson_inboat)==0:
  191. pass
  192. else:
  193. list_headperson_inboat=remove_sameeleme_inalist(list_headperson_inboat) #将重复嵌套列表元素删除
  194. # 过滤船舶范围内的head和person
  195. final_head_person_filterboat=remove_simivalue(init_head_person_filterboat,list_headperson_inboat)
  196. final_output_luoshui=final_head_person_filterboat
  197. t11 = time.time()
  198. timeInfos=('存图:%s, 过滤标签:%s ,遍历船舶范围:%s,水域过滤后的head+person:%s,水域过滤:%s,head+person、boat取出:%s,新增如果水域为空:%s,找contours:%s,图像改变:%s'
  199. %((t11-t10) * 1000,(t10-t9) * 1000,(t9-t8) * 1000,(t8-t7) * 1000,(t7-t6) * 1000,(t6-t5) * 1000,(t5-t4) * 1000 ) )
  200. return final_output_luoshui,timeInfos #返回最终绘制的结果图、最终落水人员(坐标、类别、置信度)