python中根据视频帧生成视频,保存为mp4格式

python中根据视频帧生成视频,保存为mp4格式:

def visualize(frame_data, box, predict_labels, size, fps, video_path, out_path='./results/'):
    if not os.path.exists(out_path):
        os.makedirs(out_path)
    out_name = video_path.split('/')[-1].split('.')[0] + '.mp4'
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video_writer = cv2.VideoWriter(os.path.join(out_path, out_name), fourcc, fps, size)
    for frame_id, frame in enumerate(frame_data):
        boxes = box[frame_id].astype(np.int32)
        for person_id, person in enumerate(boxes):
            cv2.rectangle(frame, (person[0], person[1]), (person[2], person[3]), (0, 255, 0), thickness=2)
            for label_id, label in enumerate(predict_labels[person_id]):
                cv2.putText(frame, label, (person[0]+10, person[1] + 20 + 20*label_id),
                            fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                            fontScale=0.65,
                            color=(0, 0, 255),
                            thickness=1)
        video_writer.write(frame)
    video_writer.release()

 

你可能感兴趣的:(图像处理,python)