update code

V0.1.0
王莹 2 years ago
parent 94bdfc7663
commit 0d19061d9a

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

@ -9,13 +9,13 @@ def build_option(device, backend, cache_file):
""" """
option = fd.RuntimeOption() option = fd.RuntimeOption()
option.use_cpu() option.use_cpu()
option.trt_option.serialize_file = cache_file # option.trt_option.serialize_file = cache_file
if device.lower() == "gpu": # if device.lower() == "gpu":
option.use_gpu(0) # option.use_gpu(0)
if backend.lower() == "trt": # if backend.lower() == "trt":
assert device.lower( # assert device.lower(
) == "gpu", "TensorRT backend require inference on device GPU." # ) == "gpu", "TensorRT backend require inference on device GPU."
option.use_trt_backend() # option.use_trt_backend()
return option return option

Loading…
Cancel
Save