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

165 lines
5.9KB

  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. t0=time.time()
  84. '''1、最大分割水域作为判断依据'''
  85. img_gray = cv2.cvtColor(_mask_cv, cv2.COLOR_BGR2GRAY) if len(_mask_cv.shape)==3 else _mask_cv
  86. contours, thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  87. # 寻找轮廓(多边界)
  88. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, 2)
  89. contour_info = []
  90. for c in contours:
  91. contour_info.append((
  92. c,
  93. cv2.isContourConvex(c),
  94. cv2.contourArea(c),
  95. ))
  96. contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
  97. max_contour = contour_info[0]
  98. #print(max_contour)
  99. t1=time.time()
  100. '''2、preds中head+person取出,boat取出。'''
  101. init_head_person=[]
  102. init_boat = []
  103. for i in range(len(preds)):
  104. #if preds[i][4]=='head' or preds[i][4]=='person':
  105. if preds[i][0]==0 or preds[i][0]==1:
  106. init_head_person.append(preds[i])
  107. else:
  108. init_boat.append(preds[i])
  109. t2=time.time()
  110. '''3、preds中head+person,通过1中水域过滤'''
  111. init_head_person_filterwater=init_head_person
  112. final_head_person_filterwater=[]
  113. for i in range(len(init_head_person_filterwater)):
  114. center_x, center_y=center_coordinate(init_head_person_filterwater[i])
  115. flag = cv2.pointPolygonTest(max_contour[0], (center_x, center_y), False) #若为False,会找点是否在内,外,或轮廓上(相应返回+1, -1, 0)。
  116. if flag==1:
  117. final_head_person_filterwater.append(init_head_person_filterwater[i])
  118. else:
  119. pass
  120. t3=time.time()
  121. '''4、水域过滤后的head+person,再通过船舶范围过滤'''
  122. init_head_person_filterboat=final_head_person_filterwater
  123. # final_head_person_filterboat=[]
  124. #获取船舶范围
  125. boat_contour=[]
  126. for i in range(len(init_boat)):
  127. boundbxs1=[init_boat[i][0],init_boat[i][1],init_boat[i][2],init_boat[i][3]]
  128. contour_temp=fourcorner_coordinate(boundbxs1) #得到boat预测框的顺序contour
  129. contour_temp_=np.array(contour_temp)
  130. contour_temp_=np.float32(contour_temp_)
  131. boat_contour.append(np.array(contour_temp_))
  132. # 遍历船舶范围,取出在船舶范围内的head和person(可能有重复元素)
  133. list_headperson_inboat=[]
  134. for i in range(len(init_head_person_filterboat)):
  135. for j in range(len(boat_contour)):
  136. center_x, center_y=center_coordinate(init_head_person_filterboat[i])
  137. # yyyyyyyy=boat_contour[j]
  138. flag = cv2.pointPolygonTest(boat_contour[j], (center_x, center_y), False) #若为False,会找点是否在内,外,或轮廓上(相应返回+1, -1, 0)。
  139. if flag==1:
  140. list_headperson_inboat.append(init_head_person_filterboat[i])
  141. else:
  142. pass
  143. if len(list_headperson_inboat)==0:
  144. pass
  145. else:
  146. list_headperson_inboat=remove_sameeleme_inalist(list_headperson_inboat) #将重复嵌套列表元素删除
  147. # 过滤船舶范围内的head和person
  148. final_head_person_filterboat=remove_simivalue(init_head_person_filterboat,list_headperson_inboat)
  149. t4=time.time()
  150. timeInfos = '%.1f (step1:%.1f step2:%.2f step3:%.3f step4:%.1f) ' %( ms(t4,t0), ms(t1,t0),ms(t2,t1),ms(t3,t2),ms(t4,t3) )
  151. return final_head_person_filterboat,timeInfos #返回最终绘制的结果图、最终落水人员(坐标、类别、置信度)