pytorch实战01-将avi格式视频转为视频帧

目录

1、导入python第三方库

2、定义路径参数

3、遍历文件夹及视频

3.1遍历视频

3.2遍历文件夹

4、完整代码


1、导入python第三方库

   代码使用的是anaconda编写

import os
import numpy as np
import cv2

 

2、定义路径参数

定义自己的视频路径和需要保存的视频帧路径

# 定义源视频根路径路径
video_root_Path = "F:/dataSet/meng_data/origin_video"
# 定义帧的存放路径
frame_root_path = "F:/dataSet/meng_data/origin_jpg"

label_name = os.listdir(video_root_Path )
label_name

 获取根路径下的文件夹(以HMDB数据集为例)

3、遍历文件夹及视频

pytorch实战01-将avi格式视频转为视频帧_第1张图片

3.1遍历视频

遍历视频文件
    1. 遍历所有视频文件
    2. 将视频路径、帧的保存路径传过来

 

def forEach(videos, video_src_path, frame_save_path):
    for each_video in videos:
        each_video_name, _ = each_video.split('.')
        if not os.path.exists(frame_save_path + '/' + each_video_name):
            os.mkdir(frame_save_path + '/' + each_video_name)
            
        each_video_save_full_path = os.path.join(frame_save_path, each_video_name) + '/'
        each_video_full_path = os.path.join(video_src_path, each_video)
        cap = cv2.VideoCapture(each_video_full_path) #  读取视频
        frame_count = 1
        success = True
        while success:
            success, frame = cap.read()
            # print('read a new frame:', success)

            params = []
            params.append(1)
            if success:
                cv2.imwrite(each_video_save_full_path + each_video_name + "_%d.jpg" % 
                            frame_count, frame, params)

            frame_count += 1
        cap.release()

3.2遍历文件夹

label_dir = {}
index = 0
for i in label_name:  # 遍历根文件夹名称
    if i.startswith('.'):
        continue
    label_dir[i] = index # 用数字代替类型, 封装成字典类型
    index += 1
    video_src_path = os.path.join(video_root_Path, i)
    frame_save_path = os.path.join(frame_root_path, i) + '_jpg'
    
    if not os.path.exists(frame_save_path):
        os.mkdir(frame_save_path)
    
    videos = os.listdir(video_src_path)
    
    videos = filter(lambda x: x.endswith('avi'), videos)
    
    forEach(videos, video_src_path, frame_save_path)
    print('文件  '+i+'  转换完成 ')
    
np.save('F:/dataSet/meng_data/origin_video/label_dir.npy', label_dir)  
print('程序运行完毕。。。')

pytorch实战01-将avi格式视频转为视频帧_第2张图片

4、完整代码

import os
import numpy as np
import cv2

# 定义源视频根路径路径
video_root_Path = "F:/dataSet/meng_data/origin_video"
# 定义帧的存放路径
frame_root_path = "F:/dataSet/meng_data/origin_jpg"
label_name = os.listdir(video_root_Path)


def forEach(videos, video_src_path, frame_save_path):
    for each_video in videos:
        each_video_name, _ = each_video.split('.')
        if not os.path.exists(frame_save_path + '/' + each_video_name):
            os.mkdir(frame_save_path + '/' + each_video_name)

        each_video_save_full_path = os.path.join(frame_save_path, each_video_name) + '/'
        each_video_full_path = os.path.join(video_src_path, each_video)
        cap = cv2.VideoCapture(each_video_full_path)  # 读取视频
        frame_count = 1
        success = True
        while success:
            success, frame = cap.read()
            # print('read a new frame:', success)

            params = []
            params.append(1)
            if success:
                cv2.imwrite(each_video_save_full_path + each_video_name + "_%d.jpg" % frame_count, frame, params)

            frame_count += 1
        cap.release()


label_dir = {}  # 定义字典
index = 0
for i in label_name:  # 遍历根文件夹名称
    if i.startswith('.'):
        continue
    label_dir[i] = index  # 用数字代替类型, 封装成字典类型
    index += 1
    video_src_path = os.path.join(video_root_Path, i)
    frame_save_path = os.path.join(frame_root_path, i) + '_jpg'

    if not os.path.exists(frame_save_path):
        os.mkdir(frame_save_path)

    videos = os.listdir(video_src_path)

    videos = filter(lambda x: x.endswith('avi'), videos)

    forEach(videos, video_src_path, frame_save_path)
    print('文件  ' + i + '  转换完成 ')


np.save('F:/dataSet/meng_data/origin_video/label_dir.npy', label_dir)
print('程序运行完毕。。。')


完整代码二:

import os
import numpy as np
import cv2

# 定义源视频根路径路径
video_root_Path = "E:/My_video"
# 定义帧的存放路径
frame_root_path = "E:/origin_jpg"
label_name = os.listdir(video_root_Path)


def forEach(videos, video_src_path, frame_save_path):
    for each_video in videos:
        each_video_name, _ = each_video.split('.')
        if not os.path.exists(frame_save_path + '/' + each_video_name):
            os.mkdir(frame_save_path + '/' + each_video_name)

        each_video_save_full_path = os.path.join(frame_save_path, each_video_name) + '/'
        each_video_full_path = os.path.join(video_src_path, each_video)
        cap = cv2.VideoCapture(each_video_full_path)  # 读取视频

        frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

        # Make sure splited video has at least 16 frames
        EXTRACT_FREQUENCY = 4
        if frame_count // EXTRACT_FREQUENCY <= 16:
            EXTRACT_FREQUENCY -= 1
            if frame_count // EXTRACT_FREQUENCY <= 16:
                EXTRACT_FREQUENCY -= 1
                if frame_count // EXTRACT_FREQUENCY <= 16:
                    EXTRACT_FREQUENCY -= 1


        # frame_count = 1
        count = 0
        i = 0
        success = True
        while (count < frame_count and success):
            success, frame = cap.read()
            # print('read a new frame:', success)
            if frame is None:
                continue

            params = []
            params.append(1)
            if count % EXTRACT_FREQUENCY == 0:
                # if (frame_height != self.resize_height) or (frame_width != self.resize_width):
                #     frame = cv2.resize(frame, (self.resize_width, self.resize_height))
                # cv2.imwrite(filename=os.path.join(save_dir, video_filename, '0000{}.jpg'.format(str(i))), img=frame)
                cv2.imwrite(each_video_save_full_path + each_video_name + "0000{}.jpg".format(str(i)), frame, params)
                i += 1
            count += 1
        cap.release()


label_dir = {}  # 定义字典
index = 0
for i in label_name:  # 遍历根文件夹名称
    if i.startswith('.'):
        continue
    label_dir[i] = index  # 用数字代替类型, 封装成字典类型
    index += 1
    video_src_path = os.path.join(video_root_Path, i)
    frame_save_path = os.path.join(frame_root_path, i) + '_jpg'

    if not os.path.exists(frame_save_path):
        os.mkdir(frame_save_path)

    videos = os.listdir(video_src_path)

    videos = filter(lambda x: x.endswith('avi'), videos)

    forEach(videos, video_src_path, frame_save_path)
    print('文件  ' + i + '  转换完成 ')

# np.save('F:/dataSet/meng_data/origin_video/label_dir.npy', label_dir)
print('程序运行完毕。。。')

 

你可能感兴趣的:(人工智能,python)