原始视频文件,需要进行标签和图象处理,才能得到效果较好的数据集。
1. 视频初处理
首先需要对原始视频进行截断,修改尺寸,并保存为易于读取的格式。
#create dataset
video_file = r'./test.mp4'
cap = cv2.VideoCapture(video_file)
fps = 24
num_frames = 4*60*fps
video_size = (1920,1080)
video_sub_file = r'./test_sub.avi'
fourcc = cv2.VideoWriter_fourcc(*"mp42")
video_writer = cv2.VideoWriter(video_sub_file, fourcc, fps, video_size)
for i in tqdm(range(num_frames)):
res, frame = cap.read()
if not res:
print("camera error")
break
video_writer.write(frame)
cap.release()
print("load finish")
fps = 24
num_frames = 4*60*fps
video_sub_file = r'./test_sub.avi'
cap = cv2.VideoCapture(video_sub_file)
video_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
video_heigth = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(video_width,video_heigth)
# print(cap.isOpened())
# print(cap.read())
save_dir = './dataset'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
for i in tqdm(range(num_frames)):
res, image = cap.read()
#print(res)
minute = i//(60*fps)
second = (i - minute*60*fps)//fps
if not res:
print("camera error")
break
mid_dir = os.path.join(save_dir, 'v_{0}_c_{1:0>2d}_{2:0>2d}_t'.format(minute+1, minute, second))
if not os.path.exists(mid_dir):
os.makedirs(mid_dir)
frame_name = os.path.join(mid_dir, 'frame{:0>6d}.jpg'.format(i%fps+1))
new_image = cv2.resize(image,(255,255))
video_file = cv2.imwrite(frame_name, new_image)
cap.release()
print("dataset finish")
# img_path = './dataset/v_1_c_00_00_t/frame000001.jpg'
# img = cv2.imread(img_path)
# print(img.shape)
最后结果将原始视频调整为4min、24fps、(255,255)尺寸的的数据集。
dataset
网友评论