您的当前位置:首页正文

动作识别数据集,视频转帧

2024-12-01 来源:个人技术集锦
import os
import argparse
import cv2
import numpy as np
parse = argparse.ArgumentParser()
parse.add_argument('--Video_path',type=str, default= '/media/data/RPZ/UCF11_updated_mpg/video') #原始视频的地址
parse.add_argument('--Fream_path',type=str, default='/media/data/RPZ/UCF11_updated_mpg/fream')
args = parse.parse_args()


def video2frame(args):
    n_count = len(os.listdir(args.Video_path)) #总视频类别的个数
    count = 0 #存储当前处理视频的个数
    n_frames = [] #存储视频片段帧的个数
    for videos_file in sorted(os.listdir(args.Video_path)):
        videos_file_path = args.Video_path + '/' + videos_file #/media/data/RPZ/UCF11_updated_mpg/video/basketball
        video_file_path = sorted(os.listdir(videos_file_path))
        video_file_path.remove('Annotation')
        count += 1 #存储当前处理视频的个数
        for video_file in video_file_path:
            video_file_path = videos_file_path + '/' + video_file #/media/data/RPZ/UCF11_updated_mpg/video/basketball/v_shooting_01
            for clip_video in sorted(os.listdir(video_file_path)):
                clip_video_path = video_file_path + '/' + clip_video #/media/data/RPZ/UCF11_updated_mpg/video/basketball/v_shooting_01/v_shooting_01_01.mpg
                vc = cv2.VideoCapture(clip_video_path) #进行视频的载入
                rval = vc.isOpened() #判断载入的视频是否可以打开
                if not rval:
                    print('Video read failed!')
                else:
                    save_frame_path = os.path.join(args.Fream_path, videos_file, video_file, clip_video.split('.')[0]) #/media/data/RPZ/UCF11_updated_mpg/fream/basketball/v_shooting_01/v_shooting_01_01
                    if not os.path.exists(save_frame_path):
                        os.makedirs(save_frame_path)
                    else:
                        continue
                    c = 0
                    while rval:
                        c = c + 1
                        rval, frame = vc.read()  #进行单张图片的读取,rval的值为True或者Flase, frame表示读入的图片
                        if rval:
                            clip_name = clip_video.split('.')[0] + '-' + str('%06d' % c) + '.jpg'
                            img_path = os.path.join(save_frame_path, clip_name) #/media/data/RPZ/UCF11_updated_mpg/fream/basketball/v_shooting_01/v_shooting_01_01-000001.jpg
                            # cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # 表示将图片转换为灰度图
                            cv2.imwrite(img_path, frame)  # 存储视频帧
                            cv2.waitKey(1)
                        else:
                            break
                    c = c - 1  # 帧的个数
                    n_frames += [c]
                vc.release()
        print(np.average(n_frames))
        print('videos_file: %s, count n_count: %04d | %04d' % (videos_file, count, n_count))
        np.save('UCF11-n_Frames.npy', n_frames)
    return n_frames
    

if __name__ == '__main__':
    video2frame(args)
    

显示全文