|
|
|
@ -20,6 +20,8 @@ import json
|
|
|
|
|
|
|
|
|
|
def data_load(args):
|
|
|
|
|
|
|
|
|
|
# print('正在运行的进程',msg)
|
|
|
|
|
# print(args)
|
|
|
|
|
source = args[0]
|
|
|
|
|
model_ymal = args[1]
|
|
|
|
|
|
|
|
|
@ -36,217 +38,189 @@ def data_load(args):
|
|
|
|
|
|
|
|
|
|
if rtsp_source:
|
|
|
|
|
|
|
|
|
|
rtsp_detect_process(source=source, model_data=model_data,
|
|
|
|
|
model_inference=model_inference)
|
|
|
|
|
cap = cv2.VideoCapture(source)
|
|
|
|
|
|
|
|
|
|
if dir_source:
|
|
|
|
|
dir_source_process(source, model_inference, model_data)
|
|
|
|
|
|
|
|
|
|
if file_source:
|
|
|
|
|
# 视频流信息
|
|
|
|
|
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
|
|
|
|
fps_num = fps*model_data['detect_time']
|
|
|
|
|
fps_num_small = fps*model_data['detect_time_small']
|
|
|
|
|
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
|
|
|
|
|
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
|
|
|
|
|
try:
|
|
|
|
|
i = 0
|
|
|
|
|
j = 0
|
|
|
|
|
|
|
|
|
|
file_source_process(source, model_inference, model_data)
|
|
|
|
|
det_t_num = 0
|
|
|
|
|
nodet_t_num = 0
|
|
|
|
|
|
|
|
|
|
det_img = []
|
|
|
|
|
|
|
|
|
|
def rtsp_detect_process(source, model_data, model_inference):
|
|
|
|
|
video_name_time = 0
|
|
|
|
|
det_fps_time = []
|
|
|
|
|
|
|
|
|
|
cap = cv2.VideoCapture(source)
|
|
|
|
|
while True:
|
|
|
|
|
ret, frame = cap.read()
|
|
|
|
|
|
|
|
|
|
# 视频流信息
|
|
|
|
|
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
|
|
|
|
fps_num = fps*model_data['detect_time']
|
|
|
|
|
fps_num_small = fps*model_data['detect_time_small']
|
|
|
|
|
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
|
|
|
|
|
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
|
|
|
|
|
try:
|
|
|
|
|
i = 0
|
|
|
|
|
j = 0
|
|
|
|
|
if not ret:
|
|
|
|
|
continue # 如果未成功读取到视频帧,则继续读取下一帧
|
|
|
|
|
|
|
|
|
|
det_t_num = 0
|
|
|
|
|
nodet_t_num = 0
|
|
|
|
|
i = i + 1
|
|
|
|
|
j = j + 1
|
|
|
|
|
|
|
|
|
|
det_img = []
|
|
|
|
|
# 读取到当前视频帧时间
|
|
|
|
|
data_now = datetime.now()
|
|
|
|
|
get_time = str(data_now.strftime("%H")) + \
|
|
|
|
|
str(data_now.strftime("%M")) + str(data_now.strftime("%S")) + \
|
|
|
|
|
str(data_now.strftime("%f"))
|
|
|
|
|
|
|
|
|
|
video_name_time = 0
|
|
|
|
|
det_fps_time = []
|
|
|
|
|
# 视频保存
|
|
|
|
|
if video_name_time == 0:
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
ret, frame = cap.read()
|
|
|
|
|
video_name_time = get_time
|
|
|
|
|
savePath = os.path.join(model_data['save_videos'], (str(data_now.strftime(
|
|
|
|
|
"%Y")) + str(data_now.strftime("%m")) + str(data_now.strftime("%d"))))
|
|
|
|
|
|
|
|
|
|
t1 = time.time()
|
|
|
|
|
if not os.path.exists(savePath):
|
|
|
|
|
os.makedirs(savePath)
|
|
|
|
|
|
|
|
|
|
if not ret:
|
|
|
|
|
continue # 如果未成功读取到视频帧,则继续读取下一帧
|
|
|
|
|
video_path = os.path.join(
|
|
|
|
|
savePath, video_name_time + '.avi')
|
|
|
|
|
print('video_path:', video_path)
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
j = j + 1
|
|
|
|
|
out_video = cv2.VideoWriter(
|
|
|
|
|
video_path, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
|
|
|
|
|
|
|
|
|
|
# 读取到当前视频帧时间
|
|
|
|
|
data_now = datetime.now()
|
|
|
|
|
get_time = str(data_now.strftime("%H")) + \
|
|
|
|
|
str(data_now.strftime("%M")) + str(data_now.strftime("%S")) + \
|
|
|
|
|
str(data_now.strftime("%f"))
|
|
|
|
|
print(source, data_now, i,j)
|
|
|
|
|
t1 = time.time()
|
|
|
|
|
|
|
|
|
|
# 视频保存
|
|
|
|
|
if video_name_time == 0:
|
|
|
|
|
imgframe_dict = {"path": source,
|
|
|
|
|
'frame': frame,
|
|
|
|
|
'get_fps': j}
|
|
|
|
|
|
|
|
|
|
video_name_time = get_time
|
|
|
|
|
savePath = os.path.join(model_data['save_videos'], (str(data_now.strftime(
|
|
|
|
|
"%Y")) + str(data_now.strftime("%m")) + str(data_now.strftime("%d"))))
|
|
|
|
|
images_det_result = img_process(imgframe_dict,
|
|
|
|
|
model_inference,
|
|
|
|
|
model_data)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(savePath):
|
|
|
|
|
os.makedirs(savePath)
|
|
|
|
|
images_update = save_process(imgframe_dict,
|
|
|
|
|
images_det_result,
|
|
|
|
|
model_data)
|
|
|
|
|
|
|
|
|
|
video_path = os.path.join(
|
|
|
|
|
savePath, video_name_time + '.avi')
|
|
|
|
|
print('video_path:', video_path)
|
|
|
|
|
print('images_det_result:',len(images_det_result))
|
|
|
|
|
|
|
|
|
|
out_video = cv2.VideoWriter(
|
|
|
|
|
video_path, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
|
|
|
|
|
if images_det_result:
|
|
|
|
|
|
|
|
|
|
print(source, data_now, i, j,video_path)
|
|
|
|
|
|
|
|
|
|
det_t_num = det_t_num + 1
|
|
|
|
|
|
|
|
|
|
# 推理部分
|
|
|
|
|
imgframe_dict = {"path": source, 'frame': frame, 'get_fps': j}
|
|
|
|
|
|
|
|
|
|
images_det_result = img_process(
|
|
|
|
|
imgframe_dict, model_inference, model_data)
|
|
|
|
|
# print(len(det_img))
|
|
|
|
|
if len(det_img) == 0:
|
|
|
|
|
img_dict = images_update.copy()
|
|
|
|
|
del img_dict['frame']
|
|
|
|
|
det_img.append(img_dict)
|
|
|
|
|
|
|
|
|
|
images_update = save_process(
|
|
|
|
|
imgframe_dict, images_det_result, model_data)
|
|
|
|
|
if not images_det_result and len(det_img) > 0:
|
|
|
|
|
|
|
|
|
|
# print('images_det_result:', len(images_det_result))
|
|
|
|
|
nodet_t_num = nodet_t_num + 1
|
|
|
|
|
|
|
|
|
|
# 结果判断,t
|
|
|
|
|
if images_det_result:
|
|
|
|
|
if (det_t_num + nodet_t_num) >= fps_num_small:
|
|
|
|
|
|
|
|
|
|
det_t_num = det_t_num + 1
|
|
|
|
|
para = determine_time(det_num=det_t_num,
|
|
|
|
|
nodet_num=nodet_t_num,
|
|
|
|
|
ratio_set=model_data['detect_ratio'])
|
|
|
|
|
|
|
|
|
|
if len(det_img) == 0:
|
|
|
|
|
img_dict = images_update.copy()
|
|
|
|
|
del img_dict['frame']
|
|
|
|
|
det_img.append(img_dict)
|
|
|
|
|
if para:
|
|
|
|
|
|
|
|
|
|
if not images_det_result and len(det_img) > 0:
|
|
|
|
|
|
|
|
|
|
nodet_t_num = nodet_t_num + 1
|
|
|
|
|
|
|
|
|
|
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'])
|
|
|
|
|
first_fps_time = det_img[0]
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
if para:
|
|
|
|
|
det_img.clear()
|
|
|
|
|
det_t_num = 0
|
|
|
|
|
nodet_t_num = 0
|
|
|
|
|
|
|
|
|
|
first_fps_time = det_img[0]
|
|
|
|
|
first_fps_time.update(
|
|
|
|
|
{"dert_fps": (j-int(first_fps_time['get_fps'])+1)})
|
|
|
|
|
det_fps_time.append(first_fps_time)
|
|
|
|
|
# print('det_img:', len(det_img), det_t_num, nodet_t_num)
|
|
|
|
|
|
|
|
|
|
det_img.clear()
|
|
|
|
|
det_t_num = 0
|
|
|
|
|
nodet_t_num = 0
|
|
|
|
|
out_video.write(images_update['frame'])
|
|
|
|
|
|
|
|
|
|
# 视频保存
|
|
|
|
|
out_video.write(images_update['frame'])
|
|
|
|
|
if j >= fps_num:
|
|
|
|
|
|
|
|
|
|
# 结果判断 ,T
|
|
|
|
|
if j >= fps_num:
|
|
|
|
|
out_video.release()
|
|
|
|
|
|
|
|
|
|
if det_img:
|
|
|
|
|
|
|
|
|
|
out_video.release()
|
|
|
|
|
first_fps_time = det_img[0]
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
# T时间截至,判断t时间结果。
|
|
|
|
|
if det_img:
|
|
|
|
|
para = determine_time(
|
|
|
|
|
det_num=det_t_num, nodet_num=nodet_t_num, ratio_set=model_data['detect_ratio'])
|
|
|
|
|
print('det_fps_time:',det_fps_time)
|
|
|
|
|
|
|
|
|
|
first_fps_time = det_img[0]
|
|
|
|
|
if det_fps_time:
|
|
|
|
|
re_list = json_get(time_list=det_fps_time,video_path=video_path)
|
|
|
|
|
json_save(re_list)
|
|
|
|
|
|
|
|
|
|
# print(j-int(first_fps_time['get_fps'])+1)
|
|
|
|
|
# print(fps_num_small/2)
|
|
|
|
|
else:
|
|
|
|
|
print(video_path)
|
|
|
|
|
os.remove(video_path)
|
|
|
|
|
print('clear videos')
|
|
|
|
|
|
|
|
|
|
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.clear()
|
|
|
|
|
video_name_time = 0
|
|
|
|
|
j = 0
|
|
|
|
|
|
|
|
|
|
print('det_fps_time:', len(det_fps_time), i, j)
|
|
|
|
|
|
|
|
|
|
# break
|
|
|
|
|
t2 = time.time()
|
|
|
|
|
tx = t2 - t1
|
|
|
|
|
print('检测一张图片的时间为:', tx)
|
|
|
|
|
|
|
|
|
|
# print('det_fps_time:', det_fps_time)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# 处理异常或错误
|
|
|
|
|
print(str(e))
|
|
|
|
|
|
|
|
|
|
if det_fps_time:
|
|
|
|
|
re_list = json_get(
|
|
|
|
|
time_list=det_fps_time, video_path=video_path,fps=fps)
|
|
|
|
|
json_save(re_list)
|
|
|
|
|
cap.release()
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print(video_path)
|
|
|
|
|
os.remove(video_path)
|
|
|
|
|
print('----------------------------------------------clear videos-----------------------------------------------')
|
|
|
|
|
if dir_source:
|
|
|
|
|
|
|
|
|
|
# 重置
|
|
|
|
|
print('----------------------------------------------next-----------------------------------------------')
|
|
|
|
|
det_img.clear()
|
|
|
|
|
det_fps_time.clear()
|
|
|
|
|
det_t_num = 0
|
|
|
|
|
nodet_t_num = 0
|
|
|
|
|
video_name_time = 0
|
|
|
|
|
j = 0
|
|
|
|
|
img_ext = [".jpg", ".JPG", ".bmp"]
|
|
|
|
|
video_ext = [".mp4", ".avi", ".MP4"]
|
|
|
|
|
|
|
|
|
|
# print('det_fps_time:', det_fps_time,'det_img:',det_img)
|
|
|
|
|
img_list = get_dir_file(source, img_ext)
|
|
|
|
|
video_list = get_dir_file(source, video_ext)
|
|
|
|
|
|
|
|
|
|
t2 = time.time()
|
|
|
|
|
tx = t2 - t1
|
|
|
|
|
print('检测一张图片的时间为:', tx)
|
|
|
|
|
if img_list:
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# 处理异常或错误
|
|
|
|
|
print(str(e))
|
|
|
|
|
for img in img_list:
|
|
|
|
|
|
|
|
|
|
cap.release()
|
|
|
|
|
t1 = time.time()
|
|
|
|
|
images = cv2.imread(img)
|
|
|
|
|
|
|
|
|
|
imgframe_dict = {"path": img, 'frame': images}
|
|
|
|
|
|
|
|
|
|
def dir_source_process(source, model_inference, model_data):
|
|
|
|
|
images_update = img_process(
|
|
|
|
|
imgframe_dict, model_inference, model_data)
|
|
|
|
|
|
|
|
|
|
img_ext = [".jpg", ".JPG", ".bmp"]
|
|
|
|
|
video_ext = [".mp4", ".avi", ".MP4"]
|
|
|
|
|
t2 = time.time()
|
|
|
|
|
tx = t2 - t1
|
|
|
|
|
print('检测一张图片的时间为:', tx)
|
|
|
|
|
|
|
|
|
|
img_list = get_dir_file(source, img_ext)
|
|
|
|
|
video_list = get_dir_file(source, video_ext)
|
|
|
|
|
if video_list:
|
|
|
|
|
|
|
|
|
|
if img_list:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
for img in img_list:
|
|
|
|
|
if file_source:
|
|
|
|
|
|
|
|
|
|
t1 = time.time()
|
|
|
|
|
images = cv2.imread(img)
|
|
|
|
|
img_para = True
|
|
|
|
|
|
|
|
|
|
imgframe_dict = {"path": img, 'frame': images}
|
|
|
|
|
if img_para:
|
|
|
|
|
images = cv2.imread(source)
|
|
|
|
|
imgframe_dict = {"path": source, 'frame': images}
|
|
|
|
|
|
|
|
|
|
images_update = img_process(
|
|
|
|
|
imgframe_dict, model_inference, model_data)
|
|
|
|
|
|
|
|
|
|
t2 = time.time()
|
|
|
|
|
tx = t2 - t1
|
|
|
|
|
print('检测一张图片的时间为:', tx)
|
|
|
|
|
|
|
|
|
|
if video_list:
|
|
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def file_source_process(source, model_inference, model_data):
|
|
|
|
|
|
|
|
|
|
img_para = True
|
|
|
|
|
|
|
|
|
|
if img_para:
|
|
|
|
|
images = cv2.imread(source)
|
|
|
|
|
imgframe_dict = {"path": source, 'frame': images}
|
|
|
|
|
|
|
|
|
|
images_update = img_process(
|
|
|
|
|
imgframe_dict, model_inference, model_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def img_process(images, model_inference, model_data):
|
|
|
|
|
|
|
|
|
@ -273,15 +247,17 @@ def img_process(images, model_inference, model_data):
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
determine_bbox = select_labels_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# print(determine_bbox)
|
|
|
|
|
|
|
|
|
|
if model_data['model_parameter']['object_num_min']:
|
|
|
|
|
if len(determine_bbox) >= model_data["model_parameter"]['object_num_min']:
|
|
|
|
|
print(determine_bbox)
|
|
|
|
|
|
|
|
|
|
if 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,14 +388,13 @@ 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 = {}
|
|
|
|
|
for i, det_dict in enumerate(time_list):
|
|
|
|
|
result_dict = {'video_path':video_path}
|
|
|
|
|
for i,det_dict in enumerate(time_list):
|
|
|
|
|
|
|
|
|
|
list_hands = ["Keypad", "hands", "keyboard", "mouse", "phone"]
|
|
|
|
|
list_sleep = ["person", "sleep"]
|
|
|
|
|
list_hands = ["Keypad","hands","keyboard", "mouse","phone"]
|
|
|
|
|
list_sleep = ["person","sleep"]
|
|
|
|
|
|
|
|
|
|
if list(det_dict['results'][0].keys())[0] in list_hands:
|
|
|
|
|
|
|
|
|
@ -426,22 +404,36 @@ 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})
|
|
|
|
|
fps_dict = {'time': det_dict['get_fps'],'duration':det_dict['dert_fps'],'result':result_lables}
|
|
|
|
|
result_dict.update({('id_'+ str(i)):fps_dict})
|
|
|
|
|
|
|
|
|
|
result_dict.update({'result':re_dict})
|
|
|
|
|
|
|
|
|
|
return result_dict
|
|
|
|
|
|
|
|
|
|
# def json_analysis(re_list):
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
# 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['info`']['video_path'].split('.')[0] + '.json'
|
|
|
|
|
|
|
|
|
|
result = json.dumps(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')
|
|
|
|
|
f = open(json_path,'w')
|
|
|
|
|
f.write(result + '\n')
|
|
|
|
|
f.close
|
|
|
|
|
|
|
|
|
|