首页 > 解决方案 > 我在从视频中提取图像并将它们保存到特定文件夹时遇到问题

问题描述

每个人。我是一个在编程方面苦苦挣扎的大一新生。我将非常感谢您的任何帮助或建议。

我想让这段代码完成这些工作。

  1. 读取特定文件夹中的视频文件。
  2. 从此视频文件中提取帧图像
  3. 将它们保存在特定文件夹中(例如,从 video_1.mp4 制作文件夹 video_1_frameimage 并将图像 video_1_frame0、video_1_frame300、......等保存到此特定文件夹中)

为了实现这个功能,我编写了这段代码,但它并不能很好地工作。

感谢您阅读我的问题!

    import cv2
    import os
    
    path_for_video = "C:/Users/정지원/Desktop/Video_files"
    video_list = os.listdir(path_for_video)
    
    for video_file in video_list:
        file_path= os.path.join(path_for_video,video_file)
        cap = cv2.VideoCapture(file_path)
        index = 0
        while (True):
            ret, frame = cap.read()
            if ret == False:
                break
            if index % 300 == 0:
                name = video_file + '_frame' + str(index) + '.jpg'
                print('Creating...' + name)
                if not os.path.exists('C:/Users/정지원/Desktop/Video_files/'+ video_file):
                    os.makedirs('C:/Users/정지원/Desktop/Video_files/' + video_file)
                path_for_image = 'C:/Users/정지원/Desktop/Video_files/' + video_file

                cv2.imwrite(os.path.join(path_for_image, name), frame)

            index += 1
    
    
    cap.release()
    cv2.destroyAllWindows()

标签: pythonimagevideodirectoryframe

解决方案


推荐阅读