text_plain_output_1.png
import cv2

import numpy as np

import os

import pandas as pd

import pandas as pd

df = pd.read_csv('../input/drive-and-act/iccv_activities_3s/activities_3s/kinect_color/tasklevel.chunks_90.split_0.train.csv')

root_path = '../input/drive-and-act/kinect_color/kinect_color/'
sample_rate = 5
for j in range(1):
    file_names = []
    labels = []
    length = []
    for i in range(df.shape[0]):
        if i > 0:
            if root_path + df.iloc[i - 1, 1] + '.mp4' != root_path + df.iloc[i, 1] + '.mp4':
                path_video = root_path + df.iloc[i, 1] + '.mp4'
                cap = cv2.VideoCapture(path_video)
                count_frame = 0
        else:
            path_video = root_path + df.iloc[i, 1] + '.mp4'
            cap = cv2.VideoCapture(path_video)
            count_frame = 0
        frame_start = df.iloc[i, 3]
        frame_end = df.iloc[i, 4]
        label = df.iloc[i, 5]
        length.append(frame_end - frame_start)
        if frame_end - frame_start > 39:
            root_save = '/kaggle/working/Trainsplit_0/' + df.iloc[i, 1] + '/' + df.iloc[i, 1].split('/')[-1] + '-' + str(frame_start) + '-' + str(frame_end - frame_start)
            file_names.append(root_save)
            labels.append(label)
            lenn = 0
            while True:
                ret, frame = cap.read()
                if count_frame >= frame_start and count_frame <= frame_end and (ret == True) and ((count_frame - frame_start) % sample_rate == 0) and (lenn < 9):
                    lenn += 1
                    frame = cv2.resize(frame, (256, 256))
                    if not os.path.exists(root_save):
                        os.makedirs(root_save)
                    save_path = df.iloc[i, 1].split('/')[-1] + str(count_frame) + '.jpg'
                    final_path = root_save + '/' + save_path
                    cv2.imwrite(final_path, frame)
                count_frame += 1
                if count_frame >= frame_end or ret == False:
                    break
    np.save('/kaggle/working/Trainsplit_0_file_names.npy', file_names)
    np.save('/kaggle/working/Trainsplit_0_labels.npy', labels)

np.unique(length, return_counts=True)