File size: 2,958 Bytes
fb5d697
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
##################
#Processing annotation for THUMOS'14 dataset
##################

import json
import pickle
import numpy as np
import argparse


def THUMOS_offset(args) :
    dataset_path = args.dataset
    mode = args.mode
    offset = args.offset

    with open(dataset_path, "r") as f:
        av_annotations = json.load(f)

    av_processed = {}

    for video in av_annotations['videos'] :
        for clip in video['clips'] :
            uid = clip['clip_uid']
            av_processed[uid] = {}

            # Make embedding template
            end_frame = clip['clip_end_frame']
            feature_num = int(np.round(end_frame/6))
            anno = np.zeros((feature_num , 3))

            i=0
            for transcript in clip['transcriptions']:

                # To make annotation by clip frame, should subtract video_start_frame from transcription's start_frame
                if i==0 :
                    initial_frame = clip['video_start_frame']
                    i+=1

                start_frame = transcript['video_start_frame'] - initial_frame
                end_frame = transcript['video_end_frame'] - initial_frame

                #Save action encoding num
                if int(transcript['person_id']) == 0 :
                    encode_num = 1
                if int(transcript['person_id']) >= 1 :
                    encode_num = 2
                if int(transcript['person_id']) == -1 :
                    encode_num = 0
                
                #Save encoding by feature
                start = start_frame //6 -1 #As index starts from 0
                end = end_frame //6
                if start <0 :
                    start = 0

                #Offset for wearer
                if encode_num == 1 :
                    start_ = start-offset
                    if start_<0 :
                        start_=0  
                    anno[start_ : start , encode_num]=1
                #For background , normal speaker
                else:
                    if end-start == 1 :
                        anno[start , encode_num]=1    
                    else :
                        anno[start:end , encode_num]=1

            #Save annotation to uid list
            av_processed[uid]['anno'] = anno
            av_processed[uid]['feature_length'] = feature_num
            av_processed[uid]['anno'][np.where(np.sum(av_processed[uid]['anno'], axis=1) == 0), 0] = 1

    print('Processing DONE!')

    with open(f'{mode}_offset_{offset}_test.pickle' , 'wb') as f :
        pickle.dump(av_processed , f)

    print('SAVED')







if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset', type = str, default = '/scratch/junhyeok/ego4d_dataset/v2/annotations/av_train.json')
    parser.add_argument('--mode', type = str, default = 'train')
    parser.add_argument('--offset', type = int, default = 5)
    args = parser.parse_args()

    THUMOS_offset(args)