输入:存放有很多视频文件的文件夹路径
输出:包含采样前后视频信息的csv文件
import os import cv2 import pandas as pd import numpy as np from PIL import Image def get_videos(vidname, read_path): allframes = [] videoins = read_path + vidname vvv = cv2.VideoCapture(videoins) if not vvv.isOpened(): print('Video is not opened! {}'.format(videoins)) else: fps = vvv.get(cv2.CAP_PROP_FPS) sampleFps = 10 ########################### 设定固定fps 10 totalFrameNumber = vvv.get(cv2.CAP_PROP_FRAME_COUNT) size = (int(vvv.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vvv.get(cv2.CAP_PROP_FRAME_HEIGHT))) second = totalFrameNumber//fps selectNum = second * sampleFps skipNum = (totalFrameNumber-selectNum)/selectNum skipNum = int(skipNum)+1 # 向下取整 ''' if totalFrameNumber != 0: for _ in range(int(totalFrameNumber)): rval, frame = vvv.read() if frame is not None: img = Image.fromarray(frame.astype('uint8')).convert('RGB') imgtrans = centrans(img).numpy() allframes.append(imgtrans) ''' if totalFrameNumber != 0: cnt = 0 cnt_total = 0 for _ in range(int(totalFrameNumber)): rval, frame = vvv.read() if frame is not None: if cnt >= skipNum: cnt_total += 1 cnt = 0 cnt += 1 #print("select num ",cnt_total) #print("total num ",totalFrameNumber) #print("second", second) #print("fps ",fps) return fps,second,int(totalFrameNumber),cnt_total if __name__ == "__main__": savepath = '/test_npy_10fps/' datapath = '/THUMOS/test/' os.chdir(datapath) allvideos = os.listdir() allvideos.sort() list_content = [] for vid in range(len(allvideos)): fps,second,totalframeNum,cnt_total = get_videos(allvideos[vid], datapath) ############################################################################## #list_content.append([allvideos[vid],totalframeNum,second,fps,10,"training",cnt_total]) list_content.append([allvideos[vid],totalframeNum,second,fps,10,"validation",cnt_total]) ############################################################################## print('%d video has been done!' % vid) data2 = pd.DataFrame(data = list_content,index = None,columns = ["video","numFrame","seconds","fps","rfps","subset","featureFrame"]) # PATH为导出文件的路径和文件名 #data2.to_csv("../train_info.csv") data2.to_csv("../test_info.csv") print('all %d video have been done!' % len(allvideos))标签:采样,cnt,vvv,totalFrameNumber,second,fps,total,时长 From: https://www.cnblogs.com/traceofMind/p/17736272.html