<jupyter_start><jupyter_text>Load data<jupyter_code>import os, glob
import cv2
import numpy as np
import matplotlib.pyplot as plt
from multiprocessing import Pool
import functools
%matplotlib inline<jupyter_output><empty_output><jupyter_text>video2npy<jupyter_code>import skvideo.io
import skvideo.datasets
vid_root = '/data/dataset/UCF/'
vid_ls = glob.glob(vid_root+"v_BabyCrawling**.avi")
vid_ls.sort()
print(len(vid_ls))
print(vid_ls[:5])
videogen = skvideo.io.vreader(vid_ls[55])

for i, frame in enumerate(videogen):
    #print(i, frame.shape)
    if i == 4:
        print(type(frame))
        print(frame[:, 40:280, :].shape)
        plt.imshow(frame)
def get_frames_npy(vid_path, out_path, length = 7, skip = 2, pre = 1):    
    if not os.path.exists(out_path):
        os.mkdir(out_path)
    
    vid_name = os.path.basename(vid_path).split('.')[0]
    videogen = skvideo.io.vreader(vid_path)
    
    frames_lst = [cv2.resize(frame[:, 40:280, :], (256, 256)) 
                  for i, frame in enumerate(videogen) if i%skip == 0]
    
    n = 0
    
    for i in range(int(len(frames_lst)/(length*2))):
        A = np.asarray(frames_lst[i:i+length])
        B = np.asarray(frames_lst[i+length-pre:i+length*2-pre])
        v = np.asarray([A,B])
        v = np.transpose(v , (0,4,1,2,3))
        np.save('{}{}_{}.npy'.format(out_path, vid_name, i),v)

        n += 1
            
    return n<jupyter_output><empty_output><jupyter_text>超参数输出<jupyter_code>npy_path = '/data/dataset/ucf-npy/'
vid_root = '/data/dataset/UCF/'
vid_ls = glob.glob(vid_root+"v_BabyCrawling**.avi")
vid_ls.sort()
total_npy = 0

print('共有视频：{}'.format(len(vid_ls)))
## Hyperparameter
N = 76 # how many videos would be used
length = 7
skip = 2
pre = 1  
if N == '': N = len(vid_ls)
if N > len(vid_ls): raise ValueError("处理视频数大于视频总数")

with Pool(12) as p:
    p.map(functools.partial(get_frames_npy, 
                            out_path = npy_path, length = length, skip = skip, pre = 1), 
          vid_ls[:N])
    
print(len(os.listdir(npy_path)))<jupyter_output>398<jupyter_text>输出检验<jupyter_code>os.listdir(npy_path)
f_t = npy_path+os.listdir(npy_path)[3]
a= np.load(f_t)
plt.figure(1)
plt.subplot(121)
plt.imshow(np.transpose(a[0], (1,2,3,0))[0])

plt.subplot(122)
plt.imshow(np.transpose(a[1], (1,2,3,0))[0])
rm -rf /data/dataset/ucf-npy/<jupyter_output><empty_output><jupyter_text>images2npy<jupyter_code>def get_one_clip(lst,index ,skip ,length  ):
    return lst[index:index+length*skip:skip]

def get_clips(img_lst,skip  ,length):
    clips =[get_one_clip(img_lst,i,skip,length) for i in range(len(img_lst)) if len(get_one_clip(img_lst,i,skip,length)) == length ]
    
    return clips
def get_pair(img_lst,pre ,skip  ,length):
    A = get_clips(img_lst[:][:-pre],skip  ,length)
    B = get_clips(img_lst[pre:],skip  ,length)
    return A,B

read_ = lambda x : cv2.resize(cv2.imread(x)[:, int(1242/2-375/2):int(1242/2+375/2),::-1],(256,256))

def gen_np(c):
#     print(c)
    img1 = [ read_(i)  for i in c[0]]
    img2 = [ read_(i)  for i in c[1]]


    v = np.asarray([img1,img2])
    v = np.transpose(v , (0,4,1,2,3))
#     v.transpose(0,4,1,2,3)
    return v
def dump(img_lst,dirpath='data' ,start =0 ,skip = 2 ,length =7 ,pre =2):

    a,b = get_pair(img_lst,pre ,skip  ,length)
    task = [ (i,j) for i ,j in zip(a,b)]
    for i ,j in enumerate(task):
        v = gen_np(j)
        np.save('{}{}.npy'.format(dirpath,i+start),v)
#         return 
      
def f(data_path):
    start = 0
    img_lst = glob.glob(data_path+"**.png")
    img_lst.sort()
    dump(img_lst,'video/{}{}'.format(*data_path.split('/')[-3:-1]),start,2 ,7 ,2)
f_lst = glob.glob('/data/dataset/depthdata/vkitti_1.3.1_rgb/**/**/')
with Pool(12) as p:
    p.map(f,f_lst)
fi =  'video/000115-deg-left0.npy'
a= np.load(fi)
plt.imshow(np.transpose(a[0],(1,2,3,0))[2])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17854366416169992, 'fraction_numerical': 0.036537963202902306, 'mean_word_length': 3.7595561035758323, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '13269', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1669, 'n_words': 274, 'path': 'Video2Video.ipynb', 'repo_name': 'zhaoying9105/vid2vid-cn', 'size': '3859'}