def handle_image(frame_msg, frame_step): | def handle_image(frame_msg, frame_step): | ||||
# (high_score_image["code"], all_frames, draw_config["font_config"]) | # (high_score_image["code"], all_frames, draw_config["font_config"]) | ||||
# high_score_image["code"][code][cls] = (frame, frame_index_list[i], cls_list) | # high_score_image["code"][code][cls] = (frame, frame_index_list[i], cls_list) | ||||
det_result, all_frames, font_config = frame_msg | |||||
det_xywh, frame, current_frame, all_frames, font_config = frame_msg | |||||
''' | ''' | ||||
det_xywh:{ | det_xywh:{ | ||||
'code':{ | 'code':{ | ||||
''' | ''' | ||||
model_info = [] | model_info = [] | ||||
# 更加模型编码解析数据 | # 更加模型编码解析数据 | ||||
for code, det_list in det_result.items(): | |||||
for code, det_list in det_xywh.items(): | |||||
if len(det_list) > 0: | if len(det_list) > 0: | ||||
for cls, target_list in det_list.items(): | for cls, target_list in det_list.items(): | ||||
frame, iframe, cls_list = target_list | |||||
if len(cls_list) > 0: | |||||
if len(target_list) > 0: | |||||
aFrame = frame.copy() | aFrame = frame.copy() | ||||
for target in cls_list: | |||||
# detect_targets_code, box, score, label_array, color | |||||
for target in target_list: | |||||
draw_painting_joint(target[1], aFrame, target[3], target[2], target[4], font_config) | draw_painting_joint(target[1], aFrame, target[3], target[2], target[4], font_config) | ||||
model_info.append({ | |||||
"or_frame": frame, | |||||
"modelCode": str(code), | |||||
"detectTargetCode": str(cls), | |||||
"aFrame": aFrame, | |||||
"current_frame": iframe, | |||||
"last_frame": iframe + frame_step | |||||
}) | |||||
model_info.append({"modelCode": str(code), "detectTargetCode": str(cls), "aFrame": aFrame}) | |||||
if len(model_info) > 0: | if len(model_info) > 0: | ||||
return model_info | |||||
image_result = { | |||||
"or_frame": frame, | |||||
"model_info": model_info, | |||||
"current_frame": current_frame, | |||||
"last_frame": current_frame + frame_step | |||||
} | |||||
return image_result | |||||
return None | return None | ||||
# @staticmethod | |||||
# def handle_image(frame_msg, frame_step): | |||||
# # (high_score_image["code"], all_frames, draw_config["font_config"]) | |||||
# # high_score_image["code"][code][cls] = (frame, frame_index_list[i], cls_list) | |||||
# det_xywh, frame, current_frame, all_frames, font_config = frame_msg | |||||
# ''' | |||||
# det_xywh:{ | |||||
# 'code':{ | |||||
# 1: [[detect_targets_code, box, score, label_array, color]] | |||||
# } | |||||
# } | |||||
# 模型编号:modeCode | |||||
# 检测目标:detectTargetCode | |||||
# ''' | |||||
# model_info = [] | |||||
# # 更加模型编码解析数据 | |||||
# for code, det_list in det_xywh.items(): | |||||
# if len(det_list) > 0: | |||||
# for cls, target_list in det_list.items(): | |||||
# if len(target_list) > 0: | |||||
# aFrame = frame.copy() | |||||
# for target in target_list: | |||||
# # detect_targets_code, box, score, label_array, color | |||||
# draw_painting_joint(target[1], aFrame, target[3], target[2], target[4], font_config) | |||||
# model_info.append({"modelCode": str(code), "detectTargetCode": str(cls), "aFrame": aFrame}) | |||||
# if len(model_info) > 0: | |||||
# image_result = { | |||||
# "or_frame": frame, | |||||
# "model_info": model_info, | |||||
# "current_frame": current_frame, | |||||
# "last_frame": current_frame + frame_step | |||||
# } | |||||
# return image_result | |||||
# return None | |||||
def run(self): | def run(self): | ||||
msg, context = self._msg, self._context | msg, context = self._msg, self._context | ||||
service = context["service"] | service = context["service"] | ||||
if image_msg[0] == 1: | if image_msg[0] == 1: | ||||
image_result = self.handle_image(image_msg[1], frame_step) | image_result = self.handle_image(image_msg[1], frame_step) | ||||
if image_result is not None: | if image_result is not None: | ||||
task, msg_list = [], [] | |||||
for model_info in image_result: | |||||
or_image = cv2.imencode(".jpg", model_info["or_frame"])[1] | |||||
or_image_name = build_image_name(model_info["current_frame"], | |||||
model_info["last_frame"], | |||||
analyse_type, | |||||
"OR", "0", "0", request_id) | |||||
or_future = t.submit(aliyunOssSdk.put_object, or_image_name, or_image.tobytes()) | |||||
task.append(or_future) | |||||
task = [] | |||||
or_image = cv2.imencode(".jpg", image_result["or_frame"])[1] | |||||
or_image_name = build_image_name(image_result["current_frame"], | |||||
image_result["last_frame"], | |||||
analyse_type, | |||||
"OR", "0", "0", request_id) | |||||
or_future = t.submit(aliyunOssSdk.put_object, or_image_name, or_image.tobytes()) | |||||
task.append(or_future) | |||||
model_info_list = image_result["model_info"] | |||||
msg_list = [] | |||||
for model_info in model_info_list: | |||||
ai_image = cv2.imencode(".jpg", model_info["aFrame"])[1] | ai_image = cv2.imencode(".jpg", model_info["aFrame"])[1] | ||||
ai_image_name = build_image_name(model_info["current_frame"], | |||||
model_info["last_frame"], | |||||
ai_image_name = build_image_name(image_result["current_frame"], | |||||
image_result["last_frame"], | |||||
analyse_type, | analyse_type, | ||||
"AI", | "AI", | ||||
model_info["modelCode"], | model_info["modelCode"], | ||||
for msg in msg_list: | for msg in msg_list: | ||||
put_queue(fb_queue, msg, timeout=2, is_ex=False) | put_queue(fb_queue, msg, timeout=2, is_ex=False) | ||||
del task, msg_list | del task, msg_list | ||||
# task = [] | |||||
# or_image = cv2.imencode(".jpg", image_result["or_frame"])[1] | |||||
# or_image_name = build_image_name(image_result["current_frame"], | |||||
# image_result["last_frame"], | |||||
# analyse_type, | |||||
# "OR", "0", "0", request_id) | |||||
# or_future = t.submit(aliyunOssSdk.put_object, or_image_name, or_image.tobytes()) | |||||
# task.append(or_future) | |||||
# model_info_list = image_result["model_info"] | |||||
# msg_list = [] | |||||
# for model_info in model_info_list: | |||||
# ai_image = cv2.imencode(".jpg", model_info["aFrame"])[1] | |||||
# ai_image_name = build_image_name(image_result["current_frame"], | |||||
# image_result["last_frame"], | |||||
# analyse_type, | |||||
# "AI", | |||||
# model_info["modelCode"], | |||||
# model_info["detectTargetCode"], | |||||
# request_id) | |||||
# ai_future = t.submit(aliyunOssSdk.put_object, ai_image_name, | |||||
# ai_image.tobytes()) | |||||
# task.append(ai_future) | |||||
# msg_list.append(message_feedback(request_id, | |||||
# AnalysisStatus.RUNNING.value, | |||||
# analyse_type, "", "", "", | |||||
# or_image_name, | |||||
# ai_image_name, | |||||
# model_info['modelCode'], | |||||
# model_info['detectTargetCode'])) | |||||
# for tk in task: | |||||
# tk.result() | |||||
# for msg in msg_list: | |||||
# put_queue(fb_queue, msg, timeout=2, is_ex=False) | |||||
# del task, msg_list | |||||
else: | else: | ||||
sleep(1) | sleep(1) | ||||
del image_msg | del image_msg |
ExceptionType.NO_RESOURCES.value[0], | ExceptionType.NO_RESOURCES.value[0], | ||||
ExceptionType.NO_RESOURCES.value[1]), timeout=2) | ExceptionType.NO_RESOURCES.value[1]), timeout=2) | ||||
break | break | ||||
del frame | |||||
continue | continue | ||||
full_timeout = None | full_timeout = None | ||||
if frame is None: | if frame is None: |
ExceptionType.NO_RESOURCES.value[0], | ExceptionType.NO_RESOURCES.value[0], | ||||
ExceptionType.NO_RESOURCES.value[1])) | ExceptionType.NO_RESOURCES.value[1])) | ||||
break | break | ||||
del frame | |||||
continue | continue | ||||
full_timeout = None | full_timeout = None | ||||
frame_list.append(frame) | frame_list.append(frame) |
ai_video_file, ai_write_status, request_id) | ai_video_file, ai_write_status, request_id) | ||||
push_stream_result = t.submit(push_video_stream, frame_merge, push_p, push_url, | push_stream_result = t.submit(push_video_stream, frame_merge, push_p, push_url, | ||||
p_push_status, request_id) | p_push_status, request_id) | ||||
# 指定多少帧上传一次问题 | |||||
if high_score_image.get("current_frame") is None: | |||||
high_score_image["current_frame"] = frame_index_list[i] | |||||
diff_frame_num = frame_index_list[i] - high_score_image["current_frame"] | |||||
# 指定帧内处理逻辑 | |||||
if frame_step >= diff_frame_num and high_score_image.get("code") is not None and len(det_xywh) > 0: | |||||
# 所有的模型编号 | |||||
cache_codes = set(high_score_image["code"].keys()) | |||||
# 遍历当前模型 | |||||
for det_code, det_clss in det_xywh.items(): | |||||
# 如果模型编号不在缓存中 | |||||
if det_code not in cache_codes: | |||||
high_score_image["code"][det_code] = {} | |||||
for cls, cls_list in det_xywh[det_code].items(): | |||||
if len(cls_list) > 0: | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
else: | |||||
# 如果模型编号在缓存中, 检查检测目标的情况 | |||||
cache_clss = set(high_score_image["code"][det_code].keys()) | |||||
for cls, cls_list in det_xywh[det_code].items(): | |||||
if len(cls_list) > 0: | |||||
if cls not in cache_clss: | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
else: | |||||
if len(det_xywh[det_code][cls]) > len(high_score_image["code"][det_code][cls][2]): | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
elif len(det_xywh[det_code][cls]) == len(high_score_image["code"][det_code][cls][2]): | |||||
# [cls, box, score, label_array, color] | |||||
hs = 0 | |||||
for ii, s in enumerate(cls_list): | |||||
if s[2] >= high_score_image["code"][det_code][cls][2][ii][2]: | |||||
hs += 1 | |||||
if hs == len(cls_list): | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
if diff_frame_num > frame_step: | |||||
if high_score_image.get("code") is not None: | |||||
high_score_image["or_frame"] = frame | |||||
put_queue(image_queue, (1, [high_score_image["code"], all_frames, draw_config["font_config"]])) | |||||
high_score_image["code"] = None | |||||
high_score_image["current_frame"] = None | |||||
# 如果有问题, 走下面的逻辑 | # 如果有问题, 走下面的逻辑 | ||||
if len(det_xywh) > 0: | if len(det_xywh) > 0: | ||||
flag = True | flag = True | ||||
# 比较上一次识别到问题的帧和当前问题帧的相似度 | |||||
if picture_similarity and high_score_image.get("or_frame") is not None: | |||||
hash1 = ImageUtils.dHash(high_score_image["or_frame"]) | |||||
if picture_similarity and len(high_score_image) > 0: | |||||
hash1 = ImageUtils.dHash(high_score_image.get("or_frame")) | |||||
hash2 = ImageUtils.dHash(frame) | hash2 = ImageUtils.dHash(frame) | ||||
dist = ImageUtils.Hamming_distance(hash1, hash2) | dist = ImageUtils.Hamming_distance(hash1, hash2) | ||||
similarity_1 = 1 - dist * 1.0 / 64 | similarity_1 = 1 - dist * 1.0 / 64 | ||||
if similarity_1 >= similarity: | if similarity_1 >= similarity: | ||||
flag = False | flag = False | ||||
# 如果缓存问题是空的,给缓存添加问题 | |||||
if flag and high_score_image.get("code") is None: | |||||
high_score_image["code"] = {} | |||||
for code, det_list in det_xywh.items(): | |||||
if high_score_image["code"].get(code) is None: | |||||
high_score_image["code"][code] = {} | |||||
for cls, cls_list in det_list.items(): | |||||
high_score_image["code"][code][cls] = (frame, frame_index_list[i], cls_list) | |||||
push_p = push_stream_result.result(timeout=5) | |||||
ai_video_file = write_ai_video_result.result(timeout=5) | |||||
or_video_file = write_or_video_result.result(timeout=5) | |||||
if len(high_score_image) > 0: | |||||
diff_frame_num = frame_index_list[i] - high_score_image.get("current_frame") | |||||
if diff_frame_num < frame_step: | |||||
flag = False | |||||
if flag: | |||||
high_score_image["or_frame"] = frame | |||||
high_score_image["current_frame"] = frame_index_list[i] | |||||
put_queue(image_queue, (1, [det_xywh, frame, frame_index_list[i], all_frames, draw_config["font_config"]])) | |||||
push_p = push_stream_result.result(timeout=60) | |||||
ai_video_file = write_ai_video_result.result(timeout=60) | |||||
or_video_file = write_or_video_result.result(timeout=60) | |||||
# 接收停止指令 | # 接收停止指令 | ||||
if push_r[0] == 2: | if push_r[0] == 2: | ||||
if 'stop' == push_r[1]: | if 'stop' == push_r[1]: | ||||
if image_queue.qsize() == 0 or image_queue.empty(): | if image_queue.qsize() == 0 or image_queue.empty(): | ||||
break | break | ||||
sleep(2) | sleep(2) | ||||
for q in [push_queue, image_queue, push_ex_queue, hb_queue]: | |||||
for q in [push_queue, image_queue, hb_queue]: | |||||
clear_queue(q) | clear_queue(q) | ||||
logger.info("推流进程停止完成!图片队列大小: {}, requestId:{}", image_queue.qsize(), request_id) | logger.info("推流进程停止完成!图片队列大小: {}, requestId:{}", image_queue.qsize(), request_id) | ||||
ai_write_status, request_id) | ai_write_status, request_id) | ||||
push_stream_result = t.submit(push_video_stream, frame_merge, push_p, push_url, | push_stream_result = t.submit(push_video_stream, frame_merge, push_p, push_url, | ||||
p_push_status, request_id) | p_push_status, request_id) | ||||
# 指定多少帧上传一次问题 | |||||
if high_score_image.get("current_frame") is None: | |||||
high_score_image["current_frame"] = frame_index_list[i] | |||||
diff_frame_num = frame_index_list[i] - high_score_image["current_frame"] | |||||
# 指定帧内处理逻辑 | |||||
if frame_step >= diff_frame_num and high_score_image.get("code") is not None and len(det_xywh) > 0: | |||||
# 所有的模型编号 | |||||
cache_codes = set(high_score_image["code"].keys()) | |||||
# 遍历当前模型 | |||||
for det_code, det_clss in det_xywh.items(): | |||||
# 如果模型编号不在缓存中 | |||||
if det_code not in cache_codes: | |||||
high_score_image["code"][det_code] = {} | |||||
for cls, cls_list in det_xywh[det_code].items(): | |||||
if len(cls_list) > 0: | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
else: | |||||
# 如果模型编号在缓存中, 检查检测目标的情况 | |||||
cache_clss = set(high_score_image["code"][det_code].keys()) | |||||
for cls, cls_list in det_xywh[det_code].items(): | |||||
if len(cls_list) > 0: | |||||
# 如果检测目标在缓存中 | |||||
if cls not in cache_clss: | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
else: | |||||
if len(det_xywh[det_code][cls]) > len(high_score_image["code"][det_code][cls][2]): | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
elif len(det_xywh[det_code][cls]) == len(high_score_image["code"][det_code][cls][2]): | |||||
# [cls, box, score, label_array, color] | |||||
hs = 0 | |||||
for ii, s in enumerate(cls_list): | |||||
if s[2] >= high_score_image["code"][det_code][cls][2][ii][2]: | |||||
hs += 1 | |||||
if hs == len(cls_list): | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
if diff_frame_num > frame_step: | |||||
if high_score_image.get("code") is not None: | |||||
high_score_image["or_frame"] = frame | |||||
put_queue(image_queue, (1, [high_score_image["code"], all_frames, draw_config["font_config"]])) | |||||
high_score_image["code"] = None | |||||
high_score_image["current_frame"] = None | |||||
# 如果有问题, 走下面的逻辑 | |||||
if len(det_xywh) > 0: | if len(det_xywh) > 0: | ||||
flag = True | flag = True | ||||
# 比较上一次识别到问题的帧和当前问题帧的相似度 | |||||
if picture_similarity and high_score_image.get("or_frame") is not None: | |||||
hash1 = ImageUtils.dHash(high_score_image["or_frame"]) | |||||
if picture_similarity and len(high_score_image) > 0: | |||||
hash1 = ImageUtils.dHash(high_score_image.get("or_frame")) | |||||
hash2 = ImageUtils.dHash(frame) | hash2 = ImageUtils.dHash(frame) | ||||
dist = ImageUtils.Hamming_distance(hash1, hash2) | dist = ImageUtils.Hamming_distance(hash1, hash2) | ||||
similarity_1 = 1 - dist * 1.0 / 64 | similarity_1 = 1 - dist * 1.0 / 64 | ||||
if similarity_1 >= similarity: | if similarity_1 >= similarity: | ||||
flag = False | flag = False | ||||
# 如果缓存问题是空的,给缓存添加问题 | |||||
if flag and high_score_image.get("code") is None: | |||||
high_score_image["code"] = {} | |||||
for code, det_list in det_xywh.items(): | |||||
if high_score_image["code"].get(code) is None: | |||||
high_score_image["code"][code] = {} | |||||
for cls, cls_list in det_list.items(): | |||||
high_score_image["code"][code][cls] = (frame, frame_index_list[i], cls_list) | |||||
push_p = push_stream_result.result(timeout=5) | |||||
ai_video_file = write_ai_video_result.result(timeout=5) | |||||
if len(high_score_image) > 0: | |||||
diff_frame_num = frame_index_list[i] - high_score_image.get("current_frame") | |||||
if diff_frame_num < frame_step: | |||||
flag = False | |||||
if flag: | |||||
high_score_image["or_frame"] = frame | |||||
high_score_image["current_frame"] = frame_index_list[i] | |||||
put_queue(image_queue, (1, [det_xywh, frame, frame_index_list[i], all_frames, draw_config["font_config"]])) | |||||
push_p = push_stream_result.result(timeout=60) | |||||
ai_video_file = write_ai_video_result.result(timeout=60) | |||||
# 接收停止指令 | # 接收停止指令 | ||||
if push_r[0] == 2: | if push_r[0] == 2: | ||||
if 'stop' == push_r[1]: | if 'stop' == push_r[1]: | ||||
if image_queue.qsize() == 0 or image_queue.empty(): | if image_queue.qsize() == 0 or image_queue.empty(): | ||||
break | break | ||||
sleep(2) | sleep(2) | ||||
for q in [push_queue, image_queue, push_ex_queue, hb_queue]: | |||||
for q in [push_queue, image_queue, hb_queue]: | |||||
clear_queue(q) | clear_queue(q) | ||||
logger.info("推流进程停止完成!requestId:{}", request_id) | logger.info("推流进程停止完成!requestId:{}", request_id) |
push_p_result = t.submit(push_video_stream, frame_merge, push_p, push_url, | push_p_result = t.submit(push_video_stream, frame_merge, push_p, push_url, | ||||
p_push_status, | p_push_status, | ||||
request_id) | request_id) | ||||
# 指定多少帧上传一次问题 | |||||
if high_score_image.get("current_frame") is None: | |||||
high_score_image["current_frame"] = frame_index_list[i] | |||||
diff_frame_num = frame_index_list[i] - high_score_image["current_frame"] | |||||
# 指定帧内处理逻辑 | |||||
if frame_step >= diff_frame_num and high_score_image.get("code") is not None and len(det_xywh) > 0: | |||||
# 所有的模型编号 | |||||
cache_codes = set(high_score_image["code"].keys()) | |||||
# 遍历当前模型 | |||||
for det_code, det_clss in det_xywh.items(): | |||||
# 如果模型编号不在缓存中 | |||||
if det_code not in cache_codes: | |||||
high_score_image["code"][det_code] = {} | |||||
for cls, cls_list in det_xywh[det_code].items(): | |||||
if len(cls_list) > 0: | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
else: | |||||
# 如果模型编号在缓存中, 检查检测目标的情况 | |||||
cache_clss = set(high_score_image["code"][det_code].keys()) | |||||
for cls, cls_list in det_xywh[det_code].items(): | |||||
if len(cls_list) > 0: | |||||
# 如果检测目标在缓存中 | |||||
if cls not in cache_clss: | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
else: | |||||
if len(det_xywh[det_code][cls]) > len(high_score_image["code"][det_code][cls][2]): | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
elif len(det_xywh[det_code][cls]) == len(high_score_image["code"][det_code][cls][2]): | |||||
# [cls, box, score, label_array, color] | |||||
hs = 0 | |||||
for ii, s in enumerate(cls_list): | |||||
if s[2] >= high_score_image["code"][det_code][cls][2][ii][2]: | |||||
hs += 1 | |||||
if hs == len(cls_list): | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
if diff_frame_num > frame_step: | |||||
if high_score_image.get("code") is not None: | |||||
high_score_image["or_frame"] = frame | |||||
put_queue(image_queue, (1, [high_score_image["code"], all_frames, draw_config["font_config"]])) | |||||
high_score_image["code"] = None | |||||
high_score_image["current_frame"] = None | |||||
# 如果有问题, 走下面的逻辑 | |||||
if len(det_xywh) > 0: | if len(det_xywh) > 0: | ||||
flag = True | flag = True | ||||
# 比较上一次识别到问题的帧和当前问题帧的相似度 | |||||
if picture_similarity and high_score_image.get("or_frame") is not None: | |||||
hash1 = ImageUtils.dHash(high_score_image["or_frame"]) | |||||
if picture_similarity and len(high_score_image) > 0: | |||||
hash1 = ImageUtils.dHash(high_score_image.get("or_frame")) | |||||
hash2 = ImageUtils.dHash(frame) | hash2 = ImageUtils.dHash(frame) | ||||
dist = ImageUtils.Hamming_distance(hash1, hash2) | dist = ImageUtils.Hamming_distance(hash1, hash2) | ||||
similarity_1 = 1 - dist * 1.0 / 64 | similarity_1 = 1 - dist * 1.0 / 64 | ||||
if similarity_1 >= similarity: | if similarity_1 >= similarity: | ||||
flag = False | flag = False | ||||
# 如果缓存问题是空的,给缓存添加问题 | |||||
if flag and high_score_image.get("code") is None: | |||||
high_score_image["code"] = {} | |||||
for code, det_list in det_xywh.items(): | |||||
if high_score_image["code"].get(code) is None: | |||||
high_score_image["code"][code] = {} | |||||
for cls, cls_list in det_list.items(): | |||||
high_score_image["code"][code][cls] = (frame, frame_index_list[i], cls_list) | |||||
push_p = push_p_result.result(timeout=5) | |||||
ai_video_file = write_ai_video_result.result(timeout=5) | |||||
or_video_file = write_or_video_result.result(timeout=5) | |||||
if len(high_score_image) > 0: | |||||
diff_frame_num = frame_index_list[i] - high_score_image.get("current_frame") | |||||
if diff_frame_num < frame_step: | |||||
flag = False | |||||
if flag: | |||||
high_score_image["or_frame"] = frame | |||||
high_score_image["current_frame"] = frame_index_list[i] | |||||
put_queue(image_queue, (1, [det_xywh, frame, frame_index_list[i], all_frames, draw_config["font_config"]])) | |||||
push_p = push_p_result.result(timeout=60) | |||||
ai_video_file = write_ai_video_result.result(timeout=60) | |||||
or_video_file = write_or_video_result.result(timeout=60) | |||||
# 接收停止指令 | # 接收停止指令 | ||||
if push_r[0] == 2: | if push_r[0] == 2: | ||||
if 'stop' == push_r[1]: | if 'stop' == push_r[1]: | ||||
if image_queue.qsize() == 0 or image_queue.empty(): | if image_queue.qsize() == 0 or image_queue.empty(): | ||||
break | break | ||||
sleep(2) | sleep(2) | ||||
for q in [push_queue, image_queue, push_ex_queue, hb_queue]: | |||||
for q in [push_queue, image_queue, hb_queue]: | |||||
clear_queue(q) | clear_queue(q) | ||||
logger.info("推流进程停止完成!requestId:{}", request_id) | logger.info("推流进程停止完成!requestId:{}", request_id) | ||||
push_p_result = t.submit(push_video_stream, frame_merge, push_p, push_url, | push_p_result = t.submit(push_video_stream, frame_merge, push_p, push_url, | ||||
p_push_status, | p_push_status, | ||||
request_id) | request_id) | ||||
# 指定多少帧上传一次问题 | |||||
if high_score_image.get("current_frame") is None: | |||||
high_score_image["current_frame"] = frame_index_list[i] | |||||
diff_frame_num = frame_index_list[i] - high_score_image["current_frame"] | |||||
# 指定帧内处理逻辑 | |||||
if frame_step >= diff_frame_num and high_score_image.get("code") is not None and len(det_xywh) > 0: | |||||
# 所有的模型编号 | |||||
cache_codes = set(high_score_image["code"].keys()) | |||||
# 遍历当前模型 | |||||
for det_code, det_clss in det_xywh.items(): | |||||
# 如果模型编号不在缓存中 | |||||
if det_code not in cache_codes: | |||||
high_score_image["code"][det_code] = {} | |||||
for cls, cls_list in det_xywh[det_code].items(): | |||||
if len(cls_list) > 0: | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
else: | |||||
# 如果模型编号在缓存中, 检查检测目标的情况 | |||||
cache_clss = set(high_score_image["code"][det_code].keys()) | |||||
for cls, cls_list in det_xywh[det_code].items(): | |||||
if len(cls_list) > 0: | |||||
# 如果检测目标在缓存中 | |||||
if cls not in cache_clss: | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
else: | |||||
if len(det_xywh[det_code][cls]) > len(high_score_image["code"][det_code][cls][2]): | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
elif len(det_xywh[det_code][cls]) == len(high_score_image["code"][det_code][cls][2]): | |||||
# [cls, box, score, label_array, color] | |||||
hs = 0 | |||||
for ii, s in enumerate(cls_list): | |||||
if s[2] >= high_score_image["code"][det_code][cls][2][ii][2]: | |||||
hs += 1 | |||||
if hs == len(cls_list): | |||||
high_score_image["code"][det_code][cls] = (frame, frame_index_list[i], cls_list) | |||||
if diff_frame_num > frame_step: | |||||
if high_score_image.get("code") is not None: | |||||
high_score_image["or_frame"] = frame | |||||
put_queue(image_queue, (1, [high_score_image["code"], all_frames, draw_config["font_config"]])) | |||||
high_score_image["code"] = None | |||||
high_score_image["current_frame"] = None | |||||
# 如果有问题, 走下面的逻辑 | |||||
if len(det_xywh) > 0: | if len(det_xywh) > 0: | ||||
flag = True | flag = True | ||||
# 比较上一次识别到问题的帧和当前问题帧的相似度 | |||||
if picture_similarity and high_score_image.get("or_frame") is not None: | |||||
hash1 = ImageUtils.dHash(high_score_image["or_frame"]) | |||||
if picture_similarity and len(high_score_image) > 0: | |||||
hash1 = ImageUtils.dHash(high_score_image.get("or_frame")) | |||||
hash2 = ImageUtils.dHash(frame) | hash2 = ImageUtils.dHash(frame) | ||||
dist = ImageUtils.Hamming_distance(hash1, hash2) | dist = ImageUtils.Hamming_distance(hash1, hash2) | ||||
similarity_1 = 1 - dist * 1.0 / 64 | similarity_1 = 1 - dist * 1.0 / 64 | ||||
if similarity_1 >= similarity: | if similarity_1 >= similarity: | ||||
flag = False | flag = False | ||||
# 如果缓存问题是空的,给缓存添加问题 | |||||
if flag and high_score_image.get("code") is None: | |||||
high_score_image["code"] = {} | |||||
for code, det_list in det_xywh.items(): | |||||
if high_score_image["code"].get(code) is None: | |||||
high_score_image["code"][code] = {} | |||||
for cls, cls_list in det_list.items(): | |||||
high_score_image["code"][code][cls] = (frame, frame_index_list[i], cls_list) | |||||
push_p = push_p_result.result(timeout=5) | |||||
ai_video_file = write_ai_video_result.result(timeout=5) | |||||
if len(high_score_image) > 0: | |||||
diff_frame_num = frame_index_list[i] - high_score_image.get("current_frame") | |||||
if diff_frame_num < frame_step: | |||||
flag = False | |||||
if flag: | |||||
high_score_image["or_frame"] = frame | |||||
high_score_image["current_frame"] = frame_index_list[i] | |||||
put_queue(image_queue, (1, [det_xywh, frame, frame_index_list[i], all_frames, draw_config["font_config"]])) | |||||
push_p = push_p_result.result(timeout=60) | |||||
ai_video_file = write_ai_video_result.result(timeout=60) | |||||
# 接收停止指令 | # 接收停止指令 | ||||
if push_r[0] == 2: | if push_r[0] == 2: | ||||
if 'stop' == push_r[1]: | if 'stop' == push_r[1]: | ||||
if image_queue.qsize() == 0 or image_queue.empty(): | if image_queue.qsize() == 0 or image_queue.empty(): | ||||
break | break | ||||
sleep(2) | sleep(2) | ||||
for q in [push_queue, image_queue, push_ex_queue, hb_queue]: | |||||
for q in [push_queue, image_queue, hb_queue]: | |||||
clear_queue(q) | clear_queue(q) | ||||
logger.info("推流进程停止完成!requestId:{}", request_id) | logger.info("推流进程停止完成!requestId:{}", request_id) |
# 图片相似度过滤 | # 图片相似度过滤 | ||||
picture_similarity: true | picture_similarity: true | ||||
similarity: 0.65 | similarity: 0.65 | ||||
frame_step: 300 | |||||
frame_step: 160 | |||||
timeout: 21600 | timeout: 21600 | ||||
cv2_pull_stream_timeout: 1000 | cv2_pull_stream_timeout: 1000 | ||||
cv2_read_stream_timeout: 1000 | cv2_read_stream_timeout: 1000 |
# 图片相似度过滤 | # 图片相似度过滤 | ||||
picture_similarity: true | picture_similarity: true | ||||
similarity: 0.65 | similarity: 0.65 | ||||
frame_step: 300 | |||||
frame_step: 160 | |||||
timeout: 21600 | timeout: 21600 | ||||
cv2_pull_stream_timeout: 1000 | cv2_pull_stream_timeout: 1000 | ||||
cv2_read_stream_timeout: 1000 | cv2_read_stream_timeout: 1000 |
# 图片相似度过滤 | # 图片相似度过滤 | ||||
picture_similarity: true | picture_similarity: true | ||||
similarity: 0.65 | similarity: 0.65 | ||||
frame_step: 300 | |||||
frame_step: 160 | |||||
timeout: 21600 | timeout: 21600 | ||||
cv2_pull_stream_timeout: 1000 | cv2_pull_stream_timeout: 1000 | ||||
cv2_read_stream_timeout: 1000 | cv2_read_stream_timeout: 1000 |
:param requestId: 请求id | :param requestId: 请求id | ||||
:return: 推流管道 | :return: 推流管道 | ||||
""" | """ | ||||
st = time() | |||||
try: | try: | ||||
if push_p is None: | if push_p is None: | ||||
height, width = frame.shape[0:2] | height, width = frame.shape[0:2] | ||||
clear_push_p(push_p, requestId) | clear_push_p(push_p, requestId) | ||||
raise s | raise s | ||||
except Exception: | except Exception: | ||||
et = time() - st | |||||
logger.error("推流异常使用时间:{}, requestId: {}", et, requestId) | |||||
if et > 20: | |||||
logger.error("推流进管道异常:{}, requestId: {}", format_exc(), requestId) | |||||
raise ServiceException(ExceptionType.PUSH_STREAMING_CHANNEL_IS_OCCUPIED.value[0], | |||||
ExceptionType.PUSH_STREAMING_CHANNEL_IS_OCCUPIED.value[1]) | |||||
if p_push_status[0] == 0: | if p_push_status[0] == 0: | ||||
p_push_status[0] = time() | p_push_status[0] = time() | ||||
p_push_status[1] += 1 | p_push_status[1] += 1 | ||||
p_push_status[0] = time() | p_push_status[0] = time() | ||||
logger.error("推流管道异常:{}, requestId: {}", format_exc(), requestId) | logger.error("推流管道异常:{}, requestId: {}", format_exc(), requestId) | ||||
clear_push_p(push_p, requestId) | clear_push_p(push_p, requestId) | ||||
if p_push_status[1] > 150: | |||||
if p_push_status[1] > 5: | |||||
logger.error("推流进管道异常:{}, requestId: {}", format_exc(), requestId) | logger.error("推流进管道异常:{}, requestId: {}", format_exc(), requestId) | ||||
raise ServiceException(ExceptionType.PUSH_STREAMING_CHANNEL_IS_OCCUPIED.value[0], | raise ServiceException(ExceptionType.PUSH_STREAMING_CHANNEL_IS_OCCUPIED.value[0], | ||||
ExceptionType.PUSH_STREAMING_CHANNEL_IS_OCCUPIED.value[1]) | ExceptionType.PUSH_STREAMING_CHANNEL_IS_OCCUPIED.value[1]) |