赞
踩
已有多个从视频中读取的CSV,CSV记录了帧数。
前两部分:将‘两类’多个CSV合并生成新的表格,记录帧数-原视频以及所在文件夹。
后两部分:根据CSV中Frame ID,提取视频中对应的图片
第一部分将多个CSV合并
files_2d = []
for i in range(1, 16):
files = glob.glob(f'/这里是你的路径,后面VP代表视频所在的文件夹/vp{i}/*.ids_1.openpose.2d.csv',
recursive = False)
files.sort()
files_2d.extend(files)
files_2d #输出结果
list_2d= [] list_3d= [] for filename in files_2d: df_2d = pd.read_csv(filename, index_col=None, header=0)[columns_list_2d] df_2d.insert(0, 'participant', os.path.basename(Path(filename).parent)) df_2d.insert(1, 'filename', os.path.basename(filename)) df_2d.insert(2, '2d', True) list_2d.append(df_2d) filename_3d = filename[:-6] + '3d.csv' df_3d = pd.read_csv(filename_3d, index_col=None, header=0)[columns_list_3d] df_3d.insert(0, 'participant', os.path.basename(Path(filename_3d).parent)) df_3d.insert(1, 'filename', os.path.basename(filename_3d)) df_3d.insert(2, '2d', False) list_3d.append(df_3d) key_points_2d = pd.concat(list_2d, axis=0, ignore_index=True) key_points_3d = pd.concat(list_3d, axis=0, ignore_index=True) key_points_2d.to_csv('/这里是你的路径/all_2d_annotations.csv', index=False) key_points_3d.to_csv('/这里是你的路径/all_3d_annotations.csv', index=False)
key_points_2d_path = '../all_2d_annotations.csv'
key_points_3d_path = '../all_3d_annotations.csv'
key_points_2d = pd.read_csv(key_points_2d_path, index_col=None, header=0)
key_points_2d #输出2d图
key_points_3d = pd.read_csv(key_points_3d_path)
key_points_3d #输出3D图
第二步从某个特定的FrameID(在汇总的Frame id)转化为针对某个特定视频的Frame id
import numpy as np import os Var1 = "VP10" #这一行不需要,只是分段编辑的时候实验用 Frame_id = 100000 #这个代表从总的CSV中想要读的Frame id Number_Video = Var1[2:] #我的视频文件夹为VP1到VP15,这里去掉前两位,取后面数字,用于之后拼接路径 print(Number_Video) #find Video Path拼接路径 Path = os.path.join('/home/KIT/SS 2022/CV Praktikum/Dataset/Annotations/2d_3d_annotations/vp' + Number_Video) #假如视频frame id 为100000,视频文件夹为5.只需要用frame id减去之前4个文件夹的总帧数,就得到了在5视频下需要提取图片的帧数。C:想从文件夹5中提取的第c帧图片 Int_Number = int(Number_Video) #Get Frame Id if Int_Number > 1: cap = cv2.VideoCapture(Path) cap.get(7) Frame_id = Frame_id - cap.get(7) Int_Number -=1 else: c = Int_Number
第三步:提取对应帧数图片
####Pick out Image from the Video relate to frame id --- c import cv2 import os cap = cv2.VideoCapture(Path) #Path of Video FPS = cap.get(5) #get Frame of Image c = 10000 #Frame ID start = 0 Videolong = cap.get(7) print('Total Frame ' + str(Videolong)) #Total Frame of the Video output =r'/home/tmac/Workingdisc/CVHCI/image/' #Path of Output/Image while (True): ret, frame = cap.read() if start <= c <= Videolong : Output_name = output + str(c) print("Getting Image: " + str(c)) print(frame.shape) cv2.imwrite(Output_name + '.jpg', frame) #cv2.imshow('Output_name',frame) #cv2.waitKey(0) break if c>Videolong: print('Out of limit') break cap.release()
第四步:提取某个特定视频的某个时间段的图片,每间隔100帧取一个图
######Pick out Image between st(starttime) until ed(endtime), import cv2 import os cap = cv2.VideoCapture(r"/home/tmac/Workingdisc/CVHCI/image/run1b.mp4") #Path of Video FPS = cap.get(5) c = 1 #time & Frame ID st = '20.15' #Starttime ed = '20.55' #Endtime start = int(st.split('.')[0]) * 60 + int(st.split('.')[1]) #Startframe end = int(ed.split('.')[0]) * 60 + int(ed.split('.')[1]) #Endframe #Time to Frame startframe = int(FPS) * start endframe = int(FPS) * end #Output Path output = r'/home/tmac/Workingdisc/CVHCI/image/'.format(st,ed) if not os.path.exists(output): os.makedirs(output) while (True): ret, frame = cap.read() if ret: if startframe<= c <= endframe and c%100==0: #get Images each 100 frame print("Clipping:" + str(c) + " th Frame") cv2.imwrite(output + str(c) + '.jpg', frame) # Save Image if c>endframe: print('Get all Images') break c += 1 else: print("No Video exists") break cap.release()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。