<jupyter_start><jupyter_text>EDA number of labels<jupyter_code>video_labels = pd.read_csv('train_labels.csv').fillna(0)
video_labels.head()
video_labels_with_impact = video_labels[video_labels['impact'] > 0]
videos = video_labels.video.unique()
num = []
for video in videos[1:]:
    v = video_labels[(video_labels["impact"]==1) & (video_labels["video"]==video)]
    print("number of labels: {} in video {}".format(len(v), video))
    num.append(len(v))

print("mean:{} max:{} min:{}".format(np.mean(num), np.max(num), np.min(num)))<jupyter_output>number of labels: 18 in video 57583_000082_Sideline.mp4
number of labels: 16 in video 57584_000336_Endzone.mp4
number of labels: 16 in video 57584_000336_Sideline.mp4
number of labels: 20 in video 57584_002674_Endzone.mp4
number of labels: 23 in video 57584_002674_Sideline.mp4
number of labels: 19 in video 57586_000540_Endzone.mp4
number of labels: 21 in video 57586_000540_Sideline.mp4
number of labels: 28 in video 57586_001934_Endzone.mp4
number of labels: 27 in video 57586_001934_Sideline.mp4
number of labels: 10 in video 57586_004152_Endzone.mp4
number of labels: 11 in video 57586_004152_Sideline.mp4
number of labels: 19 in video 58106_002918_Endzone.mp4
number of labels: 19 in video 58106_002918_Sideline.mp4
number of labels: 20 in video 57594_000923_Endzone.mp4
number of labels: 18 in video 57594_000923_Sideline.mp4
number of labels: 26 in video 57596_002686_Endzone.mp4
number of labels: 24 in video 57596_002686_Sideline.mp4
number of labels: 22 in video 57597_000658_Endzone.mp4
n[...]<jupyter_text>Make labels with all labels<jupyter_code>video_labels = pd.read_csv('train_labels.csv').fillna(0)
# filter low-confident
video_labels_with_impact = video_labels[(video_labels['impact'] > 0)*(video_labels["confidence"]>1)*(video_labels["visibility"]>0)]
video_labels['image_name'] = video_labels['video'].str.replace('.mp4', '') + '_' + video_labels['frame'].astype(str).str.zfill(3) + '.png'
video_labels['impact'] = video_labels['impact'].astype(int)+1
video_labels['x'] = video_labels['left']
video_labels['y'] = video_labels['top']
video_labels['w'] = video_labels['width']
video_labels['h'] = video_labels['height']

video_labels.to_csv("train_labels_all.csv")
video_labels.head()<jupyter_output><empty_output><jupyter_text>Generate impact only labels<jupyter_code>video_labels = pd.read_csv('train_labels.csv').fillna(0)
video_labels_with_impact = video_labels[(video_labels['impact'] > 0)*(video_labels["confidence"]>1)*(video_labels["visibility"]>0)]
for row in tqdm(video_labels_with_impact[['video','frame','label']].values):
    frames = np.array([-4,-3,-2,-1,1,2,3,4])+row[1]
    video_labels.loc[(video_labels['video'] == row[0]) 
                                 & (video_labels['frame'].isin(frames))
                                 & (video_labels['label'] == row[2]), 'impact'] = 1
video_labels['image_name'] = video_labels['video'].str.replace('.mp4', '') + '_' + video_labels['frame'].astype(str).str.zfill(3) + '.png'
video_labels = video_labels[video_labels.groupby('image_name')['impact'].transform("sum") > 0].reset_index(drop=True)
video_labels['impact'] = video_labels['impact'].astype(int)+1
video_labels['x'] = video_labels['left']
video_labels['y'] = video_labels['top']
video_labels['w'] = video_labels['width']
video_labels['h'] = video_labels['height']
video_labels.head()
video_labels.to_csv("train_labels_4.csv")
video_labels = pd.read_csv('train_labels.csv').fillna(0)
video_labels_with_impact = video_labels[(video_labels['impact'] > 0)*(video_labels["confidence"]>1)*(video_labels["visibility"]>0)]
for row in tqdm(video_labels_with_impact[['video','frame','label']].values):
    frames = np.array([-3,-2,-1,1,2,3])+row[1]
    video_labels.loc[(video_labels['video'] == row[0]) 
                                 & (video_labels['frame'].isin(frames))
                                 & (video_labels['label'] == row[2]), 'impact'] = 1
video_labels['image_name'] = video_labels['video'].str.replace('.mp4', '') + '_' + video_labels['frame'].astype(str).str.zfill(3) + '.png'
video_labels = video_labels[video_labels.groupby('image_name')['impact'].transform("sum") > 0].reset_index(drop=True)
video_labels['impact'] = video_labels['impact'].astype(int)+1
video_labels['x'] = video_labels['left']
video_labels['y'] = video_labels['top']
video_labels['w'] = video_labels['width']
video_labels['h'] = video_labels['height']
video_labels.head()
video_labels.to_csv("train_labels_3.csv")
video_labels = pd.read_csv('train_labels.csv').fillna(0)
video_labels_with_impact = video_labels[(video_labels['impact'] > 0)*(video_labels["confidence"]>1)*(video_labels["visibility"]>0)]
for row in tqdm(video_labels_with_impact[['video','frame','label']].values):
    frames = np.array([-2,-1,1,2,])+row[1]
    video_labels.loc[(video_labels['video'] == row[0]) 
                                 & (video_labels['frame'].isin(frames))
                                 & (video_labels['label'] == row[2]), 'impact'] = 1
video_labels['image_name'] = video_labels['video'].str.replace('.mp4', '') + '_' + video_labels['frame'].astype(str).str.zfill(3) + '.png'
video_labels = video_labels[video_labels.groupby('image_name')['impact'].transform("sum") > 0].reset_index(drop=True)
video_labels['impact'] = video_labels['impact'].astype(int)+1
video_labels['x'] = video_labels['left']
video_labels['y'] = video_labels['top']
video_labels['w'] = video_labels['width']
video_labels['h'] = video_labels['height']
video_labels.head()
video_labels.to_csv("train_labels_2.csv")<jupyter_output><empty_output><jupyter_text>Generate images from video<jupyter_code>def mk_images(video_name, video_labels, video_dir, out_dir, only_with_impact=True):
    video_path=f"{video_dir}/{video_name}"
    video_name = os.path.basename(video_path)
    vidcap = cv2.VideoCapture(video_path)
    if only_with_impact:
        boxes_all = video_labels.query("video == @video_name")
        print(video_path, boxes_all[boxes_all.impact > 1.0].shape[0])
    else:
        print(video_path)
    frame = 0
    while True:
        it_worked, img = vidcap.read()
        if not it_worked:
            break
        frame += 1
        if only_with_impact:
            boxes = video_labels.query("video == @video_name and frame == @frame")
            boxes_with_impact = boxes[boxes.impact > 1.0]
            if boxes_with_impact.shape[0] == 0:
                continue
        img_name = f"{video_name}_frame{frame}"
        image_path = f'{out_dir}/{video_name}'.replace('.mp4',f'_{str(frame).zfill(3)}.png')
        _ = cv2.imwrite(image_path, img)
uniq_video = video_labels.video.unique()
video_dir = 'train'
out_dir = 'train_images'
!mkdir -p $out_dir
for video_name in uniq_video:
    mk_images(video_name, video_labels, video_dir, out_dir)
uniq_video = video_labels.video.unique()
video_dir = 'train'
out_dir = 'train_images'
!mkdir -p $out_dir
for video_name in uniq_video:
    mk_images(video_name, video_labels, video_dir, out_dir, False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16294293439590152, 'fraction_numerical': 0.04738864380247616, 'mean_word_length': 5.586691658856608, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '489401', 'license': 'MIT', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2415, 'n_words': 389, 'path': 'train-prepare-labels.ipynb', 'repo_name': 'kentaroy47/kaggle-nflimpact-13thplace', 'size': '7027'}