Atlas 200I DK A2视频保存

Atlas 200I DK A2开发者套件内置案例第一个目标检测,视频保存下来只能用VLC打开,电脑自带的播放器打不开,应该是ffmpeg生成格式问题(参考:解决ffmpeg生成mp4文件不能正常预览的问题_mp4reader__无相_的博客-CSDN博客),修改为cv2保存可用自带播放器正常在本地展示。

原代码部分

def infer_video(video_path, model, labels_dict, cfg, output_path='output.mp4'):
    """视频推理"""
    image_widget = widgets.Image(format='jpeg', width=800, height=600)
    display(image_widget)

    # 读入视频
    cap = vreader(video_path)
    video_writer = None
    for img_frame in cap:
        # 对视频帧进行推理
        image_pred = infer_frame_with_vis(img_frame, model, labels_dict, cfg, bgr2rgb=False)
        image_widget.value = img2bytes(image_pred[:, :, ::-1])

        if video_writer is None:
            # 初始化视频写入对象
            video_writer = FFmpegWriter(output_path)
        # 将每一帧的预测结果写入视频对象
        video_writer.writeFrame(image_pred)
    video_writer.close()

视频打不开

 Atlas 200I DK A2视频保存_第1张图片

VLC能打开

 

修改后(参考:Pyhon cv2.VideoWriter 保存视频_cv2保存视频_南洲.的博客-CSDN博客)

def infer_video(video_path, model, labels_dict, cfg, output_path='output.mp4'):
    """视频推理"""
    # 读入视频
    cap = cv2.VideoCapture(video_path)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = cap.get(cv2.CAP_PROP_FPS)
    fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
    outVideo = cv2.VideoWriter('save_test_video.avi', fourcc, fps, (width,height))
    
    image_widget = widgets.Image(format='jpeg', width=800, height=600)
    display(image_widget)
    
    while True:
        # 对视频帧进行推理
        ret, frame = cap.read()
        if not ret:
            print("... end of video file reached")
            break
        image_pred = infer_frame_with_vis(frame, model, labels_dict, cfg, bgr2rgb=False)
        image_widget.value = img2bytes(image_pred)
        outVideo.write(image_pred)

保存为avi格式,也能在jupyter中正常展示:

你可能感兴趣的:(音视频,计算机视觉,python)