|
|
|
@ -20,6 +20,8 @@ import json
|
|
|
|
|
|
|
|
|
|
def data_load(args):
|
|
|
|
|
|
|
|
|
|
# print('正在运行的进程',msg)
|
|
|
|
|
# print(args)
|
|
|
|
|
source = args[0]
|
|
|
|
|
model_ymal = args[1]
|
|
|
|
|
|
|
|
|
@ -36,19 +38,6 @@ def data_load(args):
|
|
|
|
|
|
|
|
|
|
if rtsp_source:
|
|
|
|
|
|
|
|
|
|
rtsp_detect_process(source=source, model_data=model_data,
|
|
|
|
|
model_inference=model_inference)
|
|
|
|
|
|
|
|
|
|
if dir_source:
|
|
|
|
|
dir_source_process(source, model_inference, model_data)
|
|
|
|
|
|
|
|
|
|
if file_source:
|
|
|
|
|
|
|
|
|
|
file_source_process(source, model_inference, model_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rtsp_detect_process(source, model_data, model_inference):
|
|
|
|
|
|
|
|
|
|
cap = cv2.VideoCapture(source)
|
|
|
|
|
|
|
|
|
|
# 视频流信息
|
|
|
|
@ -72,8 +61,6 @@ def rtsp_detect_process(source, model_data, model_inference):
|
|
|
|
|
while True:
|
|
|
|
|
ret, frame = cap.read()
|
|
|
|
|
|
|
|
|
|
t1 = time.time()
|
|
|
|
|
|
|
|
|
|
if not ret:
|
|
|
|
|
continue # 如果未成功读取到视频帧,则继续读取下一帧
|
|
|
|
|
|
|
|
|
@ -103,25 +90,28 @@ def rtsp_detect_process(source, model_data, model_inference):
|
|
|
|
|
out_video = cv2.VideoWriter(
|
|
|
|
|
video_path, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
|
|
|
|
|
|
|
|
|
|
print(source, data_now, i, j,video_path)
|
|
|
|
|
|
|
|
|
|
print(source, data_now, i,j)
|
|
|
|
|
t1 = time.time()
|
|
|
|
|
|
|
|
|
|
# 推理部分
|
|
|
|
|
imgframe_dict = {"path": source, 'frame': frame, 'get_fps': j}
|
|
|
|
|
imgframe_dict = {"path": source,
|
|
|
|
|
'frame': frame,
|
|
|
|
|
'get_fps': j}
|
|
|
|
|
|
|
|
|
|
images_det_result = img_process(
|
|
|
|
|
imgframe_dict, model_inference, model_data)
|
|
|
|
|
images_det_result = img_process(imgframe_dict,
|
|
|
|
|
model_inference,
|
|
|
|
|
model_data)
|
|
|
|
|
|
|
|
|
|
images_update = save_process(
|
|
|
|
|
imgframe_dict, images_det_result, model_data)
|
|
|
|
|
images_update = save_process(imgframe_dict,
|
|
|
|
|
images_det_result,
|
|
|
|
|
model_data)
|
|
|
|
|
|
|
|
|
|
# print('images_det_result:', len(images_det_result))
|
|
|
|
|
print('images_det_result:',len(images_det_result))
|
|
|
|
|
|
|
|
|
|
# 结果判断,t
|
|
|
|
|
if images_det_result:
|
|
|
|
|
|
|
|
|
|
det_t_num = det_t_num + 1
|
|
|
|
|
|
|
|
|
|
# print(len(det_img))
|
|
|
|
|
if len(det_img) == 0:
|
|
|
|
|
img_dict = images_update.copy()
|
|
|
|
|
del img_dict['frame']
|
|
|
|
@ -133,69 +123,55 @@ def rtsp_detect_process(source, model_data, model_inference):
|
|
|
|
|
|
|
|
|
|
if (det_t_num + nodet_t_num) >= fps_num_small:
|
|
|
|
|
|
|
|
|
|
para = determine_time(
|
|
|
|
|
det_num=det_t_num, nodet_num=nodet_t_num, ratio_set=model_data['detect_ratio'])
|
|
|
|
|
para = determine_time(det_num=det_t_num,
|
|
|
|
|
nodet_num=nodet_t_num,
|
|
|
|
|
ratio_set=model_data['detect_ratio'])
|
|
|
|
|
|
|
|
|
|
if para:
|
|
|
|
|
|
|
|
|
|
first_fps_time = det_img[0]
|
|
|
|
|
first_fps_time.update(
|
|
|
|
|
{"dert_fps": (j-int(first_fps_time['get_fps'])+1)})
|
|
|
|
|
print({"dert_fps": (j-int(first_fps_time['get_fps'])+1)})
|
|
|
|
|
first_fps_time.update({"dert_fps": (j-int(first_fps_time['get_fps'])+1)})
|
|
|
|
|
det_fps_time.append(first_fps_time)
|
|
|
|
|
|
|
|
|
|
det_img.clear()
|
|
|
|
|
det_t_num = 0
|
|
|
|
|
nodet_t_num = 0
|
|
|
|
|
|
|
|
|
|
# 视频保存
|
|
|
|
|
# print('det_img:', len(det_img), det_t_num, nodet_t_num)
|
|
|
|
|
|
|
|
|
|
out_video.write(images_update['frame'])
|
|
|
|
|
|
|
|
|
|
# 结果判断 ,T
|
|
|
|
|
if j >= fps_num:
|
|
|
|
|
|
|
|
|
|
out_video.release()
|
|
|
|
|
|
|
|
|
|
# T时间截至,判断t时间结果。
|
|
|
|
|
if det_img:
|
|
|
|
|
para = determine_time(
|
|
|
|
|
det_num=det_t_num, nodet_num=nodet_t_num, ratio_set=model_data['detect_ratio'])
|
|
|
|
|
|
|
|
|
|
first_fps_time = det_img[0]
|
|
|
|
|
|
|
|
|
|
# print(j-int(first_fps_time['get_fps'])+1)
|
|
|
|
|
# print(fps_num_small/2)
|
|
|
|
|
|
|
|
|
|
if (j-int(first_fps_time['get_fps'])+1) >= (fps_num_small/2):
|
|
|
|
|
|
|
|
|
|
first_fps_time.update(
|
|
|
|
|
{"dert_fps": (j-int(first_fps_time['get_fps'])+1)})
|
|
|
|
|
print({"dert_fps": (j-int(first_fps_time['get_fps'])+1)})
|
|
|
|
|
first_fps_time.update({"dert_fps": (j-int(first_fps_time['get_fps'])+1)})
|
|
|
|
|
det_fps_time.append(first_fps_time)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# print('det_fps_time:', det_fps_time)
|
|
|
|
|
print('det_fps_time:',det_fps_time)
|
|
|
|
|
|
|
|
|
|
if det_fps_time:
|
|
|
|
|
re_list = json_get(
|
|
|
|
|
time_list=det_fps_time, video_path=video_path,fps=fps)
|
|
|
|
|
re_list = json_get(time_list=det_fps_time,video_path=video_path)
|
|
|
|
|
json_save(re_list)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print(video_path)
|
|
|
|
|
os.remove(video_path)
|
|
|
|
|
print('----------------------------------------------clear videos-----------------------------------------------')
|
|
|
|
|
print('clear videos')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 重置
|
|
|
|
|
print('----------------------------------------------next-----------------------------------------------')
|
|
|
|
|
det_img.clear()
|
|
|
|
|
det_fps_time.clear()
|
|
|
|
|
det_t_num = 0
|
|
|
|
|
nodet_t_num = 0
|
|
|
|
|
video_name_time = 0
|
|
|
|
|
j = 0
|
|
|
|
|
|
|
|
|
|
# print('det_fps_time:', det_fps_time,'det_img:',det_img)
|
|
|
|
|
print('det_fps_time:', len(det_fps_time), i, j)
|
|
|
|
|
|
|
|
|
|
# break
|
|
|
|
|
t2 = time.time()
|
|
|
|
|
tx = t2 - t1
|
|
|
|
|
print('检测一张图片的时间为:', tx)
|
|
|
|
@ -206,8 +182,7 @@ def rtsp_detect_process(source, model_data, model_inference):
|
|
|
|
|
|
|
|
|
|
cap.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dir_source_process(source, model_inference, model_data):
|
|
|
|
|
if dir_source:
|
|
|
|
|
|
|
|
|
|
img_ext = [".jpg", ".JPG", ".bmp"]
|
|
|
|
|
video_ext = [".mp4", ".avi", ".MP4"]
|
|
|
|
@ -235,8 +210,7 @@ def dir_source_process(source, model_inference, model_data):
|
|
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def file_source_process(source, model_inference, model_data):
|
|
|
|
|
if file_source:
|
|
|
|
|
|
|
|
|
|
img_para = True
|
|
|
|
|
|
|
|
|
@ -274,14 +248,16 @@ def img_process(images, model_inference, model_data):
|
|
|
|
|
|
|
|
|
|
determine_bbox = select_labels_list
|
|
|
|
|
|
|
|
|
|
# print(determine_bbox)
|
|
|
|
|
|
|
|
|
|
print(determine_bbox)
|
|
|
|
|
|
|
|
|
|
if model_data['model_parameter']['object_num_min'] :
|
|
|
|
|
if len(determine_bbox) >= model_data["model_parameter"]['object_num_min']:
|
|
|
|
|
if len(determine_bbox) <= model_data["model_parameter"]['object_num_min']:
|
|
|
|
|
|
|
|
|
|
# print(len(determine_bbox))
|
|
|
|
|
print(len(determine_bbox))
|
|
|
|
|
determine_bbox.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 返回检测后结果
|
|
|
|
|
return determine_bbox
|
|
|
|
|
|
|
|
|
@ -347,6 +323,9 @@ def images_save(images, save_path):
|
|
|
|
|
# data_now.year) + '/' + str(data_now.month) + '_' + str(data_now.day) + '/'
|
|
|
|
|
img_save_path = os.path.join(save_path, str(images['path'].split('.')[-1]))
|
|
|
|
|
images_name = images['get_time'] + '.jpg'
|
|
|
|
|
# img_save_path = save_path + '/' + str(images['path'].split('.')[-1]) + '/'
|
|
|
|
|
|
|
|
|
|
# print(img_save_path)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(img_save_path):
|
|
|
|
|
os.makedirs(img_save_path)
|
|
|
|
@ -381,7 +360,7 @@ def determine_time(det_num, nodet_num, ratio_set):
|
|
|
|
|
|
|
|
|
|
ratio = det_num / (det_num + nodet_num)
|
|
|
|
|
|
|
|
|
|
# print(det_num, nodet_num, ratio)
|
|
|
|
|
print(det_num, nodet_num, ratio)
|
|
|
|
|
|
|
|
|
|
if ratio >= ratio_set:
|
|
|
|
|
|
|
|
|
@ -409,10 +388,9 @@ def video_synthesis(imglist, savePath, size, fps, videoname):
|
|
|
|
|
out.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def json_get(time_list, video_path,fps):
|
|
|
|
|
def json_get(time_list,video_path):
|
|
|
|
|
|
|
|
|
|
result_dict ={'info': {'video_path': video_path,'fps':fps}}
|
|
|
|
|
re_dict = {}
|
|
|
|
|
result_dict = {'video_path':video_path}
|
|
|
|
|
for i,det_dict in enumerate(time_list):
|
|
|
|
|
|
|
|
|
|
list_hands = ["Keypad","hands","keyboard", "mouse","phone"]
|
|
|
|
@ -426,19 +404,33 @@ def json_get(time_list, video_path,fps):
|
|
|
|
|
|
|
|
|
|
result_lables = "sleep"
|
|
|
|
|
|
|
|
|
|
fps_dict = {'time': det_dict['get_fps'],
|
|
|
|
|
'duration': det_dict['dert_fps'], 'result': result_lables}
|
|
|
|
|
re_dict.update({('id_' + str(i)): fps_dict})
|
|
|
|
|
|
|
|
|
|
result_dict.update({'result':re_dict})
|
|
|
|
|
fps_dict = {'time': det_dict['get_fps'],'duration':det_dict['dert_fps'],'result':result_lables}
|
|
|
|
|
result_dict.update({('id_'+ str(i)):fps_dict})
|
|
|
|
|
|
|
|
|
|
return result_dict
|
|
|
|
|
|
|
|
|
|
# def json_analysis(re_list):
|
|
|
|
|
|
|
|
|
|
def json_save(result_dict):
|
|
|
|
|
# update_list = []
|
|
|
|
|
# copy_list = [x for x in re_list not in update_list]
|
|
|
|
|
|
|
|
|
|
# for i in range(len(copy_list)-1):
|
|
|
|
|
|
|
|
|
|
# j = i + 1
|
|
|
|
|
|
|
|
|
|
json_path = result_dict['info`']['video_path'].split('.')[0] + '.json'
|
|
|
|
|
# re_i = int(re_list[i]['fps'])
|
|
|
|
|
# re_i_add = int(re_list[i]['dert_fps'])
|
|
|
|
|
# re_j = int(re_list[j]['fps'])
|
|
|
|
|
|
|
|
|
|
# if re_i + re_i_add == re_j:
|
|
|
|
|
|
|
|
|
|
# update_list.append(re_i,re_j)
|
|
|
|
|
# print()
|
|
|
|
|
|
|
|
|
|
def json_save(result_dict):
|
|
|
|
|
|
|
|
|
|
json_path = result_dict['video_path'].split('.')[0] + '.json'
|
|
|
|
|
del result_dict['video_path']
|
|
|
|
|
result = json.dumps(result_dict)
|
|
|
|
|
|
|
|
|
|
f = open(json_path,'w')
|
|
|
|
|