platform_remove_detect.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import cv2
  2. import torch
  3. import time
  4. import math
  5. from shapely.geometry import box, Polygon
  6. import threading
  7. import numpy as np
  8. from datetime import datetime
  9. from ultralytics import YOLO
  10. from utils.tool import IoU
  11. from globals import stop_event,redis_client
  12. from config import SAVE_IMG_PATH,POST_IMG_PATH4,PLATFORM_SETUP_MODEL,PLATFORM_SETUP_VIDEO_SOURCES
  13. from globals import platform_remove_steps_detect_num,platform_remove_final_result,platform_remove_steps_img,remove_detection_status,remove_detection_timers
  14. def update_detection_status(platform_remove_steps):
  15. global remove_detection_status,remove_detection_timers
  16. current_time = time.time()
  17. for i,nums in enumerate(platform_remove_steps):
  18. if nums>0:
  19. remove_detection_timers[i] = current_time # 更新物体的最后检测时间
  20. remove_detection_status[i] = False # 重置状态为 False
  21. def check_timeout():
  22. current_time = time.time()
  23. for i in range(14):
  24. if current_time - remove_detection_timers[i] > 10: # 如果超过10秒未检测到
  25. remove_detection_status[i] = True # 将状态置为 True
  26. # return True
  27. # return False
  28. def init_platform_remove_detection():
  29. global remove_detection_timers,remove_detection_status
  30. remove_detection_timers = [time.time()] * 14 # 初始化计时器
  31. remove_detection_status = [False]*14 # 初始化检
  32. redis_client.delete("platform_remove_order")
  33. for i in range(1, 14):
  34. redis_client.delete(f"platform_remove_{i}_img")
  35. def start_platform_remove_detection(start_events):
  36. threads = []
  37. for video_source in PLATFORM_SETUP_VIDEO_SOURCES:
  38. event = threading.Event()
  39. start_events.append(event)
  40. thread = threading.Thread(target=process_video, args=(PLATFORM_SETUP_MODEL,video_source,event))
  41. threads.append(thread)
  42. thread.daemon=True
  43. thread.start()
  44. # Wait for all threads to complete
  45. for thread in threads:
  46. thread.join()
  47. print("平台拆除子线程启动")
  48. # Function to process video with YOLO model
  49. def process_video(model_path, video_source,start_event):
  50. # Load YOLO model
  51. model = YOLO(model_path)
  52. cap = cv2.VideoCapture(video_source)
  53. while cap.isOpened():
  54. if stop_event.is_set():#控制停止推理
  55. #del model
  56. break
  57. # Read a frame from the video
  58. success, frame = cap.read()
  59. if success:
  60. # Run YOLOv8 inference on the frame
  61. if cap.get(cv2.CAP_PROP_POS_FRAMES) % 25 != 0:
  62. continue
  63. results = model.predict(frame,conf=0.6,verbose=False,task='obb')
  64. LEVLEL0_REGION = np.array([[1167, 908], [931, 1153], [1962, 1187], [2034, 936]], np.int32)
  65. LEVLEL1_REGION = np.array([[1163, 574], [859, 818], [1969, 828], [2060, 588]], np.int32)
  66. LEVLEL2_REGION = np.array([[1147, 263], [778, 438], [1953, 442], [2044, 248]], np.int32)
  67. LEVLEL3_REGION = np.array([[1142, 34], [793, 163], [1945, 112], [2050, 17]], np.int32)
  68. DIAGONAL_REGION = np.array([[838, 147], [845, 1145], [1935, 1147], [1943, 147]], np.int32)
  69. global platform_remove_steps_detect_num,remove_detection_status,platform_remove_steps_img
  70. # 每十秒归零 platform_remove_steps_detect_num
  71. # current_time = time.time()
  72. # if not hasattr(process_video, 'last_reset_time'):
  73. # process_video.last_reset_time = current_time
  74. # if current_time - process_video.last_reset_time >= 10:
  75. # platform_remove_steps_detect_num = [0] * 14
  76. # process_video.last_reset_time = current_time
  77. for r in results:
  78. boxes=r.obb.xyxyxyxy
  79. boxes1=r.obb.xywhr
  80. confidences=r.obb.conf
  81. classes=r.obb.cls
  82. # 0: montant
  83. # 1: diagonal
  84. # 2: wheel
  85. # 3: vertical_bar
  86. # 4: horizontal_bar
  87. # 5: ladder
  88. # 6: toe_board
  89. # 7: scaffold
  90. # 1=轮子
  91. # 2=立杆
  92. # 3=纵向扫地杆
  93. # 4=横向扫地杆
  94. # 5=纵向水平杆1
  95. # 6=横向水平杆1
  96. # 7=纵向水平杆2
  97. # 8=横向水平杆2
  98. # 9=斜撑
  99. # 10=爬梯
  100. # 11=脚手板
  101. # 12=挡脚板
  102. # 13=纵向水平杆3
  103. # 14=横向水平杆3
  104. platform_remove_steps = [0] * 14
  105. hengxianggan=0
  106. zongxinaggan=0
  107. for i in range(len(boxes)):
  108. confidence = confidences[i].item()
  109. cls = int(classes[i].item())
  110. label = model.names[cls]
  111. #print(boxes[i].tolist())
  112. box_coords = boxes[i].tolist()
  113. x_center = (box_coords[0][0] + box_coords[1][0]+box_coords[2][0]+box_coords[3][0]) / 4
  114. y_center=(box_coords[0][1] + box_coords[1][1]+box_coords[2][1]+box_coords[3][1]) / 4
  115. center_point = (int(x_center), int(y_center))
  116. rotation= math.degrees(boxes1[i][4])
  117. if label=="wheel":#轮子
  118. # platform_remove_steps_detect_num[0]+=1
  119. # if platform_remove_steps_detect_num[0]>3:
  120. platform_remove_steps[0]+=1
  121. elif label=="bar":#立杆
  122. # platform_setup_steps_detect_num[1]+=1
  123. # if platform_setup_steps_detect_num[1]>3:
  124. # platform_setup_steps[1]+=1
  125. if video_source == PLATFORM_SETUP_VIDEO_SOURCES[0]:
  126. if 70 < rotation < 110:
  127. platform_remove_steps[1] += 1 # 立杆
  128. elif rotation < 10 or rotation > 170:
  129. hengxianggan += 1 # 横向杆
  130. elif 135 < rotation < 150:
  131. platform_remove_steps[8] += 1 # 斜杆
  132. else:
  133. zongxinaggan += 1 # 纵向杆
  134. elif video_source == PLATFORM_SETUP_VIDEO_SOURCES[1]:
  135. if 70 < rotation < 110:
  136. platform_remove_steps[1] += 1 # 立杆
  137. elif 5 < rotation < 25:
  138. hengxianggan += 1 # 横向杆
  139. elif 40 < rotation < 60:
  140. platform_remove_steps[8] += 1 # 斜杆
  141. else:
  142. zongxinaggan += 1 # 纵向杆
  143. # elif label=="montant":#立杆
  144. # platform_remove_steps_detect_num[1]+=1
  145. # if platform_remove_steps_detect_num[1]>3:
  146. # platform_remove_steps[1]+=1
  147. # elif label=="vertical_bar":#水平横杆,纵杆
  148. # is_inside0 = cv2.pointPolygonTest(LEVLEL0_REGION.reshape((-1, 1, 2)), center_point, False)
  149. # is_inside1 = cv2.pointPolygonTest(LEVLEL1_REGION.reshape((-1, 1, 2)), center_point, False)
  150. # is_inside2 = cv2.pointPolygonTest(LEVLEL2_REGION.reshape((-1, 1, 2)), center_point, False)
  151. # is_inside3 = cv2.pointPolygonTest(LEVLEL3_REGION.reshape((-1, 1, 2)), center_point, False)
  152. # #print(is_inside)
  153. # if is_inside0>=0 :
  154. # platform_remove_steps_detect_num[2]+=1
  155. # if platform_remove_steps_detect_num[2]>3:
  156. # platform_remove_steps[2]+=1 #表示纵向扫地杆
  157. # elif is_inside1>=0:
  158. # platform_remove_steps_detect_num[4]+=1
  159. # if platform_remove_steps_detect_num[4]>3:
  160. # platform_remove_steps[4]+=1#5=纵向水平杆1
  161. # elif is_inside2>=0:
  162. # platform_remove_steps_detect_num[6]+=1
  163. # if platform_remove_steps_detect_num[6]>3:
  164. # platform_remove_steps[6]+=1#7=纵向水平杆2
  165. # elif is_inside3>=0:
  166. # platform_remove_steps_detect_num[12]+=1
  167. # if platform_remove_steps_detect_num[12]>3:
  168. # platform_remove_steps[12]+=1#13=纵向水平杆3
  169. # elif label=="horizontal_bar":#水平纵杆
  170. # is_inside0 = cv2.pointPolygonTest(LEVLEL0_REGION.reshape((-1, 1, 2)), center_point, False)
  171. # is_inside1 = cv2.pointPolygonTest(LEVLEL1_REGION.reshape((-1, 1, 2)), center_point, False)
  172. # is_inside2 = cv2.pointPolygonTest(LEVLEL2_REGION.reshape((-1, 1, 2)), center_point, False)
  173. # is_inside3 = cv2.pointPolygonTest(LEVLEL3_REGION.reshape((-1, 1, 2)), center_point, False)
  174. # #print(is_inside)
  175. # if is_inside0>=0 :
  176. # platform_remove_steps_detect_num[3]+=1
  177. # if platform_remove_steps_detect_num[3]>3:
  178. # platform_remove_steps[3]+=1#4=横向扫地杆
  179. # elif is_inside1>=0:
  180. # platform_remove_steps_detect_num[5]+=1
  181. # if platform_remove_steps_detect_num[5]>3:
  182. # platform_remove_steps[5]+=1#6=横向水平杆1
  183. # elif is_inside2>=0:
  184. # platform_remove_steps_detect_num[7]+=1
  185. # if platform_remove_steps_detect_num[7]>3:
  186. # platform_remove_steps[7]+=1# 8=横向水平杆2
  187. # elif is_inside3>=0:
  188. # platform_remove_steps_detect_num[13]+=1
  189. # if platform_remove_steps_detect_num[13]>3:
  190. # platform_remove_steps[13]+=1#14=横向水平杆3
  191. # elif label=="diagonal":#斜撑
  192. # is_inside = cv2.pointPolygonTest(DIAGONAL_REGION.reshape((-1, 1, 2)), center_point, False)
  193. # if is_inside>=0:
  194. # platform_remove_steps_detect_num[8]+=1
  195. # if platform_remove_steps_detect_num[8]>3:
  196. # platform_remove_steps[8]+=1# 9=斜撑
  197. elif label=="ladder":#梯子
  198. #10=爬梯
  199. # platform_remove_steps_detect_num[9]+=1
  200. # if platform_remove_steps_detect_num[9]>3:
  201. platform_remove_steps[9]+=1
  202. elif label=="scaffold":#脚手板
  203. #11=脚手板
  204. # platform_remove_steps_detect_num[10]+=1
  205. # if platform_remove_steps_detect_num[10]>3:
  206. platform_remove_steps[10]+=1
  207. elif label=="toe_board":#档脚板
  208. #12=挡脚板
  209. # platform_remove_steps_detect_num[11]+=1
  210. # if platform_remove_steps_detect_num[11]>3:
  211. platform_remove_steps[11]+=1
  212. if hengxianggan >= 2:
  213. platform_remove_steps[3] = 2
  214. if hengxianggan >= 4:
  215. platform_remove_steps[5] = 2
  216. if hengxianggan >= 6:
  217. platform_remove_steps[7] = 2
  218. if hengxianggan >= 8:
  219. platform_remove_steps[13] = 2
  220. if zongxinaggan>=2:
  221. platform_remove_steps[2]=2
  222. if zongxinaggan>=4:
  223. platform_remove_steps[4]=2
  224. if zongxinaggan>=6:
  225. platform_remove_steps[6]=2
  226. if zongxinaggan>=8:
  227. platform_remove_steps[12]=2
  228. update_detection_status(platform_remove_steps)
  229. check_timeout()#返回True表示10秒内未检测到某物体,表示该物体拆除完成
  230. for i, status in reversed(list(enumerate(remove_detection_status))):
  231. if status and platform_remove_steps_img[i]==False:
  232. redis_client.rpush("platform_remove_order", i+1)
  233. platform_remove_steps_img[i]=True
  234. save_time=datetime.now().strftime('%Y%m%d_%H%M')
  235. imgpath = f"{SAVE_IMG_PATH}/platform_remove{i+1}_{save_time}.jpg"
  236. post_path= f"{POST_IMG_PATH4}/platform_remove{i+1}_{save_time}.jpg"
  237. annotated_frame = results[0].plot()
  238. cv2.imwrite(imgpath, annotated_frame)
  239. redis_client.set(f"platform_remove_{i+1}_img",post_path)
  240. #redis_client.set(f"platform_remove_{14-i-1}", "1")
  241. start_event.set()
  242. else:
  243. # Break the loop if the end of the video is reached
  244. break
  245. # Release the video capture object and close the display window
  246. cap.release()
  247. if torch.cuda.is_available():
  248. torch.cuda.empty_cache()
  249. del model