File size: 3,731 Bytes
33569f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import sys
import numpy as np
import pandas as pd
import json
import os
from joblib import Parallel, delayed

import yaml


# with open("./config/anet.yaml", 'r', encoding='utf-8') as f:
#         tmp = f.read()
#         config = yaml.load(tmp, Loader=yaml.FullLoader)


# vid_path = config['dataset']['test']['visual_feature_path']
# nms_thresh = config['testing']['nms_thresh'] 



def load_json(file):
    with open(file) as json_file:
        data = json.load(json_file)
        return data


def get_infer_dict(vid_anno,subset='test'):
    # df = pd.read_csv(vid_info)
    database = load_json(vid_anno)
    video_dict = {}
    for i in range(len(database)):
        video_name = video_name = os.path.splitext(os.path.basename(database[i]['file']))[0]
        # if os.path.exists(os.path.join(vid_path+"/test",video_name+".npy")):
        video_info = database[i]
        video_new_info = {}
        video_new_info['duration_frame'] = video_info['video_frames']
        video_new_info['duration_second'] = video_info['duration']
        video_new_info["feature_frame"] = video_info['video_frames']
        video_subset = video_info["split"]
        video_anno = video_info['fake_periods']
        video_new_info['fake_periods'] = video_info['fake_periods']
        if len(video_anno) > 0:
            video_label = 'Fake'
            if video_subset == subset:
                    video_dict[video_name] = video_new_info
    return video_dict



def Soft_NMS(df, nms_threshold=1e-5, num_prop=100):
 
    df = df.sort_values(by="score", ascending=False)

    tstart = list(df.xmin.values[:])
    tend = list(df.xmax.values[:])
    tscore = list(df.score.values[:])
    tlabel = list(df.label.values[:])

    rstart = []
    rend = []
    rscore = []
    rlabel = []


    while len(tscore) > 1 and len(rscore) < num_prop and max(tscore)>0:
        max_index = tscore.index(max(tscore))
        for idx in range(0, len(tscore)):
            if idx != max_index:
                tmp_iou = IOU(tstart[max_index], tend[max_index], tstart[idx], tend[idx])
                if tmp_iou > 0:
                    tscore[idx] = tscore[idx] * (np.exp(-np.square(tmp_iou)*10) / nms_threshold)

        rstart.append(tstart[max_index])
        rend.append(tend[max_index])
        rscore.append(tscore[max_index])
        rlabel.append(tlabel[max_index])
        tstart.pop(max_index)
        tend.pop(max_index)
        tscore.pop(max_index)
        tlabel.pop(max_index)

    newDf = pd.DataFrame()
    newDf['score'] = rscore
    newDf['xmin'] = rstart
    newDf['xmax'] = rend
    newDf['label'] = rlabel

    return newDf



def IOU(s1, e1, s2, e2):
    if (s2 > e1) or (s1 > e2):
        return 0
    Aor = max(e1, e2) - min(s1, s2)
    Aand = min(e1, e2) - min(s1, s2)
    return float(Aand) / (Aor - Aand + (e2-s2))



def multithread_detection(video_name, video_cls, pred_prop):
    
    old_df = pred_prop[pred_prop.video_name == video_name]
    # print(df)
    
    df = pd.DataFrame()
    df['score'] = old_df.score.values[:]
    df['label'] = old_df.label.values[:]
    df['xmin'] = old_df.xmin.values[:]
    df['xmax'] = old_df.xmax.values[:]

    best_score = np.max(video_cls)

    # if len(df) > 1:
    #     df = Soft_NMS(df, nms_thresh)
    df = df.sort_values(by="score", ascending=False)
    proposal_list = []

    for j in range(min(100, len(df))):
            tmp_proposal = {}
            tmp_proposal["label"] = 'Fake'
            tmp_proposal["score"] = float(df.score.values[j])*best_score
            tmp_proposal["segment"] = [max(0, df.xmin.values[j]),
                                    min(1, df.xmax.values[j])]
            proposal_list.append(tmp_proposal)

    return {video_name: proposal_list}