Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

343 lines
20KB

  1. # -*- coding: utf-8 -*-
  2. from concurrent.futures import ThreadPoolExecutor
  3. from multiprocessing import Process
  4. from os import getpid
  5. from os.path import join
  6. from time import time, sleep
  7. from traceback import format_exc
  8. import cv2
  9. import psutil
  10. from loguru import logger
  11. from enums.ExceptionEnum import ExceptionType
  12. from exception.CustomerException import ServiceException
  13. from util import ImageUtils
  14. from util.Cv2Utils import video_conjuncing, write_or_video, write_ai_video, push_video_stream, close_all_p
  15. from util.ImageUtils import url2Array, add_water_pic
  16. from util.LogUtils import init_log
  17. from util.PlotsUtils import draw_painting_joint, xywh2xyxy2
  18. from util.QueUtil import get_no_block_queue, put_queue, clear_queue
  19. class PushStreamProcess2(Process):
  20. __slots__ = ("_msg", "_push_queue", "_image_queue", '_push_ex_queue', '_hb_queue', "_context")
  21. def __init__(self, *args):
  22. super().__init__()
  23. # 传参
  24. self._msg, self._push_queue, self._image_queue, self._push_ex_queue, self._hb_queue, self._context = args
  25. def build_logo_url(self):
  26. logo = None
  27. if self._context["video"]["video_add_water"]:
  28. logo = self._msg.get("logo_url")
  29. if logo:
  30. logo = url2Array(logo, enable_ex=False)
  31. if logo is None:
  32. logo = cv2.imread(join(self._context['base_dir'], "image/logo.png"), -1)
  33. self._context["logo"] = logo
  34. class OnPushStreamProcess2(PushStreamProcess2):
  35. __slots__ = ()
  36. def run(self):
  37. msg, context = self._msg, self._context
  38. self.build_logo_url()
  39. request_id = msg["request_id"]
  40. base_dir, env = context["base_dir"], context['env']
  41. push_queue, image_queue, push_ex_queue, hb_queue = self._push_queue, self._image_queue, self._push_ex_queue, \
  42. self._hb_queue
  43. orFilePath, aiFilePath, logo = context["orFilePath"], context["aiFilePath"], context["logo"]
  44. or_video_file, ai_video_file, push_p, push_url = None, None, None, msg["push_url"]
  45. service_timeout = int(context["service"]["timeout"]) + 120
  46. frame_score = context["service"]["filter"]["frame_score"]
  47. ex = None
  48. ex_status = True
  49. high_score_image = {}
  50. # 相似度, 默认值0.65
  51. similarity = context["service"]["filter"]["similarity"]
  52. # 图片相似度开关
  53. picture_similarity = bool(context["service"]["filter"]["picture_similarity"])
  54. frame_step = int(context["service"]["filter"]["frame_step"])
  55. try:
  56. init_log(base_dir, env)
  57. logger.info("开始启动推流进程!requestId:{}", request_id)
  58. with ThreadPoolExecutor(max_workers=3) as t:
  59. # 定义三种推流、写原视频流、写ai视频流策略
  60. # 第一个参数时间, 第二个参数重试次数
  61. p_push_status, or_write_status, ai_write_status = [0, 0], [0, 0], [0, 0]
  62. start_time = time()
  63. while True:
  64. # 检测推流执行超时时间
  65. if time() - start_time > service_timeout:
  66. logger.error("推流超时, requestId: {}", request_id)
  67. raise ServiceException(ExceptionType.PUSH_STREAM_TIMEOUT_EXCEPTION.value[0],
  68. ExceptionType.PUSH_STREAM_TIMEOUT_EXCEPTION.value[1])
  69. # 系统由于各种问题可能会杀死内存使用多的进程, 自己杀掉自己
  70. if psutil.Process(getpid()).ppid() == 1:
  71. ex_status = False
  72. logger.info("推流进程检测到父进程异常停止, 自动停止推流进程, requestId: {}", request_id)
  73. for q in [push_queue, image_queue, push_ex_queue, hb_queue]:
  74. clear_queue(q)
  75. break
  76. # 获取推流的视频帧
  77. push_r = get_no_block_queue(push_queue)
  78. if push_r is not None:
  79. # [(1, ...] 视频帧操作
  80. # [(2, 操作指令)] 指令操作
  81. if push_r[0] == 1:
  82. # 如果是多模型push_objs数组可能包含[模型1识别数组, 模型2识别数组, 模型3识别数组]
  83. frame_list, frame_index_list, all_frames, draw_config, push_objs = push_r[1]
  84. # 处理每一帧图片
  85. for i, frame in enumerate(frame_list):
  86. # 复制帧用来画图
  87. copy_frame = frame.copy()
  88. # 所有问题记录字典
  89. det_xywh, thread_p = {}, []
  90. # [模型1识别数组, 模型2识别数组, 模型3识别数组]
  91. for s_det_list in push_objs:
  92. code, det_result = s_det_list[0], s_det_list[1][i]
  93. if len(det_result) > 0:
  94. font_config, allowedList = draw_config["font_config"], draw_config[code]["allowedList"]
  95. rainbows, label_arrays = draw_config[code]["rainbows"], draw_config[code]["label_arrays"]
  96. for qs in det_result:
  97. box, score, cls = xywh2xyxy2(qs)
  98. if cls not in allowedList or score < frame_score:
  99. continue
  100. label_array, color = label_arrays[cls], rainbows[cls]
  101. rr = t.submit(draw_painting_joint, box, copy_frame, label_array, score, color, font_config)
  102. thread_p.append(rr)
  103. if det_xywh.get(code) is None:
  104. det_xywh[code] = {}
  105. cd = det_xywh[code].get(cls)
  106. if cd is None:
  107. det_xywh[code][cls] = [[cls, box, score, label_array, color]]
  108. else:
  109. det_xywh[code][cls].append([cls, box, score, label_array, color])
  110. if logo:
  111. frame = add_water_pic(frame, logo, request_id)
  112. copy_frame = add_water_pic(copy_frame, logo, request_id)
  113. if len(thread_p) > 0:
  114. for r in thread_p:
  115. r.result()
  116. frame_merge = video_conjuncing(frame, copy_frame)
  117. # 写原视频到本地
  118. write_or_video_result = t.submit(write_or_video, frame, orFilePath, or_video_file,
  119. or_write_status, request_id)
  120. # 写识别视频到本地
  121. write_ai_video_result = t.submit(write_ai_video, frame_merge, aiFilePath,
  122. ai_video_file, ai_write_status, request_id)
  123. push_p_result = t.submit(push_video_stream, frame_merge, push_p, push_url,
  124. p_push_status,
  125. request_id)
  126. if len(det_xywh) > 0:
  127. flag = True
  128. if picture_similarity and len(high_score_image) > 0:
  129. hash1 = ImageUtils.dHash(high_score_image.get("or_frame"))
  130. hash2 = ImageUtils.dHash(frame)
  131. dist = ImageUtils.Hamming_distance(hash1, hash2)
  132. similarity_1 = 1 - dist * 1.0 / 64
  133. if similarity_1 >= similarity:
  134. flag = False
  135. if len(high_score_image) > 0:
  136. diff_frame_num = frame_index_list[i] - high_score_image.get("current_frame")
  137. if diff_frame_num < frame_step:
  138. flag = False
  139. if flag:
  140. high_score_image["or_frame"] = frame
  141. high_score_image["current_frame"] = frame_index_list[i]
  142. put_queue(image_queue, (1, [det_xywh, frame, frame_index_list[i], all_frames, draw_config["font_config"]]))
  143. push_p = push_p_result.result(timeout=60)
  144. ai_video_file = write_ai_video_result.result(timeout=60)
  145. or_video_file = write_or_video_result.result(timeout=60)
  146. # 接收停止指令
  147. if push_r[0] == 2:
  148. if 'stop' == push_r[1]:
  149. logger.info("停止推流线程, requestId: {}", request_id)
  150. break
  151. if 'stop_ex' == push_r[1]:
  152. logger.info("停止推流线程, requestId: {}", request_id)
  153. ex_status = False
  154. break
  155. del push_r
  156. else:
  157. sleep(1)
  158. except ServiceException as s:
  159. logger.error("推流进程异常:{}, requestId:{}", s.msg, request_id)
  160. ex = s.code, s.msg
  161. except Exception:
  162. logger.error("推流进程异常:{}, requestId:{}", format_exc(), request_id)
  163. ex = ExceptionType.SERVICE_INNER_EXCEPTION.value[0], ExceptionType.SERVICE_INNER_EXCEPTION.value[1]
  164. finally:
  165. # 关闭推流管, 原视频写对象, 分析视频写对象
  166. close_all_p(push_p, or_video_file, ai_video_file, request_id)
  167. if ex:
  168. code, msg = ex
  169. put_queue(push_ex_queue, (1, code, msg), timeout=2)
  170. else:
  171. if ex_status:
  172. # 关闭推流的时候, 等待1分钟图片队列处理完,如果1分钟内没有处理完, 清空图片队列, 丢弃没有上传的图片
  173. c_time = time()
  174. while time() - c_time < 60:
  175. if image_queue.qsize() == 0 or image_queue.empty():
  176. break
  177. sleep(2)
  178. for q in [push_queue, image_queue, hb_queue]:
  179. clear_queue(q)
  180. logger.info("推流进程停止完成!requestId:{}", request_id)
  181. class OffPushStreamProcess2(PushStreamProcess2):
  182. __slots__ = ()
  183. def run(self):
  184. self.build_logo_url()
  185. msg, context = self._msg, self._context
  186. request_id = msg["request_id"]
  187. base_dir, env = context["base_dir"], context['env']
  188. push_queue, image_queue, push_ex_queue, hb_queue = self._push_queue, self._image_queue, self._push_ex_queue, \
  189. self._hb_queue
  190. aiFilePath, logo = context["aiFilePath"], context["logo"]
  191. ai_video_file, push_p, push_url = None, None, msg["push_url"]
  192. service_timeout = int(context["service"]["timeout"]) + 120
  193. frame_score = context["service"]["filter"]["frame_score"]
  194. ex = None
  195. ex_status = True
  196. high_score_image = {}
  197. # 相似度, 默认值0.65
  198. similarity = context["service"]["filter"]["similarity"]
  199. # 图片相似度开关
  200. picture_similarity = bool(context["service"]["filter"]["picture_similarity"])
  201. frame_step = int(context["service"]["filter"]["frame_step"])
  202. try:
  203. init_log(base_dir, env)
  204. logger.info("开始启动离线推流进程!requestId:{}", request_id)
  205. with ThreadPoolExecutor(max_workers=2) as t:
  206. # 定义三种推流、写原视频流、写ai视频流策略
  207. # 第一个参数时间, 第二个参数重试次数
  208. p_push_status, ai_write_status = [0, 0], [0, 0]
  209. start_time = time()
  210. while True:
  211. # 检测推流执行超时时间
  212. if time() - start_time > service_timeout:
  213. logger.error("离线推流超时, requestId: {}", request_id)
  214. raise ServiceException(ExceptionType.PUSH_STREAM_TIMEOUT_EXCEPTION.value[0],
  215. ExceptionType.PUSH_STREAM_TIMEOUT_EXCEPTION.value[1])
  216. # 系统由于各种问题可能会杀死内存使用多的进程, 自己杀掉自己
  217. if psutil.Process(getpid()).ppid() == 1:
  218. ex_status = False
  219. logger.info("离线推流进程检测到父进程异常停止, 自动停止推流进程, requestId: {}", request_id)
  220. for q in [push_queue, image_queue, push_ex_queue, hb_queue]:
  221. clear_queue(q)
  222. break
  223. # 获取推流的视频帧
  224. push_r = get_no_block_queue(push_queue)
  225. if push_r is not None:
  226. # [(1, ...] 视频帧操作
  227. # [(2, 操作指令)] 指令操作
  228. if push_r[0] == 1:
  229. frame_list, frame_index_list, all_frames, draw_config, push_objs = push_r[1]
  230. # 处理每一帧图片
  231. for i, frame in enumerate(frame_list):
  232. if frame_index_list[i] % 300 == 0 and frame_index_list[i] <= all_frames:
  233. task_process = "%.2f" % (float(frame_index_list[i]) / float(all_frames))
  234. put_queue(hb_queue, {"hb_value": task_process}, timeout=2)
  235. # 复制帧用来画图
  236. copy_frame = frame.copy()
  237. # 所有问题记录字典
  238. det_xywh, thread_p = {}, []
  239. for s_det_list in push_objs:
  240. code, det_result = s_det_list[0], s_det_list[1][i]
  241. if len(det_result) > 0:
  242. font_config, allowedList = draw_config["font_config"], draw_config[code]["allowedList"]
  243. rainbows, label_arrays = draw_config[code]["rainbows"], draw_config[code]["label_arrays"]
  244. for qs in det_result:
  245. box, score, cls = xywh2xyxy2(qs)
  246. if cls not in allowedList or score < frame_score:
  247. continue
  248. label_array, color = label_arrays[cls], rainbows[cls]
  249. rr = t.submit(draw_painting_joint, box, copy_frame, label_array, score, color, font_config)
  250. thread_p.append(rr)
  251. if det_xywh.get(code) is None:
  252. det_xywh[code] = {}
  253. cd = det_xywh[code].get(cls)
  254. if cd is None:
  255. det_xywh[code][cls] = [[cls, box, score, label_array, color]]
  256. else:
  257. det_xywh[code][cls].append([cls, box, score, label_array, color])
  258. if logo:
  259. frame = add_water_pic(frame, logo, request_id)
  260. copy_frame = add_water_pic(copy_frame, logo, request_id)
  261. if len(thread_p) > 0:
  262. for r in thread_p:
  263. r.result()
  264. frame_merge = video_conjuncing(frame, copy_frame)
  265. # 写识别视频到本地
  266. write_ai_video_result = t.submit(write_ai_video, frame_merge, aiFilePath,
  267. ai_video_file,
  268. ai_write_status, request_id)
  269. push_p_result = t.submit(push_video_stream, frame_merge, push_p, push_url,
  270. p_push_status,
  271. request_id)
  272. if len(det_xywh) > 0:
  273. flag = True
  274. if picture_similarity and len(high_score_image) > 0:
  275. hash1 = ImageUtils.dHash(high_score_image.get("or_frame"))
  276. hash2 = ImageUtils.dHash(frame)
  277. dist = ImageUtils.Hamming_distance(hash1, hash2)
  278. similarity_1 = 1 - dist * 1.0 / 64
  279. if similarity_1 >= similarity:
  280. flag = False
  281. if len(high_score_image) > 0:
  282. diff_frame_num = frame_index_list[i] - high_score_image.get("current_frame")
  283. if diff_frame_num < frame_step:
  284. flag = False
  285. if flag:
  286. high_score_image["or_frame"] = frame
  287. high_score_image["current_frame"] = frame_index_list[i]
  288. put_queue(image_queue, (1, [det_xywh, frame, frame_index_list[i], all_frames, draw_config["font_config"]]))
  289. push_p = push_p_result.result(timeout=60)
  290. ai_video_file = write_ai_video_result.result(timeout=60)
  291. # 接收停止指令
  292. if push_r[0] == 2:
  293. if 'stop' == push_r[1]:
  294. logger.info("停止推流线程, requestId: {}", request_id)
  295. break
  296. if 'stop_ex' == push_r[1]:
  297. logger.info("停止推流线程, requestId: {}", request_id)
  298. ex_status = False
  299. break
  300. del push_r
  301. else:
  302. sleep(1)
  303. except ServiceException as s:
  304. logger.error("推流进程异常:{}, requestId:{}", s.msg, request_id)
  305. ex = s.code, s.msg
  306. except Exception:
  307. logger.error("推流进程异常:{}, requestId:{}", format_exc(), request_id)
  308. ex = ExceptionType.SERVICE_INNER_EXCEPTION.value[0], ExceptionType.SERVICE_INNER_EXCEPTION.value[1]
  309. finally:
  310. # 关闭推流管, 分析视频写对象
  311. close_all_p(push_p, None, ai_video_file, request_id)
  312. if ex:
  313. code, msg = ex
  314. put_queue(push_ex_queue, (1, code, msg), timeout=2)
  315. else:
  316. if ex_status:
  317. # 关闭推流的时候, 等待1分钟图片队列处理完,如果1分钟内没有处理完, 清空图片队列, 丢弃没有上传的图片
  318. c_time = time()
  319. while time() - c_time < 60:
  320. if image_queue.qsize() == 0 or image_queue.empty():
  321. break
  322. sleep(2)
  323. for q in [push_queue, image_queue, hb_queue]:
  324. clear_queue(q)
  325. logger.info("推流进程停止完成!requestId:{}", request_id)