platform_remove_detect.py 11 KB

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