def generate_classes(meta_dir, split, use_ambiguous=False):

# R-C3D
# Written by Huijuan Xu

import sys, os, errno
import numpy as np
import csv
import json
import copy
import argparse
import subprocess

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FRAME_DIR = '/media/F/THUMOS14'
META_DIR = os.path.join(FRAME_DIR, 'annotation_')

def nms(dets, thresh=0.4):
    """Pure Python NMS baseline."""
    if len(dets) == 0: return []
    x1 = dets[:, 0]
    x2 = dets[:, 1]
    scores = dets[:, 2]
    lengths = x2 - x1
    order = scores.argsort()[::-1]
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        inter = np.maximum(0.0, xx2 - xx1)
        ovr = inter / (lengths[i] + lengths[order[1:]] - inter)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep

class_id = {0: 'Background'}
    with open(os.path.join(meta_dir, 'detclasslist.txt'), 'r') as f:
        lines = f.readlines()
        for l in lines:
            cname = l.strip().split()[-1]
            cid = int(l.strip().split()[0])
            class_id[cid] = cname
        if use_ambiguous:
            class_id[21] = 'Ambiguous'

    return class_id
'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
      # video name and sliding window length
      if "fg_name :" in l:
         vid = l.split('/')[-1]

      # frame index, time, confident score
      elif "frames :" in l:
         start_frame=int(l.split()[4])
         end_frame=int(l.split()[5])
         stride = int(l.split()[6].split(']')[0])

      elif "activity:" in l:
         label = int(l.split()[1])
         tmp['label'] = label
         find_next = True

      elif "im_detect" in l:
         return vid, segments

      elif find_next:
         try:
           left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
           right_frame = float(l.split()[1])*stride + start_frame
         except:
           left_frame = float(l.split()[1])*stride + start_frame
           right_frame = float(l.split()[2])*stride + start_frame
         if (left_frame < end_frame) and (right_frame <= end_frame):
           left  = left_frame / 25.0
           right = right_frame / 25.0
           try:
             score = float(l.split()[-1].split(']')[0])
           except:
             score = float(l.split()[-2])
           if score > thresh:
             tmp1 = copy.deepcopy(tmp)
             tmp1['score'] = score
             tmp1['segment'] = [left, right]
             segments.append(tmp1)
         elif (left_frame < end_frame) and (right_frame > end_frame):
             if (end_frame-left_frame)*1.0/(right_frame-left_frame)>=0:
                 right_frame = end_frame
                 left  = left_frame / 25.0
                 right = right_frame / 25.0
                 try:
                   score = float(l.split()[-1].split(']')[0])
                 except:
                   score = float(l.split()[-2])
                 if score > thresh:
                     tmp1 = copy.deepcopy(tmp)
                     tmp1['score'] = score
                     tmp1['segment'] = [left, right]
                     segments.append(tmp1)

'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
        # video name and sliding window length
        if "fg_name:" in l:
            vid = l.split('/')[-1]

        # frame index, time, confident score
        elif "frames:" in l:
            start_frame=int(l.split()[3])
            end_frame=int(l.split()[4])
            stride = int(l.split()[5].split(']')[0])

        elif "activity:" in l:
            label = int(l.split()[1])
            tmp['label'] = label
            find_next = True

        elif "im_detect" in l:
            return vid, segments

        elif find_next:
            try:
                left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
                right_frame = float(l.split()[1])*stride + start_frame
            except:
                left_frame = float(l.split()[1])*stride + start_frame
                right_frame = float(l.split()[2])*stride + start_frame

            try:
                score = float(l.split()[-1].split(']')[0])
            except:
                score = float(l.split()[-2])

            if (left_frame >= right_frame):
                print("???", l)
                continue

            if right_frame > end_frame:
                right_frame = end_frame

            left  = left_frame / framerate
            right = right_frame / framerate
            if score > thresh:
                tmp1 = copy.deepcopy(tmp)
                tmp1['score'] = score
                tmp1['segment'] = [left, right]
                segments.append(tmp1)

def analysis_log(logfile, thresh, framerate):
    with open(logfile, 'r') as f:
        lines = f.read().splitlines()
    predict_data = []
    res = {}
    for l in lines:
        if "frames:" in l:
            predict_data = []
        predict_data.append(l)
        if "im_detect:" in l:
            vid, segments = get_segments(predict_data, thresh, framerate)
            if vid not in res:
                res[vid] = []
            res[vid] += segments
    return res

def select_top(segmentations, nms_thresh=0.99999, num_cls=0, topk=0):
  res = {}
  for vid, vinfo in segmentations.items():
    # select most likely classes
    if num_cls > 0:
      ave_scores = np.zeros(21)
      for i in xrange(1, 21):
        ave_scores[i] = np.sum([d['score'] for d in vinfo if d['label']==i])
      labels = list(ave_scores.argsort()[::-1][:num_cls])
    else:
      labels = list(set([d['label'] for d in vinfo]))

    # NMS
    res_nms = []
    for lab in labels:
      nms_in = [d['segment'] + [d['score']] for d in vinfo if d['label'] == lab]
      keep = nms(np.array(nms_in), nms_thresh)
      for i in keep:
        tmp = {'label': lab, 'score':nms_in[i][2], 'segment': nms_in[i][0:2]}
        res_nms.append(tmp)

    # select topk
    scores = [d['score'] for d in res_nms]
    sortid = np.argsort(scores)[-topk:]
    res[vid] = [res_nms[id] for id in sortid]
  return res

parser = argparse.ArgumentParser(description="log analysis.py")
parser.add_argument('log_file', type=str, help="test log file path")
parser.add_argument('--framerate', type=int, help="frame rate of videos extract by ffmpeg")
parser.add_argument('--thresh', type=float, default=0.005, help="filter those dets low than the thresh, default=0.0005")
parser.add_argument('--nms_thresh', type=float, default=0.4, help="nms thresh, default=0.3")
parser.add_argument('--topk', type=int, default=200, help="select topk dets, default=200")
parser.add_argument('--num_cls', type=int, default=0, help="select most likely classes, default=0")

args = parser.parse_args()
classes = generate_classes(META_DIR+'test', 'test', use_ambiguous=False)
segmentations = analysis_log(args.log_file, thresh = args.thresh, framerate=args.framerate)
segmentations = select_top(segmentations, nms_thresh=args.nms_thresh, num_cls=args.num_cls, topk=args.topk)

res = {'version': 'VERSION 1.3',
       'external_data': {'used': True, 'details': 'C3D pre-trained on activity-1.3 training set'},
       'results': {}}
for vid, vinfo in segmentations.items():
  res['results'][vid] = vinfo

with open('tmp.txt', 'w') as outfile:
  for vid, vinfo in segmentations.items():
    for seg in vinfo:
      outfile.write("{} {} {} {} {}\n".format(vid, seg['segment'][0], seg['segment'][1], int(seg['label']) ,seg['score']))

def matlab_eval():
    print('Computing results with the official Matlab eval code')
    path = os.path.join(THIS_DIR, 'Evaluation')
    cmd = 'cp tmp.txt {} && '.format(THIS_DIR)
    cmd += 'cd {} && '.format(path)
    cmd += 'matlab -nodisplay -nodesktop '
    cmd += '-r "dbstop if error; '
    cmd += 'eval_thumos14(); quit;"'

    print('Runing: \n {}'.format(cmd))
    status = subprocess.call(cmd, shell=True)

matlab_eval()


with open(logfile, 'r') as f:
        lines = f.read().splitlines()
    predict_data = []
    res = {}
    for l in lines:
        if "frames:" in l:
            predict_data = []
        predict_data.append(l)
        if "im_detect:" in l:
            vid, segments = get_segments(predict_data, thresh, framerate)
            if vid not in res:
                res[vid] = []
            res[vid] += segments
    return res

# R-C3D
# Written by Huijuan Xu

import sys, os, errno
import numpy as np
import csv
import json
import copy
import argparse
import subprocess

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FRAME_DIR = '/media/F/THUMOS14'
META_DIR = os.path.join(FRAME_DIR, 'annotation_')

def nms(dets, thresh=0.4):
    """Pure Python NMS baseline."""
    if len(dets) == 0: return []
    x1 = dets[:, 0]
    x2 = dets[:, 1]
    scores = dets[:, 2]
    lengths = x2 - x1
    order = scores.argsort()[::-1]
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        inter = np.maximum(0.0, xx2 - xx1)
        ovr = inter / (lengths[i] + lengths[order[1:]] - inter)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep

def generate_classes(meta_dir, split, use_ambiguous=False):
    class_id = {0: 'Background'}
    with open(os.path.join(meta_dir, 'detclasslist.txt'), 'r') as f:
        lines = f.readlines()
        for l in lines:
            cname = l.strip().split()[-1]
            cid = int(l.strip().split()[0])
            class_id[cid] = cname
        if use_ambiguous:
            class_id[21] = 'Ambiguous'

    return class_id
'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
      # video name and sliding window length
      if "fg_name :" in l:
         vid = l.split('/')[-1]

      # frame index, time, confident score
      elif "frames :" in l:
         start_frame=int(l.split()[4])
         end_frame=int(l.split()[5])
         stride = int(l.split()[6].split(']')[0])

      elif "activity:" in l:
         label = int(l.split()[1])
         tmp['label'] = label
         find_next = True

      elif "im_detect" in l:
         return vid, segments

      elif find_next:
         try:
           left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
           right_frame = float(l.split()[1])*stride + start_frame
         except:
           left_frame = float(l.split()[1])*stride + start_frame
           right_frame = float(l.split()[2])*stride + start_frame
         if (left_frame < end_frame) and (right_frame <= end_frame):
           left  = left_frame / 25.0
           right = right_frame / 25.0
           try:
             score = float(l.split()[-1].split(']')[0])
           except:
             score = float(l.split()[-2])
           if score > thresh:
             tmp1 = copy.deepcopy(tmp)
             tmp1['score'] = score
             tmp1['segment'] = [left, right]
             segments.append(tmp1)
         elif (left_frame < end_frame) and (right_frame > end_frame):
             if (end_frame-left_frame)*1.0/(right_frame-left_frame)>=0:
                 right_frame = end_frame
                 left  = left_frame / 25.0
                 right = right_frame / 25.0
                 try:
                   score = float(l.split()[-1].split(']')[0])
                 except:
                   score = float(l.split()[-2])
                 if score > thresh:
                     tmp1 = copy.deepcopy(tmp)
                     tmp1['score'] = score
                     tmp1['segment'] = [left, right]
                     segments.append(tmp1)

'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
        # video name and sliding window length
        if "fg_name:" in l:
            vid = l.split('/')[-1]

        # frame index, time, confident score
        elif "frames:" in l:
            start_frame=int(l.split()[3])
            end_frame=int(l.split()[4])
            stride = int(l.split()[5].split(']')[0])

        elif "activity:" in l:
            label = int(l.split()[1])
            tmp['label'] = label
            find_next = True

        elif "im_detect" in l:
            return vid, segments

        elif find_next:
            try:
                left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
                right_frame = float(l.split()[1])*stride + start_frame
            except:
                left_frame = float(l.split()[1])*stride + start_frame
                right_frame = float(l.split()[2])*stride + start_frame

            try:
                score = float(l.split()[-1].split(']')[0])
            except:
                score = float(l.split()[-2])

            if (left_frame >= right_frame):
                print("???", l)
                continue

            if right_frame > end_frame:
                right_frame = end_frame

            left  = left_frame / framerate
            right = right_frame / framerate
            if score > thresh:
                tmp1 = copy.deepcopy(tmp)
                tmp1['score'] = score
                tmp1['segment'] = [left, right]
                segments.append(tmp1)

def analysis_log(logfile, thresh, framerate):

def select_top(segmentations, nms_thresh=0.99999, num_cls=0, topk=0):
  res = {}
  for vid, vinfo in segmentations.items():
    # select most likely classes
    if num_cls > 0:
      ave_scores = np.zeros(21)
      for i in xrange(1, 21):
        ave_scores[i] = np.sum([d['score'] for d in vinfo if d['label']==i])
      labels = list(ave_scores.argsort()[::-1][:num_cls])
    else:
      labels = list(set([d['label'] for d in vinfo]))

    # NMS
    res_nms = []
    for lab in labels:
      nms_in = [d['segment'] + [d['score']] for d in vinfo if d['label'] == lab]
      keep = nms(np.array(nms_in), nms_thresh)
      for i in keep:
        tmp = {'label': lab, 'score':nms_in[i][2], 'segment': nms_in[i][0:2]}
        res_nms.append(tmp)

    # select topk
    scores = [d['score'] for d in res_nms]
    sortid = np.argsort(scores)[-topk:]
    res[vid] = [res_nms[id] for id in sortid]
  return res

parser = argparse.ArgumentParser(description="log analysis.py")
parser.add_argument('log_file', type=str, help="test log file path")
parser.add_argument('--framerate', type=int, help="frame rate of videos extract by ffmpeg")
parser.add_argument('--thresh', type=float, default=0.005, help="filter those dets low than the thresh, default=0.0005")
parser.add_argument('--nms_thresh', type=float, default=0.4, help="nms thresh, default=0.3")
parser.add_argument('--topk', type=int, default=200, help="select topk dets, default=200")
parser.add_argument('--num_cls', type=int, default=0, help="select most likely classes, default=0")

args = parser.parse_args()
classes = generate_classes(META_DIR+'test', 'test', use_ambiguous=False)
segmentations = analysis_log(args.log_file, thresh = args.thresh, framerate=args.framerate)
segmentations = select_top(segmentations, nms_thresh=args.nms_thresh, num_cls=args.num_cls, topk=args.topk)

res = {'version': 'VERSION 1.3',
       'external_data': {'used': True, 'details': 'C3D pre-trained on activity-1.3 training set'},
       'results': {}}
for vid, vinfo in segmentations.items():
  res['results'][vid] = vinfo

with open('tmp.txt', 'w') as outfile:
  for vid, vinfo in segmentations.items():
    for seg in vinfo:
      outfile.write("{} {} {} {} {}\n".format(vid, seg['segment'][0], seg['segment'][1], int(seg['label']) ,seg['score']))

def matlab_eval():
    print('Computing results with the official Matlab eval code')
    path = os.path.join(THIS_DIR, 'Evaluation')
    cmd = 'cp tmp.txt {} && '.format(THIS_DIR)
    cmd += 'cd {} && '.format(path)
    cmd += 'matlab -nodisplay -nodesktop '
    cmd += '-r "dbstop if error; '
    cmd += 'eval_thumos14(); quit;"'

    print('Runing: \n {}'.format(cmd))
    status = subprocess.call(cmd, shell=True)

matlab_eval()


# R-C3D
# Written by Huijuan Xu

import sys, os, errno
import numpy as np
import csv
import json
import copy
import argparse
import subprocess

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FRAME_DIR = '/media/F/THUMOS14'
META_DIR = os.path.join(FRAME_DIR, 'annotation_')

def nms(dets, thresh=0.4):
    """Pure Python NMS baseline."""
    if len(dets) == 0: return []
    x1 = dets[:, 0]
    x2 = dets[:, 1]
    scores = dets[:, 2]
    lengths = x2 - x1
    order = scores.argsort()[::-1]
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        inter = np.maximum(0.0, xx2 - xx1)
        ovr = inter / (lengths[i] + lengths[order[1:]] - inter)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep

def generate_classes(meta_dir, split, use_ambiguous=False):
    class_id = {0: 'Background'}
    with open(os.path.join(meta_dir, 'detclasslist.txt'), 'r') as f:
        lines = f.readlines()
        for l in lines:
            cname = l.strip().split()[-1]
            cid = int(l.strip().split()[0])
            class_id[cid] = cname
        if use_ambiguous:
            class_id[21] = 'Ambiguous'

    return class_id
'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
      # video name and sliding window length
      if "fg_name :" in l:
         vid = l.split('/')[-1]

      # frame index, time, confident score
      elif "frames :" in l:
         start_frame=int(l.split()[4])
         end_frame=int(l.split()[5])
         stride = int(l.split()[6].split(']')[0])

      elif "activity:" in l:
         label = int(l.split()[1])
         tmp['label'] = label
         find_next = True

      elif "im_detect" in l:
         return vid, segments

      elif find_next:
         try:
           left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
           right_frame = float(l.split()[1])*stride + start_frame
         except:
           left_frame = float(l.split()[1])*stride + start_frame
           right_frame = float(l.split()[2])*stride + start_frame
         if (left_frame < end_frame) and (right_frame <= end_frame):
           left  = left_frame / 25.0
           right = right_frame / 25.0
           try:
             score = float(l.split()[-1].split(']')[0])
           except:
             score = float(l.split()[-2])
           if score > thresh:
             tmp1 = copy.deepcopy(tmp)
             tmp1['score'] = score
             tmp1['segment'] = [left, right]
             segments.append(tmp1)
         elif (left_frame < end_frame) and (right_frame > end_frame):
             if (end_frame-left_frame)*1.0/(right_frame-left_frame)>=0:
                 right_frame = end_frame
                 left  = left_frame / 25.0
                 right = right_frame / 25.0
                 try:
                   score = float(l.split()[-1].split(']')[0])
                 except:
                   score = float(l.split()[-2])
                 if score > thresh:
                     tmp1 = copy.deepcopy(tmp)
                     tmp1['score'] = score
                     tmp1['segment'] = [left, right]
                     segments.append(tmp1)

'''
def get_segments(data, thresh, framerate):

    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
        # video name and sliding window length
        if "fg_name:" in l:
            vid = l.split('/')[-1]

        # frame index, time, confident score
        elif "frames:" in l:
            start_frame=int(l.split()[3])
            end_frame=int(l.split()[4])
            stride = int(l.split()[5].split(']')[0])

        elif "activity:" in l:
            label = int(l.split()[1])
            tmp['label'] = label
            find_next = True

        elif "im_detect" in l:
            return vid, segments

        elif find_next:
            try:
                left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
                right_frame = float(l.split()[1])*stride + start_frame
            except:
                left_frame = float(l.split()[1])*stride + start_frame
                right_frame = float(l.split()[2])*stride + start_frame

            try:
                score = float(l.split()[-1].split(']')[0])
            except:
                score = float(l.split()[-2])

            if (left_frame >= right_frame):
                print("???", l)
                continue

            if right_frame > end_frame:
                right_frame = end_frame

            left  = left_frame / framerate
            right = right_frame / framerate
            if score > thresh:
                tmp1 = copy.deepcopy(tmp)
                tmp1['score'] = score
                tmp1['segment'] = [left, right]
                segments.append(tmp1)

def analysis_log(logfile, thresh, framerate):
    with open(logfile, 'r') as f:
        lines = f.read().splitlines()
    predict_data = []
    res = {}
    for l in lines:
        if "frames:" in l:
            predict_data = []
        predict_data.append(l)
        if "im_detect:" in l:
            vid, segments = get_segments(predict_data, thresh, framerate)
            if vid not in res:
                res[vid] = []
            res[vid] += segments
    return res

def select_top(segmentations, nms_thresh=0.99999, num_cls=0, topk=0):
  res = {}
  for vid, vinfo in segmentations.items():
    # select most likely classes
    if num_cls > 0:
      ave_scores = np.zeros(21)
      for i in xrange(1, 21):
        ave_scores[i] = np.sum([d['score'] for d in vinfo if d['label']==i])
      labels = list(ave_scores.argsort()[::-1][:num_cls])
    else:
      labels = list(set([d['label'] for d in vinfo]))

    # NMS
    res_nms = []
    for lab in labels:
      nms_in = [d['segment'] + [d['score']] for d in vinfo if d['label'] == lab]
      keep = nms(np.array(nms_in), nms_thresh)
      for i in keep:
        tmp = {'label': lab, 'score':nms_in[i][2], 'segment': nms_in[i][0:2]}
        res_nms.append(tmp)

    # select topk
    scores = [d['score'] for d in res_nms]
    sortid = np.argsort(scores)[-topk:]
    res[vid] = [res_nms[id] for id in sortid]
  return res

parser = argparse.ArgumentParser(description="log analysis.py")
parser.add_argument('log_file', type=str, help="test log file path")
parser.add_argument('--framerate', type=int, help="frame rate of videos extract by ffmpeg")
parser.add_argument('--thresh', type=float, default=0.005, help="filter those dets low than the thresh, default=0.0005")
parser.add_argument('--nms_thresh', type=float, default=0.4, help="nms thresh, default=0.3")
parser.add_argument('--topk', type=int, default=200, help="select topk dets, default=200")
parser.add_argument('--num_cls', type=int, default=0, help="select most likely classes, default=0")

args = parser.parse_args()
classes = generate_classes(META_DIR+'test', 'test', use_ambiguous=False)
segmentations = analysis_log(args.log_file, thresh = args.thresh, framerate=args.framerate)
segmentations = select_top(segmentations, nms_thresh=args.nms_thresh, num_cls=args.num_cls, topk=args.topk)

res = {'version': 'VERSION 1.3',
       'external_data': {'used': True, 'details': 'C3D pre-trained on activity-1.3 training set'},
       'results': {}}
for vid, vinfo in segmentations.items():
  res['results'][vid] = vinfo

with open('tmp.txt', 'w') as outfile:
  for vid, vinfo in segmentations.items():
    for seg in vinfo:
      outfile.write("{} {} {} {} {}\n".format(vid, seg['segment'][0], seg['segment'][1], int(seg['label']) ,seg['score']))

def matlab_eval():
    print('Computing results with the official Matlab eval code')
    path = os.path.join(THIS_DIR, 'Evaluation')
    cmd = 'cp tmp.txt {} && '.format(THIS_DIR)
    cmd += 'cd {} && '.format(path)
    cmd += 'matlab -nodisplay -nodesktop '
    cmd += '-r "dbstop if error; '
    cmd += 'eval_thumos14(); quit;"'

    print('Runing: \n {}'.format(cmd))
    status = subprocess.call(cmd, shell=True)

matlab_eval()


# R-C3D
# Written by Huijuan Xu

import sys, os, errno
import numpy as np
import csv
import json
import copy
import argparse
import subprocess

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FRAME_DIR = '/media/F/THUMOS14'
META_DIR = os.path.join(FRAME_DIR, 'annotation_')

def nms(dets, thresh=0.4):
    """Pure Python NMS baseline."""
    if len(dets) == 0: return []
    x1 = dets[:, 0]
    x2 = dets[:, 1]
    scores = dets[:, 2]
    lengths = x2 - x1
    order = scores.argsort()[::-1]
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        inter = np.maximum(0.0, xx2 - xx1)
        ovr = inter / (lengths[i] + lengths[order[1:]] - inter)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep

def generate_classes(meta_dir, split, use_ambiguous=False):
    class_id = {0: 'Background'}
    with open(os.path.join(meta_dir, 'detclasslist.txt'), 'r') as f:
        lines = f.readlines()
        for l in lines:
            cname = l.strip().split()[-1]
            cid = int(l.strip().split()[0])
            class_id[cid] = cname
        if use_ambiguous:
            class_id[21] = 'Ambiguous'

    return class_id
'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
      # video name and sliding window length
      if "fg_name :" in l:
         vid = l.split('/')[-1]

      # frame index, time, confident score
      elif "frames :" in l:
         start_frame=int(l.split()[4])
         end_frame=int(l.split()[5])
         stride = int(l.split()[6].split(']')[0])

      elif "activity:" in l:
         label = int(l.split()[1])
         tmp['label'] = label
         find_next = True

      elif "im_detect" in l:
         return vid, segments

      elif find_next:
         try:
           left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
           right_frame = float(l.split()[1])*stride + start_frame
         except:
           left_frame = float(l.split()[1])*stride + start_frame
           right_frame = float(l.split()[2])*stride + start_frame
         if (left_frame < end_frame) and (right_frame <= end_frame):
           left  = left_frame / 25.0
           right = right_frame / 25.0
           try:
             score = float(l.split()[-1].split(']')[0])
           except:
             score = float(l.split()[-2])
           if score > thresh:
             tmp1 = copy.deepcopy(tmp)
             tmp1['score'] = score
             tmp1['segment'] = [left, right]
             segments.append(tmp1)
         elif (left_frame < end_frame) and (right_frame > end_frame):
             if (end_frame-left_frame)*1.0/(right_frame-left_frame)>=0:
                 right_frame = end_frame
                 left  = left_frame / 25.0
                 right = right_frame / 25.0
                 try:
                   score = float(l.split()[-1].split(']')[0])
                 except:
                   score = float(l.split()[-2])
                 if score > thresh:
                     tmp1 = copy.deepcopy(tmp)
                     tmp1['score'] = score
                     tmp1['segment'] = [left, right]
                     segments.append(tmp1)

'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'

    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
        # video name and sliding window length
        if "fg_name:" in l:
            vid = l.split('/')[-1]

        # frame index, time, confident score
        elif "frames:" in l:
            start_frame=int(l.split()[3])
            end_frame=int(l.split()[4])
            stride = int(l.split()[5].split(']')[0])

        elif "activity:" in l:
            label = int(l.split()[1])
            tmp['label'] = label
            find_next = True

        elif "im_detect" in l:
            return vid, segments

        elif find_next:
            try:
                left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
                right_frame = float(l.split()[1])*stride + start_frame
            except:
                left_frame = float(l.split()[1])*stride + start_frame
                right_frame = float(l.split()[2])*stride + start_frame

            try:
                score = float(l.split()[-1].split(']')[0])
            except:
                score = float(l.split()[-2])

            if (left_frame >= right_frame):
                print("???", l)
                continue

            if right_frame > end_frame:
                right_frame = end_frame

            left  = left_frame / framerate
            right = right_frame / framerate
            if score > thresh:
                tmp1 = copy.deepcopy(tmp)
                tmp1['score'] = score
                tmp1['segment'] = [left, right]
                segments.append(tmp1)

def analysis_log(logfile, thresh, framerate):
    with open(logfile, 'r') as f:
        lines = f.read().splitlines()
    predict_data = []
    res = {}
    for l in lines:
        if "frames:" in l:
            predict_data = []
        predict_data.append(l)
        if "im_detect:" in l:
            vid, segments = get_segments(predict_data, thresh, framerate)
            if vid not in res:
                res[vid] = []
            res[vid] += segments
    return res

def select_top(segmentations, nms_thresh=0.99999, num_cls=0, topk=0):
  res = {}
  for vid, vinfo in segmentations.items():
    # select most likely classes
    if num_cls > 0:
      ave_scores = np.zeros(21)
      for i in xrange(1, 21):
        ave_scores[i] = np.sum([d['score'] for d in vinfo if d['label']==i])
      labels = list(ave_scores.argsort()[::-1][:num_cls])
    else:
      labels = list(set([d['label'] for d in vinfo]))

    # NMS
    res_nms = []
    for lab in labels:
      nms_in = [d['segment'] + [d['score']] for d in vinfo if d['label'] == lab]
      keep = nms(np.array(nms_in), nms_thresh)
      for i in keep:
        tmp = {'label': lab, 'score':nms_in[i][2], 'segment': nms_in[i][0:2]}
        res_nms.append(tmp)

    # select topk
    scores = [d['score'] for d in res_nms]
    sortid = np.argsort(scores)[-topk:]
    res[vid] = [res_nms[id] for id in sortid]
  return res

parser = argparse.ArgumentParser(description="log analysis.py")
parser.add_argument('log_file', type=str, help="test log file path")
parser.add_argument('--framerate', type=int, help="frame rate of videos extract by ffmpeg")
parser.add_argument('--thresh', type=float, default=0.005, help="filter those dets low than the thresh, default=0.0005")
parser.add_argument('--nms_thresh', type=float, default=0.4, help="nms thresh, default=0.3")
parser.add_argument('--topk', type=int, default=200, help="select topk dets, default=200")
parser.add_argument('--num_cls', type=int, default=0, help="select most likely classes, default=0")

args = parser.parse_args()
classes = generate_classes(META_DIR+'test', 'test', use_ambiguous=False)
segmentations = analysis_log(args.log_file, thresh = args.thresh, framerate=args.framerate)
segmentations = select_top(segmentations, nms_thresh=args.nms_thresh, num_cls=args.num_cls, topk=args.topk)

res = {'version': 'VERSION 1.3',
       'external_data': {'used': True, 'details': 'C3D pre-trained on activity-1.3 training set'},
       'results': {}}
for vid, vinfo in segmentations.items():
  res['results'][vid] = vinfo

with open('tmp.txt', 'w') as outfile:
  for vid, vinfo in segmentations.items():
    for seg in vinfo:
      outfile.write("{} {} {} {} {}\n".format(vid, seg['segment'][0], seg['segment'][1], int(seg['label']) ,seg['score']))

def matlab_eval():
    print('Computing results with the official Matlab eval code')
    path = os.path.join(THIS_DIR, 'Evaluation')
    cmd = 'cp tmp.txt {} && '.format(THIS_DIR)
    cmd += 'cd {} && '.format(path)
    cmd += 'matlab -nodisplay -nodesktop '
    cmd += '-r "dbstop if error; '
    cmd += 'eval_thumos14(); quit;"'

    print('Runing: \n {}'.format(cmd))
    status = subprocess.call(cmd, shell=True)

matlab_eval()


tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
        # video name and sliding window length
        if "fg_name:" in l:
            vid = l.split('/')[-1]

        # frame index, time, confident score
        elif "frames:" in l:
            start_frame=int(l.split()[3])
            end_frame=int(l.split()[4])
            stride = int(l.split()[5].split(']')[0])

        elif "activity:" in l:
            label = int(l.split()[1])
            tmp['label'] = label
            find_next = True

        elif "im_detect" in l:
            return vid, segments

        elif find_next:
            try:
                left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
                right_frame = float(l.split()[1])*stride + start_frame
            except:
                left_frame = float(l.split()[1])*stride + start_frame
                right_frame = float(l.split()[2])*stride + start_frame

            try:
                score = float(l.split()[-1].split(']')[0])
            except:
                score = float(l.split()[-2])

            if (left_frame >= right_frame):
                print("???", l)
                continue

            if right_frame > end_frame:
                right_frame = end_frame

            left  = left_frame / framerate
            right = right_frame / framerate
            if score > thresh:
                tmp1 = copy.deepcopy(tmp)
                tmp1['score'] = score
                tmp1['segment'] = [left, right]
                segments.append(tmp1)

# R-C3D
# Written by Huijuan Xu

import sys, os, errno
import numpy as np
import csv
import json
import copy
import argparse
import subprocess

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FRAME_DIR = '/media/F/THUMOS14'
META_DIR = os.path.join(FRAME_DIR, 'annotation_')

def nms(dets, thresh=0.4):
    """Pure Python NMS baseline."""
    if len(dets) == 0: return []
    x1 = dets[:, 0]
    x2 = dets[:, 1]
    scores = dets[:, 2]
    lengths = x2 - x1
    order = scores.argsort()[::-1]
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        inter = np.maximum(0.0, xx2 - xx1)
        ovr = inter / (lengths[i] + lengths[order[1:]] - inter)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep

def generate_classes(meta_dir, split, use_ambiguous=False):
    class_id = {0: 'Background'}
    with open(os.path.join(meta_dir, 'detclasslist.txt'), 'r') as f:
        lines = f.readlines()
        for l in lines:
            cname = l.strip().split()[-1]
            cid = int(l.strip().split()[0])
            class_id[cid] = cname
        if use_ambiguous:
            class_id[21] = 'Ambiguous'

    return class_id
'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
      # video name and sliding window length
      if "fg_name :" in l:
         vid = l.split('/')[-1]

      # frame index, time, confident score
      elif "frames :" in l:
         start_frame=int(l.split()[4])
         end_frame=int(l.split()[5])
         stride = int(l.split()[6].split(']')[0])

      elif "activity:" in l:
         label = int(l.split()[1])
         tmp['label'] = label
         find_next = True

      elif "im_detect" in l:
         return vid, segments

      elif find_next:
         try:
           left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
           right_frame = float(l.split()[1])*stride + start_frame
         except:
           left_frame = float(l.split()[1])*stride + start_frame
           right_frame = float(l.split()[2])*stride + start_frame
         if (left_frame < end_frame) and (right_frame <= end_frame):
           left  = left_frame / 25.0
           right = right_frame / 25.0
           try:
             score = float(l.split()[-1].split(']')[0])
           except:
             score = float(l.split()[-2])
           if score > thresh:
             tmp1 = copy.deepcopy(tmp)
             tmp1['score'] = score
             tmp1['segment'] = [left, right]
             segments.append(tmp1)
         elif (left_frame < end_frame) and (right_frame > end_frame):
             if (end_frame-left_frame)*1.0/(right_frame-left_frame)>=0:
                 right_frame = end_frame
                 left  = left_frame / 25.0
                 right = right_frame / 25.0
                 try:
                   score = float(l.split()[-1].split(']')[0])
                 except:
                   score = float(l.split()[-2])
                 if score > thresh:
                     tmp1 = copy.deepcopy(tmp)
                     tmp1['score'] = score
                     tmp1['segment'] = [left, right]
                     segments.append(tmp1)

'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False

def analysis_log(logfile, thresh, framerate):
    with open(logfile, 'r') as f:
        lines = f.read().splitlines()
    predict_data = []
    res = {}
    for l in lines:
        if "frames:" in l:
            predict_data = []
        predict_data.append(l)
        if "im_detect:" in l:
            vid, segments = get_segments(predict_data, thresh, framerate)
            if vid not in res:
                res[vid] = []
            res[vid] += segments
    return res

def select_top(segmentations, nms_thresh=0.99999, num_cls=0, topk=0):
  res = {}
  for vid, vinfo in segmentations.items():
    # select most likely classes
    if num_cls > 0:
      ave_scores = np.zeros(21)
      for i in xrange(1, 21):
        ave_scores[i] = np.sum([d['score'] for d in vinfo if d['label']==i])
      labels = list(ave_scores.argsort()[::-1][:num_cls])
    else:
      labels = list(set([d['label'] for d in vinfo]))

    # NMS
    res_nms = []
    for lab in labels:
      nms_in = [d['segment'] + [d['score']] for d in vinfo if d['label'] == lab]
      keep = nms(np.array(nms_in), nms_thresh)
      for i in keep:
        tmp = {'label': lab, 'score':nms_in[i][2], 'segment': nms_in[i][0:2]}
        res_nms.append(tmp)

    # select topk
    scores = [d['score'] for d in res_nms]
    sortid = np.argsort(scores)[-topk:]
    res[vid] = [res_nms[id] for id in sortid]
  return res

parser = argparse.ArgumentParser(description="log analysis.py")
parser.add_argument('log_file', type=str, help="test log file path")
parser.add_argument('--framerate', type=int, help="frame rate of videos extract by ffmpeg")
parser.add_argument('--thresh', type=float, default=0.005, help="filter those dets low than the thresh, default=0.0005")
parser.add_argument('--nms_thresh', type=float, default=0.4, help="nms thresh, default=0.3")
parser.add_argument('--topk', type=int, default=200, help="select topk dets, default=200")
parser.add_argument('--num_cls', type=int, default=0, help="select most likely classes, default=0")

args = parser.parse_args()
classes = generate_classes(META_DIR+'test', 'test', use_ambiguous=False)
segmentations = analysis_log(args.log_file, thresh = args.thresh, framerate=args.framerate)
segmentations = select_top(segmentations, nms_thresh=args.nms_thresh, num_cls=args.num_cls, topk=args.topk)

res = {'version': 'VERSION 1.3',
       'external_data': {'used': True, 'details': 'C3D pre-trained on activity-1.3 training set'},
       'results': {}}
for vid, vinfo in segmentations.items():
  res['results'][vid] = vinfo

with open('tmp.txt', 'w') as outfile:
  for vid, vinfo in segmentations.items():
    for seg in vinfo:
      outfile.write("{} {} {} {} {}\n".format(vid, seg['segment'][0], seg['segment'][1], int(seg['label']) ,seg['score']))

def matlab_eval():
    print('Computing results with the official Matlab eval code')
    path = os.path.join(THIS_DIR, 'Evaluation')
    cmd = 'cp tmp.txt {} && '.format(THIS_DIR)
    cmd += 'cd {} && '.format(path)
    cmd += 'matlab -nodisplay -nodesktop '
    cmd += '-r "dbstop if error; '
    cmd += 'eval_thumos14(); quit;"'

    print('Runing: \n {}'.format(cmd))
    status = subprocess.call(cmd, shell=True)

matlab_eval()


# R-C3D
# Written by Huijuan Xu

import sys, os, errno
import numpy as np
import csv
import json
import copy
import argparse
import subprocess

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FRAME_DIR = '/media/F/THUMOS14'
META_DIR = os.path.join(FRAME_DIR, 'annotation_')

def nms(dets, thresh=0.4):
    """Pure Python NMS baseline."""
    if len(dets) == 0: return []
    x1 = dets[:, 0]
    x2 = dets[:, 1]
    scores = dets[:, 2]
    lengths = x2 - x1
    order = scores.argsort()[::-1]
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        inter = np.maximum(0.0, xx2 - xx1)
        ovr = inter / (lengths[i] + lengths[order[1:]] - inter)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep

def generate_classes(meta_dir, split, use_ambiguous=False):
    class_id = {0: 'Background'}
    with open(os.path.join(meta_dir, 'detclasslist.txt'), 'r') as f:
        lines = f.readlines()
        for l in lines:
            cname = l.strip().split()[-1]
            cid = int(l.strip().split()[0])
            class_id[cid] = cname
        if use_ambiguous:
            class_id[21] = 'Ambiguous'

    return class_id
'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
      # video name and sliding window length
      if "fg_name :" in l:
         vid = l.split('/')[-1]

      # frame index, time, confident score
      elif "frames :" in l:
         start_frame=int(l.split()[4])
         end_frame=int(l.split()[5])
         stride = int(l.split()[6].split(']')[0])

      elif "activity:" in l:
         label = int(l.split()[1])
         tmp['label'] = label
         find_next = True

      elif "im_detect" in l:
         return vid, segments

      elif find_next:
         try:
           left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
           right_frame = float(l.split()[1])*stride + start_frame
         except:
           left_frame = float(l.split()[1])*stride + start_frame
           right_frame = float(l.split()[2])*stride + start_frame
         if (left_frame < end_frame) and (right_frame <= end_frame):
           left  = left_frame / 25.0
           right = right_frame / 25.0
           try:
             score = float(l.split()[-1].split(']')[0])
           except:
             score = float(l.split()[-2])
           if score > thresh:
             tmp1 = copy.deepcopy(tmp)
             tmp1['score'] = score
             tmp1['segment'] = [left, right]
             segments.append(tmp1)
         elif (left_frame < end_frame) and (right_frame > end_frame):
             if (end_frame-left_frame)*1.0/(right_frame-left_frame)>=0:
                 right_frame = end_frame
                 left  = left_frame / 25.0
                 right = right_frame / 25.0
                 try:
                   score = float(l.split()[-1].split(']')[0])
                 except:
                   score = float(l.split()[-2])
                 if score > thresh:
                     tmp1 = copy.deepcopy(tmp)
                     tmp1['score'] = score
                     tmp1['segment'] = [left, right]
                     segments.append(tmp1)

'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
        # video name and sliding window length
        if "fg_name:" in l:
            vid = l.split('/')[-1]

        # frame index, time, confident score
        elif "frames:" in l:
            start_frame=int(l.split()[3])
            end_frame=int(l.split()[4])
            stride = int(l.split()[5].split(']')[0])

        elif "activity:" in l:
            label = int(l.split()[1])
            tmp['label'] = label
            find_next = True

        elif "im_detect" in l:
            return vid, segments

        elif find_next:
            try:
                left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
                right_frame = float(l.split()[1])*stride + start_frame
            except:
                left_frame = float(l.split()[1])*stride + start_frame
                right_frame = float(l.split()[2])*stride + start_frame

            try:
                score = float(l.split()[-1].split(']')[0])
            except:
                score = float(l.split()[-2])

            if (left_frame >= right_frame):
                print("???", l)
                continue

            if right_frame > end_frame:
                right_frame = end_frame

            left  = left_frame / framerate
            right = right_frame / framerate
            if score > thresh:
                tmp1 = copy.deepcopy(tmp)
                tmp1['score'] = score
                tmp1['segment'] = [left, right]
                segments.append(tmp1)

def analysis_log(logfile, thresh, framerate):
    with open(logfile, 'r') as f:
        lines = f.read().splitlines()
    predict_data = []
    res = {}
    for l in lines:
        if "frames:" in l:
            predict_data = []
        predict_data.append(l)
        if "im_detect:" in l:
            vid, segments = get_segments(predict_data, thresh, framerate)
            if vid not in res:
                res[vid] = []
            res[vid] += segments
    return res

def select_top(segmentations, nms_thresh=0.99999, num_cls=0, topk=0):
  res = {}
  for vid, vinfo in segmentations.items():
    # select most likely classes
    if num_cls > 0:
      ave_scores = np.zeros(21)
      for i in xrange(1, 21):
        ave_scores[i] = np.sum([d['score'] for d in vinfo if d['label']==i])
      labels = list(ave_scores.argsort()[::-1][:num_cls])
    else:
      labels = list(set([d['label'] for d in vinfo]))

    # NMS
    res_nms = []
    for lab in labels:
      nms_in = [d['segment'] + [d['score']] for d in vinfo if d['label'] == lab]
      keep = nms(np.array(nms_in), nms_thresh)
      for i in keep:
        tmp = {'label': lab, 'score':nms_in[i][2], 'segment': nms_in[i][0:2]}
        res_nms.append(tmp)

    # select topk
    scores = [d['score'] for d in res_nms]
    sortid = np.argsort(scores)[-topk:]
    res[vid] = [res_nms[id] for id in sortid]
  return res

parser = argparse.ArgumentParser(description="log analysis.py")
parser.add_argument('log_file', type=str, help="test log file path")
parser.add_argument('--framerate', type=int, help="frame rate of videos extract by ffmpeg")
parser.add_argument('--thresh', type=float, default=0.005, help="filter those dets low than the thresh, default=0.0005")
parser.add_argument('--nms_thresh', type=float, default=0.4, help="nms thresh, default=0.3")
parser.add_argument('--topk', type=int, default=200, help="select topk dets, default=200")
parser.add_argument('--num_cls', type=int, default=0, help="select most likely classes, default=0")

args = parser.parse_args()
classes = generate_classes(META_DIR+'test', 'test', use_ambiguous=False)
segmentations = analysis_log(args.log_file, thresh = args.thresh, framerate=args.framerate)
segmentations = select_top(segmentations, nms_thresh=args.nms_thresh, num_cls=args.num_cls, topk=args.topk)

res = {'version': 'VERSION 1.3',
       'external_data': {'used': True, 'details': 'C3D pre-trained on activity-1.3 training set'},
       'results': {}}
for vid, vinfo in segmentations.items():
  res['results'][vid] = vinfo

with open('tmp.txt', 'w') as outfile:
  for vid, vinfo in segmentations.items():
    for seg in vinfo:
      outfile.write("{} {} {} {} {}\n".format(vid, seg['segment'][0], seg['segment'][1], int(seg['label']) ,seg['score']))

def matlab_eval():
    print('Computing results with the official Matlab eval code')
    path = os.path.join(THIS_D

    cmd = 'cp tmp.txt {} && '.format(THIS_DIR)
    cmd += 'cd {} && '.format(path)
    cmd += 'matlab -nodisplay -nodesktop '
    cmd += '-r "dbstop if error; '
    cmd += 'eval_thumos14(); quit;"'

    print('Runing: \n {}'.format(cmd))
    status = subprocess.call(cmd, shell=True)

matlab_eval()


if right_frame > end_frame:
                right_frame = end_frame

# R-C3D
# Written by Huijuan Xu

import sys, os, errno
import numpy as np
import csv
import json
import copy
import argparse
import subprocess

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FRAME_DIR = '/media/F/THUMOS14'
META_DIR = os.path.join(FRAME_DIR, 'annotation_')

def nms(dets, thresh=0.4):
    """Pure Python NMS baseline."""
    if len(dets) == 0: return []
    x1 = dets[:, 0]
    x2 = dets[:, 1]
    scores = dets[:, 2]
    lengths = x2 - x1
    order = scores.argsort()[::-1]
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        inter = np.maximum(0.0, xx2 - xx1)
        ovr = inter / (lengths[i] + lengths[order[1:]] - inter)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep

def generate_classes(meta_dir, split, use_ambiguous=False):
    class_id = {0: 'Background'}
    with open(os.path.join(meta_dir, 'detclasslist.txt'), 'r') as f:
        lines = f.readlines()
        for l in lines:
            cname = l.strip().split()[-1]
            cid = int(l.strip().split()[0])
            class_id[cid] = cname
        if use_ambiguous:
            class_id[21] = 'Ambiguous'

    return class_id
'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
      # video name and sliding window length
      if "fg_name :" in l:
         vid = l.split('/')[-1]

      # frame index, time, confident score
      elif "frames :" in l:
         start_frame=int(l.split()[4])
         end_frame=int(l.split()[5])
         stride = int(l.split()[6].split(']')[0])

      elif "activity:" in l:
         label = int(l.split()[1])
         tmp['label'] = label
         find_next = True

      elif "im_detect" in l:
         return vid, segments

      elif find_next:
         try:
           left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
           right_frame = float(l.split()[1])*stride + start_frame
         except:
           left_frame = float(l.split()[1])*stride + start_frame
           right_frame = float(l.split()[2])*stride + start_frame
         if (left_frame < end_frame) and (right_frame <= end_frame):
           left  = left_frame / 25.0
           right = right_frame / 25.0
           try:
             score = float(l.split()[-1].split(']')[0])
           except:
             score = float(l.split()[-2])
           if score > thresh:
             tmp1 = copy.deepcopy(tmp)
             tmp1['score'] = score
             tmp1['segment'] = [left, right]
             segments.append(tmp1)
         elif (left_frame < end_frame) and (right_frame > end_frame):
             if (end_frame-left_frame)*1.0/(right_frame-left_frame)>=0:
                 right_frame = end_frame
                 left  = left_frame / 25.0
                 right = right_frame / 25.0
                 try:
                   score = float(l.split()[-1].split(']')[0])
                 except:
                   score = float(l.split()[-2])
                 if score > thresh:
                     tmp1 = copy.deepcopy(tmp)
                     tmp1['score'] = score
                     tmp1['segment'] = [left, right]
                     segments.append(tmp1)

'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
        # video name and sliding window length
        if "fg_name:" in l:
            vid = l.split('/')[-1]

        # frame index, time, confident score
        elif "frames:" in l:
            start_frame=int(l.split()[3])
            end_frame=int(l.split()[4])
            stride = int(l.split()[5].split(']')[0])

        elif "activity:" in l:
            label = int(l.split()[1])
            tmp['label'] = label
            find_next = True

        elif "im_detect" in l:
            return vid, segments

        elif find_next:
            try:
                left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
                right_frame = float(l.split()[1])*stride + start_frame
            except:
                left_frame = float(l.split()[1])*stride + start_frame
                right_frame = float(l.split()[2])*stride + start_frame

            try:
                score = float(l.split()[-1].split(']')[0])
            except:
                score = float(l.split()[-2])

            if (left_frame >= right_frame):
                print("???", l)
                continue

            left  = left_frame / framerate
            right = right_frame / framerate
            if score > thresh:
                tmp1 = copy.deepcopy(tmp)
                tmp1['score'] = score
                tmp1['segment'] = [left, right]
                segments.append(tmp1)

def analysis_log(logfile, thresh, framerate):
    with open(logfile, 'r') as f:
        lines = f.read().splitlines()
    predict_data = []
    res = {}
    for l in lines:
        if "frames:" in l:
            predict_data = []
        predict_data.append(l)
        if "im_detect:" in l:
            vid, segments = get_segments(predict_data, thresh, framerate)
            if vid not in res:
                res[vid] = []
            res[vid] += segments
    return res

def select_top(segmentations, nms_thresh=0.99999, num_cls=0, topk=0):
  res = {}
  for vid, vinfo in segmentations.items():
    # select most likely classes
    if num_cls > 0:
      ave_scores = np.zeros(21)
      for i in xrange(1, 21):
        ave_scores[i] = np.sum([d['score'] for d in vinfo if d['label']==i])
      labels = list(ave_scores.argsort()[::-1][:num_cls])
    else:
      labels = list(set([d['label'] for d in vinfo]))

    # NMS
    res_nms = []
    for lab in labels:
      nms_in = [d['segment'] + [d['score']] for d in vinfo if d['label'] == lab]
      keep = nms(np.array(nms_in), nms_thresh)
      for i in keep:
        tmp = {'label': lab, 'score':nms_in[i][2], 'segment': nms_in[i][0:2]}
        res_nms.append(tmp)

    # select topk
    scores = [d['score'] for d in res_nms]
    sortid = np.argsort(scores)[-topk:]
    res[vid] = [res_nms[id] for id in sortid]
  return res

parser = argparse.ArgumentParser(description="log analysis.py")
parser.add_argument('log_file', type=str, help="test log file path")
parser.add_argument('--framerate', type=int, help="frame rate of videos extract by ffmpeg")
parser.add_argument('--thresh', type=float, default=0.005, help="filter those dets low than the thresh, default=0.0005")
parser.add_argument('--nms_thresh', type=float, default=0.4, help="nms thresh, default=0.3")
parser.add_argument('--topk', type=int, default=200, help="select topk dets, default=200")
parser.add_argument('--num_cls', type=int, default=0, help="select most likely classes, default=0")

args = parser.parse_args()
classes = generate_classes(META_DIR+'test', 'test', use_ambiguous=False)
segmentations = analysis_log(args.log_file, thresh = args.thresh, framerate=args.framerate)
segmentations = select_top(segmentations, nms_thresh=args.nms_thresh, num_cls=args.num_cls, topk=args.topk)

res = {'version': 'VERSION 1.3',
       'external_data': {'used': True, 'details': 'C3D pre-trained on activity-1.3 training set'},
       'results': {}}
for vid, vinfo in segmentations.items():
  res['results'][vid] = vinfo

with open('tmp.txt', 'w') as outfile:
  for vid, vinfo in segmentations.items():
    for seg in vinfo:
      outfile.write("{} {} {} {} {}\n".format(vid, seg['segment'][0], seg['segment'][1], int(seg['label']) ,seg['score']))

def matlab_eval():
    print('Computing results with the official Matlab eval code')
    path = os.path.join(THIS_DIR, 'Evaluation')
    cmd = 'cp tmp.txt {} && '.format(THIS_DIR)
    cmd += 'cd {} && '.format(path)
    cmd += 'matlab -nodisplay -nodesktop '
    cmd += '-r "dbstop if error; '
    cmd += 'eval_thumos14(); quit;"'

    print('Runing: \n {}'.format(cmd))
    status = subprocess.call(cmd, shell=True)

matlab_eval()


for seg in vinfo:
      outfile.write("{} {} {} {} {}\n".format(vid, seg['segment'][0], seg['segment'][1], int(seg['label']) ,seg['score']))

# R-C3D
# Written by Huijuan Xu

import sys, os, errno
import numpy as np
import csv
import json
import copy
import argparse
import subprocess

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FRAME_DIR = '/media/F/THUMOS14'
META_DIR = os.path.join(FRAME_DIR, 'annotation_')

def nms(dets, thresh=0.4):
    """Pure Python NMS baseline."""
    if len(dets) == 0: return []
    x1 = dets[:, 0]
    x2 = dets[:, 1]
    scores = dets[:, 2]
    lengths = x2 - x1
    order = scores.argsort()[::-1]
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        inter = np.maximum(0.0, xx2 - xx1)
        ovr = inter / (lengths[i] + lengths[order[1:]] - inter)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep

def generate_classes(meta_dir, split, use_ambiguous=False):
    class_id = {0: 'Background'}
    with open(os.path.join(meta_dir, 'detclasslist.txt'), 'r') as f:
        lines = f.readlines()
        for l in lines:
            cname = l.strip().split()[-1]
            cid = int(l.strip().split()[0])
            class_id[cid] = cname
        if use_ambiguous:
            class_id[21] = 'Ambiguous'

    return class_id
'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
      # video name and sliding window length
      if "fg_name :" in l:
         vid = l.split('/')[-1]

      # frame index, time, confident score
      elif "frames :" in l:
         start_frame=int(l.split()[4])
         end_frame=int(l.split()[5])
         stride = int(l.split()[6].split(']')[0])

      elif "activity:" in l:
         label = int(l.split()[1])
         tmp['label'] = label
         find_next = True

      elif "im_detect" in l:
         return vid, segments

      elif find_next:
         try:
           left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
           right_frame = float(l.split()[1])*stride + start_frame
         except:
           left_frame = float(l.split()[1])*stride + start_frame
           right_frame = float(l.split()[2])*stride + start_frame
         if (left_frame < end_frame) and (right_frame <= end_frame):
           left  = left_frame / 25.0
           right = right_frame / 25.0
           try:
             score = float(l.split()[-1].split(']')[0])
           except:
             score = float(l.split()[-2])
           if score > thresh:
             tmp1 = copy.deepcopy(tmp)
             tmp1['score'] = score
             tmp1['segment'] = [left, right]
             segments.append(tmp1)
         elif (left_frame < end_frame) and (right_frame > end_frame):
             if (end_frame-left_frame)*1.0/(right_frame-left_frame)>=0:
                 right_frame = end_frame
                 left  = left_frame / 25.0
                 right = right_frame / 25.0
                 try:
                   score = float(l.split()[-1].split(']')[0])
                 except:
                   score = float(l.split()[-2])
                 if score > thresh:
                     tmp1 = copy.deepcopy(tmp)
                     tmp1['score'] = score
                     tmp1['segment'] = [left, right]
                     segments.append(tmp1)

'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
        # video name and sliding window length
        if "fg_name:" in l:
            vid = l.split('/')[-1]

        # frame index, time, confident score
        elif "frames:" in l:
            start_frame=int(l.split()[3])
            end_frame=int(l.split()[4])
            stride = int(l.split()[5].split(']')[0])

        elif "activity:" in l:
            label = int(l.split()[1])
            tmp['label'] = label
            find_next = True

        elif "im_detect" in l:
            return vid, segments

        elif find_next:
            try:
                left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
                right_frame = float(l.split()[1])*stride + start_frame
            except:
                left_frame = float(l.split()[1])*stride + start_frame
                right_frame = float(l.split()[2])*stride + start_frame

            try:
                score = float(l.split()[-1].split(']')[0])
            except:
                score = float(l.split()[-2])

            if (left_frame >= right_frame):
                print("???", l)
                continue

            if right_frame > end_frame:
                right_frame = end_frame

            left  = left_frame / framerate
            right = right_frame / framerate
            if score > thresh:
                tmp1 = copy.deepcopy(tmp)
                tmp1['score'] = score
                tmp1['segment'] = [left, right]
                segments.append(tmp1)

def analysis_log(logfile, thresh, framerate):
    with open(logfile, 'r') as f:
        lines = f.read().splitlines()
    predict_data = []
    res = {}
    for l in lines:
        if "frames:" in l:
            predict_data = []
        predict_data.append(l)
        if "im_detect:" in l:
            vid, segments = get_segments(predict_data, thresh, framerate)
            if vid not in res:
                res[vid] = []
            res[vid] += segments
    return res

def select_top(segmentations, nms_thresh=0.99999, num_cls=0, topk=0):
  res = {}
  for vid, vinfo in segmentations.items():
    # select most likely classes
    if num_cls > 0:
      ave_scores = np.zeros(21)
      for i in xrange(1, 21):
        ave_scores[i] = np.sum([d['score'] for d in vinfo if d['label']==i])
      labels = list(ave_scores.argsort()[::-1][:num_cls])
    else:
      labels = list(set([d['label'] for d in vinfo]))

    # NMS
    res_nms = []
    for lab in labels:
      nms_in = [d['segment'] + [d['score']] for d in vinfo if d['label'] == lab]
      keep = nms(np.array(nms_in), nms_thresh)
      for i in keep:
        tmp = {'label': lab, 'score':nms_in[i][2], 'segment': nms_in[i][0:2]}
        res_nms.append(tmp)

    # select topk
    scores = [d['score'] for d in res_nms]
    sortid = np.argsort(scores)[-topk:]
    res[vid] = [res_nms[id] for id in sortid]
  return res

parser = argparse.ArgumentParser(description="log analysis.py")
parser.add_argument('log_file', type=str, help="test log file path")
parser.add_argument('--framerate', type=int, help="frame rate of videos extract by ffmpeg")
parser.add_argument('--thresh', type=float, default=0.005, help="filter those dets low than the thresh, default=0.0005")
parser.add_argument('--nms_thresh', type=float, default=0.4, help="nms thresh, default=0.3")
parser.add_argument('--topk', type=int, default=200, help="select topk dets, default=200")
parser.add_argument('--num_cls', type=int, default=0, help="select most likely classes, default=0")

args = parser.parse_args()
classes = generate_classes(META_DIR+'test', 'test', use_ambiguous=False)
segmentations = analysis_log(args.log_file, thresh = args.thresh, framerate=args.framerate)
segmentations = select_top(segmentations, nms_thresh=args.nms_thresh, num_cls=args.num_cls, topk=args.topk)

res = {'version': 'VERSION 1.3',
       'external_data': {'used': True, 'details': 'C3D pre-trained on activity-1.3 training set'},
       'results': {}}
for vid, vinfo in segmentations.items():
  res['results'][vid] = vinfo

with open('tmp.txt', 'w') as outfile:
  for vid, vinfo in segmentations.items():

def matlab_eval():
    print('Computing results with the official Matlab eval code')
    path = os.path.join(THIS_DIR, 'Evaluation')
    cmd = 'cp tmp.txt {} && '.format(THIS_DIR)
    cmd += 'cd {} && '.format(path)
    cmd += 'matlab -nodisplay -nodesktop '
    cmd += '-r "dbstop if error; '
    cmd += 'eval_thumos14(); quit;"'

    print('Runing: \n {}'.format(cmd))
    status = subprocess.call(cmd, shell=True)

matlab_eval()


while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        inter = np.maximum(0.0, xx2 - xx1)
        ovr = inter / (lengths[i] + lengths[order[1:]] - inter)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]

# R-C3D
# Written by Huijuan Xu

import sys, os, errno
import numpy as np
import csv
import json
import copy
import argparse
import subprocess

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FRAME_DIR = '/media/F/THUMOS14'
META_DIR = os.path.join(FRAME_DIR, 'annotation_')

def nms(dets, thresh=0.4):
    """Pure Python NMS baseline."""
    if len(dets) == 0: return []
    x1 = dets[:, 0]
    x2 = dets[:, 1]
    scores = dets[:, 2]
    lengths = x2 - x1
    order = scores.argsort()[::-1]
    keep = []

    return keep

def generate_classes(meta_dir, split, use_ambiguous=False):
    class_id = {0: 'Background'}
    with open(os.path.join(meta_dir, 'detclasslist.txt'), 'r') as f:
        lines = f.readlines()
        for l in lines:
            cname = l.strip().split()[-1]
            cid = int(l.strip().split()[0])
            class_id[cid] = cname
        if use_ambiguous:
            class_id[21] = 'Ambiguous'

    return class_id
'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
      # video name and sliding window length
      if "fg_name :" in l:
         vid = l.split('/')[-1]

      # frame index, time, confident score
      elif "frames :" in l:
         start_frame=int(l.split()[4])
         end_frame=int(l.split()[5])
         stride = int(l.split()[6].split(']')[0])

      elif "activity:" in l:
         label = int(l.split()[1])
         tmp['label'] = label
         find_next = True

      elif "im_detect" in l:
         return vid, segments

      elif find_next:
         try:
           left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
           right_frame = float(l.split()[1])*stride + start_frame
         except:
           left_frame = float(l.split()[1])*stride + start_frame
           right_frame = float(l.split()[2])*stride + start_frame
         if (left_frame < end_frame) and (right_frame <= end_frame):
           left  = left_frame / 25.0
           right = right_frame / 25.0
           try:
             score = float(l.split()[-1].split(']')[0])
           except:
             score = float(l.split()[-2])
           if score > thresh:
             tmp1 = copy.deepcopy(tmp)
             tmp1['score'] = score
             tmp1['segment'] = [left, right]
             segments.append(tmp1)
         elif (left_frame < end_frame) and (right_frame > end_frame):
             if (end_frame-left_frame)*1.0/(right_frame-left_frame)>=0:
                 right_frame = end_frame
                 left  = left_frame / 25.0
                 right = right_frame / 25.0
                 try:
                   score = float(l.split()[-1].split(']')[0])
                 except:
                   score = float(l.split()[-2])
                 if score > thresh:
                     tmp1 = copy.deepcopy(tmp)
                     tmp1['score'] = score
                     tmp1['segment'] = [left, right]
                     segments.append(tmp1)

'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
        # video name and sliding window length
        if "fg_name:" in l:
            vid = l.split('/')[-1]

        # frame index, time, confident score
        elif "frames:" in l:
            start_frame=int(l.split()[3])
            end_frame=int(l.split()[4])
            stride = int(l.split()[5].split(']')[0])

        elif "activity:" in l:
            label = int(l.split()[1])
            tmp['label'] = label
            find_next = True

        elif "im_detect" in l:
            return vid, segments

        elif find_next:
            try:
                left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
                right_frame = float(l.split()[1])*stride + start_frame
            except:
                left_frame = float(l.split()[1])*stride + start_frame
                right_frame = float(l.split()[2])*stride + start_frame

            try:
                score = float(l.split()[-1].split(']')[0])
            except:
                score = float(l.split()[-2])

            if (left_frame >= right_frame):
                print("???", l)
                continue

            if right_frame > end_frame:
                right_frame = end_frame

            left  = left_frame / framerate
            right = right_frame / framerate
            if score > thresh:
                tmp1 = copy.deepcopy(tmp)
                tmp1['score'] = score
                tmp1['segment'] = [left, right]
                segments.append(tmp1)

def analysis_log(logfile, thresh, framerate):
    with open(logfile, 'r') as f:
        lines = f.read().splitlines()
    predict_data = []
    res = {}
    for l in lines:
        if "frames:" in l:
            predict_data = []
        predict_data.append(l)
        if "im_detect:" in l:
            vid, segments = get_segments(predict_data, thresh, framerate)
            if vid not in res:
                res[vid] = []
            res[vid] += segments
    return res

def select_top(segmentations, nms_thresh=0.99999, num_cls=0, topk=0):
  res = {}
  for vid, vinfo in segmentations.items():
    # select most likely classes
    if num_cls > 0:
      ave_scores = np.zeros(21)
      for i in xrange(1, 21):
        ave_scores[i] = np.sum([d['score'] for d in vinfo if d['label']==i])
      labels = list(ave_scores.argsort()[::-1][:num_cls])
    else:
      labels = list(set([d['label'] for d in vinfo]))

    # NMS
    res_nms = []
    for lab in labels:
      nms_in = [d['segment'] + [d['score']] for d in vinfo if d['label'] == lab]
      keep = nms(np.array(nms_in), nms_thresh)
      for i in keep:
        tmp = {'label': lab, 'score':nms_in[i][2], 'segment': nms_in[i][0:2]}
        res_nms.append(tmp)

    # select topk
    scores = [d['score'] for d in res_nms]
    sortid = np.argsort(scores)[-topk:]
    res[vid] = [res_nms[id] for id in sortid]
  return res

parser = argparse.ArgumentParser(description="log analysis.py")
parser.add_argument('log_file', type=str, help="test log file path")
parser.add_argument('--framerate', type=int, help="frame rate of videos extract by ffmpeg")
parser.add_argument('--thresh', type=float, default=0.005, help="filter those dets low than the thresh, default=0.0005")
parser.add_argument('--nms_thresh', type=float, default=0.4, help="nms thresh, default=0.3")
parser.add_argument('--topk', type=int, default=200, help="select topk dets, default=200")
parser.add_argument('--num_cls', type=int, default=0, help="select most likely classes, default=0")

args = parser.parse_args()
classes = generate_classes(META_DIR+'test', 'test', use_ambiguous=False)
segmentations = analysis_log(args.log_file, thresh = args.thresh, framerate=args.framerate)
segmentations = select_top(segmentations, nms_thresh=args.nms_thresh, num_cls=args.num_cls, topk=args.topk)

res = {'version': 'VERSION 1.3',
       'external_data': {'used': True, 'details': 'C3D pre-trained on activity-1.3 training set'},
       'results': {}}
for vid, vinfo in segmentations.items():
  res['results'][vid] = vinfo

with open('tmp.txt', 'w') as outfile:
  for vid, vinfo in segmentations.items():
    for seg in vinfo:
      outfile.write("{} {} {} {} {}\n".format(vid, seg['segment'][0], seg['segment'][1], int(seg['label']) ,seg['score']))

def matlab_eval():
    print('Computing results with the official Matlab eval code')
    path = os.path.join(THIS_DIR, 'Evaluation')
    cmd = 'cp tmp.txt {} && '.format(THIS_DIR)
    cmd += 'cd {} && '.format(path)
    cmd += 'matlab -nodisplay -nodesktop '
    cmd += '-r "dbstop if error; '
    cmd += 'eval_thumos14(); quit;"'

    print('Runing: \n {}'.format(cmd))
    status = subprocess.call(cmd, shell=True)

matlab_eval()


# R-C3D
# Written by Huijuan Xu

import sys, os, errno
import numpy as np
import csv
import json
import copy
import argparse
import subprocess

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FRAME_DIR = '/media/F/THUMOS14'
META_DIR = os.path.join(FRAME_DIR, 'annotation_')

def nms(dets, thresh=0.4):
    """Pure Python NMS baseline."""
    if len(dets) == 0: return []
    x1 = dets[:, 0]
    x2 = dets[:, 1]
    scores = dets[:, 2]
    lengths = x2 - x1
    order = scores.argsort()[::-1]
    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        inter = np.maximum(0.0, xx2 - xx1)
        ovr = inter / (lengths[i] + lengths[order[1:]] - inter)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep

def generate_classes(meta_dir, split, use_ambiguous=False):
    class_id = {0: 'Background'}
    with open(os.path.join(meta_dir, 'detclasslist.txt'), 'r') as f:
        lines = f.readlines()
        for l in lines:
            cname = l.strip().split()[-1]
            cid = int(l.strip().split()[0])
            class_id[cid] = cname
        if use_ambiguous:
            class_id[21] = 'Ambiguous'

    return class_id
'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
      # video name and sliding window length
      if "fg_name :" in l:
         vid = l.split('/')[-1]

      # frame index, time, confident score
      elif "frames :" in l:
         start_frame=int(l.split()[4])
         end_frame=int(l.split()[5])
         stride = int(l.split()[6].split(']')[0])

      elif "activity:" in l:
         label = int(l.split()[1])
         tmp['label'] = label
         find_next = True

      elif "im_detect" in l:
         return vid, segments

      elif find_next:
         try:
           left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
           right_frame = float(l.split()[1])*stride + start_frame
         except:
           left_frame = float(l.split()[1])*stride + start_frame
           right_frame = float(l.split()[2])*stride + start_frame
         if (left_frame < end_frame) and (right_frame <= end_frame):
           left  = left_frame / 25.0
           right = right_frame / 25.0
           try:
             score = float(l.split()[-1].split(']')[0])
           except:
             score = float(l.split()[-2])
           if score > thresh:
             tmp1 = copy.deepcopy(tmp)
             tmp1['score'] = score
             tmp1['segment'] = [left, right]
             segments.append(tmp1)
         elif (left_frame < end_frame) and (right_frame > end_frame):
             if (end_frame-left_frame)*1.0/(right_frame-left_frame)>=0:
                 right_frame = end_frame
                 left  = left_frame / 25.0
                 right = right_frame / 25.0
                 try:
                   score = float(l.split()[-1].split(']')[0])
                 except:
                   score = float(l.split()[-2])
                 if score > thresh:
                     tmp1 = copy.deepcopy(tmp)
                     tmp1['score'] = score
                     tmp1['segment'] = [left, right]
                     segments.append(tmp1)

'''
def get_segments(data, thresh, framerate):
    segments = []
    vid = 'Background'
    find_next = False
    tmp = {'label' : 0, 'score': 0, 'segment': [0, 0]}
    for l in data:
        # video name and sliding window length
        if "fg_name:" in l:
            vid = l.split('/')[-1]

        # frame index, time, confident score
        elif "frames:" in l:
            start_frame=int(l.split()[3])
            end_frame=int(l.split()[4])
            stride = int(l.split()[5].split(']')[0])

        elif "activity:" in l:
            label = int(l.split()[1])
            tmp['label'] = label
            find_next = True

        elif "im_detect" in l:
            return vid, segments

        elif find_next:
            try:
                left_frame = float(l.split()[0].split('[')[-1])*stride + start_frame
                right_frame = float(l.split()[1])*stride + start_frame
            except:
                left_frame = float(l.split()[1])*stride + start_frame
                right_frame = float(l.split()[2])*stride + start_frame

            if (left_frame >= right_frame):
                print("???", l)
                continue

            if right_frame > end_frame:
                right_frame = end_frame

            left  = left_frame / framerate
            right = right_frame / framerate
            if score > thresh:
                tmp1 = copy.deepcopy(tmp)
                tmp1['score'] = score
                tmp1['segment'] = [left, right]
                segments.append(tmp1)

def analysis_log(logfile, thresh, framerate):
    with open(logfile, 'r') as f:
        lines = f.read().splitlines()
    predict_data = []
    res = {}
    for l in lines:
        if "frames:" in l:
            predict_data = []
        predict_data.append(l)
        if "im_detect:" in l:
            vid, segments = get_segments(predict_data, thresh, framerate)
            if vid not in res:
                res[vid] = []
            res[vid] += segments
    return res

def select_top(segmentations, nms_thresh=0.99999, num_cls=0, topk=0):
  res = {}
  for vid, vinfo in segmentations.items():
    # select most likely classes
    if num_cls > 0:
      ave_scores = np.zeros(21)
      for i in xrange(1, 21):
        ave_scores[i] = np.sum([d['score'] for d in vinfo if d['label']==i])
      labels = list(ave_scores.argsort()[::-1][:num_cls])
    else:
      labels = list(set([d['label'] for d in vinfo]))

    # NMS
    res_nms = []
    for lab in labels:
      nms_in = [d['segment'] + [d['score']] for d in vinfo if d['label'] == lab]
      keep = nms(np.array(nms_in), nms_thresh)
      for i in keep:
        tmp = {'label': lab, 'score':nms_in[i][2], 'segment': nms_in[i][0:2]}
        res_nms.append(tmp)

    # select topk
    scores = [d['score'] for d in res_nms]
    sortid = np.argsort(scores)[-topk:]
    res[vid] = [res_nms[id] for id in sortid]
  return res

parser = argparse.ArgumentParser(description="log analysis.py")
parser.add_argument('log_file', type=str, help="test log file path")
parser.add_argument('--framerate', type=int, help="frame rate of videos extract by ffmpeg")
parser.add_argument('--thresh', type=float, default=0.005, help="filter those dets low than the thresh, default=0.0005")
parser.add_argument('--nms_thresh', type=float, default=0.4, help="nms thresh, default=0.3")
parser.add_argument('--topk', type=int, default=200, help="select topk dets, default=200")
parser.add_argument('--num_cls', type=int, default=0, help="select most likely classes, default=0")

args = parser.parse_args()
classes = generate_classes(META_DIR+'test', 'test', use_ambiguous=False)
segmentations = analysis_log(args.log_file, thresh = args.thresh, framerate=args.framerate)
segmentations = select_top(segmentations, nms_thresh=args.nms_thresh, num_cls=args.num_cls, topk=args.topk)

res = {'version': 'VERSION 1.3',
       'external_data': {'used': True, 'details': 'C3D pre-trained on activity-1.3 training set'},
       'results': {}}
for vid, vinfo in segmentations.items():
  res['results'][vid] = vinfo

with open('tmp.txt', 'w') as outfile:
  for vid, vinfo in segmentations.items():
    for seg in vinfo:
      outfile.write("{} {} {} {} {}\n".format(vid, seg['segment'][0], seg['segment'][1], int(seg['label']) ,seg['score']))

def matlab_eval():
    print('Computing results with the official Matlab eval code')
    path = os.path.join(THIS_DIR, 'Evaluation')
    cmd = 'cp tmp.txt {} && '.format(THIS_DIR)
    cmd += 'cd {} && '.format(path)
    cmd += 'matlab -nodisplay -nodesktop '
    cmd += '-r "dbstop if error; '
    cmd += 'eval_thumos14(); quit;"'

    print('Runing: \n {}'.format(cmd))
    status = subprocess.call(cmd, shell=True)

matlab_eval()


self._side = side

    def get_side(self):
        """ Return the side of the Cube """
        return self._side

    def set_side(self, new_side):
        """ Set the value of the Cube's side. """
        self._side = abs(new_side)


""" Set the value of the Cube's side. """
        self._side = abs(new_side)

class Cube:
    def __init__(self, side=0):
        self._side = side

    def get_side(self):
        """ Return the side of the Cube """
        return self._side

    def set_side(self, new_side):


class Cube:
    def __init__(self, side=0):
        self._side = side

    def get_side(self):
        """ Return the side of the Cube """
        return self._side

    def set_side(self, new_side):
        """ Set the value of the Cube's side. """
        self._side = abs(new_si


class Cube:
    def __init__(self, side=0):
        self._side = side

    def get_side(self):
        """ Return the side of the Cube """

    def set_side(self, new_side):
        """ Set the value of the Cube's side. """
        self._side = abs(new_side)


def fx_quantity_unitless() -> Quantity:
    # From Q605704 (dozen)

from pytest import fixture

from wikidata.client import Client
from wikidata.entity import Entity, EntityId
from wikidata.quantity import Quantity

@fixture

return Quantity(
        amount=12,
        lower_bound=None,
        upper_bound=None,
        unit=None)

@fixture
def fx_quantity_with_unit() -> Quantity:
    client = Client()
    # From Q520
    return Quantity(
        amount=610.13,
        lower_bound=610.12,
        upper_bound=610.14,
        unit=client.get(EntityId("Q828224")))

def test_quantity_unitless_value(fx_quantity_unitless: Quantity):
    assert fx_quantity_unitless.amount == 12
    assert fx_quantity_unitless.lower_bound is None
    assert fx_quantity_unitless.upper_bound is None
    assert fx_quantity_unitless.unit is None

def test_quantity_unitless_repr(fx_quantity_unitless: Quantity):
    assert (repr(fx_quantity_unitless) ==
            ("wikidata.quantity.Quantity(12, None, None, None)"))

def test_quantity_with_unit_value(fx_quantity_with_unit: Quantity):
    assert fx_quantity_with_unit.amount == 610.13
    assert fx_quantity_with_unit.lower_bound == 610.12
    assert fx_quantity_with_unit.upper_bound == 610.14
    assert isinstance(fx_quantity_with_unit.unit, Entity)
    assert fx_quantity_with_unit.unit.id == "Q828224"

def test_quantity_with_unit_repr(fx_quantity_with_unit: Quantity):
    assert (repr(fx_quantity_with_unit) ==
            ("wikidata.quantity.Quantity(610.13, 610.12, 610.14, "
             "<wikidata.entity.Entity Q828224>)"))


client = Client()
    # From Q520
    return Quantity(
        amount=610.13,
        lower_bound=610.12,
        upper_bound=610.14,
        unit=client.get(EntityId("Q828224")))

from pytest import fixture

from wikidata.client import Client
from wikidata.entity import Entity, EntityId
from wikidata.quantity import Quantity

@fixture
def fx_quantity_unitless() -> Quantity:
    # From Q605704 (dozen)
    return Quantity(
        amount=12,
        lower_bound=None,
        upper_bound=None,
        unit=None)

@fixture
def fx_quantity_with_unit() -> Quantity:

def test_quantity_unitless_value(fx_quantity_unitless: Quantity):
    assert fx_quantity_unitless.amount == 12
    assert fx_quantity_unitless.lower_bound is None
    assert fx_quantity_unitless.upper_bound is None
    assert fx_quantity_unitless.unit is None

def test_quantity_unitless_repr(fx_quantity_unitless: Quantity):
    assert (repr(fx_quantity_unitless) ==
            ("wikidata.quantity.Quantity(12, None, None, None)"))

def test_quantity_with_unit_value(fx_quantity_with_unit: Quantity):
    assert fx_quantity_with_unit.amount == 610.13
    assert fx_quantity_with_unit.lower_bound == 610.12
    assert fx_quantity_with_unit.upper_bound == 610.14
    assert isinstance(fx_quantity_with_unit.unit, Entity)
    assert fx_quantity_with_unit.unit.id == "Q828224"

def test_quantity_with_unit_repr(fx_quantity_with_unit: Quantity):
    assert (repr(fx_quantity_with_unit) ==
            ("wikidata.quantity.Quantity(610.13, 610.12, 610.14, "
             "<wikidata.entity.Entity Q828224>)"))


    # From Q520
    return Quantity(
        amount=610.13,
        lower_bound=610.12,
        upper_bound=610.14,
        unit=client.get(EntityId("Q828224")))

def test_quantity_unitless_value(fx_quantity_unitless: Quantity):
    assert fx_quantity_unitless.amount == 12
    assert fx_quantity_unitless.lower_bound is None
    assert fx_quantity_unitless.upper_bound is None
    assert fx_quantity_unitless.unit is None

def test_quantity_unitless_repr(fx_quantity_unitless: Quantity):
    assert (repr(fx_quantity_unitless) ==
            ("wikidata.quantity.Quantity(12, None, None, None)"))

def test_quantity_with_unit_value(fx_quantity_with_unit: Quantity):
    assert fx_quantity_with_unit.amount == 610.13
    assert fx_quantity_with_unit.lower_bound == 610.12
    assert fx_quantity_with_unit.upper_bound == 610.14
    assert isinstance(fx_quantity_with_unit.unit, Entity)
    assert fx_quantity_with_unit.unit.id == "Q828224"

def test_quantity_with_unit_repr(fx_quantity_with_unit: Quantity):
    assert (repr(fx_quantity_with_unit) ==
            ("wikidata.quantity.Quantity(610.13, 610.12, 610.14, "
             "<wikidata.entity.Entity Q828224>)"))


from pytest import fixture

from wikidata.client import Client
from wikidata.entity import Entity, EntityId
from wikidata.quantity import Quantity

@fixture
def fx_quantity_unitless() -> Quantity:
    # From Q605704 (dozen)
    return Quantity(
        amount=12,
        lower_bound=None,
        upper_bound=None,
        unit=None)

@fixture
def fx_quantity_with_unit() -> Quantity:
    client = Client()

    return Quantity(
        amount=610.13,
        lower_bound=610.12,
        upper_bound=610.14,
        unit=client.get(EntityId("Q828224")))

def test_quantity_unitless_value(fx_quantity_unitless: Quantity):
    assert fx_quantity_unitless.amount == 12
    assert fx_quantity_unitless.lower_bound is None
    assert fx_quantity_unitless.upper_bound is None
    assert fx_quantity_unitless.unit is None

def test_quantity_unitless_repr(fx_quantity_unitless: Quantity):
    assert (repr(fx_quantity_unitless) ==
            ("wikidata.quantity.Quantity(12, None, None, None)"))

def test_quantity_with_unit_value(fx_quantity_with_unit: Quantity):
    assert fx_quantity_with_unit.amount == 610.13
    assert fx_quantity_with_unit.lower_bound == 610.12
    assert fx_quantity_with_unit.upper_bound == 610.14
    assert isinstance(fx_quantity_with_unit.unit, Entity)
    assert fx_quantity_with_unit.unit.id == "Q828224"

def test_quantity_with_unit_repr(fx_quantity_with_unit: Quantity):
    assert (repr(fx_quantity_with_unit) ==
            ("wikidata.quantity.Quantity(610.13, 610.12, 610.14, "
             "<wikidata.entity.Entity Q828224>)"))


return Quantity(
        amount=610.13,
        lower_bound=610.12,
        upper_bound=610.14,
        unit=client.get(EntityId("Q828224")))

from pytest import fixture

from wikidata.client import Client
from wikidata.entity import Entity, EntityId
from wikidata.quantity import Quantity

@fixture
def fx_quantity_unitless() -> Quantity:
    # From Q605704 (dozen)
    return Quantity(
        amount=12,
        lower_bound=None,
        upper_bound=None,
        unit=None)

@fixture
def fx_quantity_with_unit() -> Quantity:
    client = Client()
    # From Q520


from pytest import fixture

from wikidata.client import Client
from wikidata.entity import Entity, EntityId
from wikidata.quantity import Quantity

@fixture
def fx_quantity_unitless() -> Quantity:
    # From Q605704 (dozen)
    return Quantity(
        amount=12,
        lower_bound=None,
        upper_bound=None,
        unit=None)

@fixture
def fx_quantity_with_unit() -> Quantity:
    client = Client()
    # From Q520
    return Quantity(
        amount=610.13,
        lower_bound=610.12,
        upper_bound=610.14,
        unit=client.get(EntityId("Q828224")))

def test_quantity_unitless_value(fx_quantity_unitless: Quantity):
    assert fx_quantity_unitless.amount == 12
    assert fx_quantity_unitless.lower_bound is None
    assert fx_quantity_unitless

    assert fx_quantity_unitless.unit is None

def test_quantity_unitless_repr(fx_quantity_unitless: Quantity):
    assert (repr(fx_quantity_unitless) ==
            ("wikidata.quantity.Quantity(12, None, None, None)"))

def test_quantity_with_unit_value(fx_quantity_with_unit: Quantity):
    assert fx_quantity_with_unit.amount == 610.13
    assert fx_quantity_with_unit.lower_bound == 610.12
    assert fx_quantity_with_unit.upper_bound == 610.14
    assert isinstance(fx_quantity_with_unit.unit, Entity)
    assert fx_quantity_with_unit.unit.id == "Q828224"

def test_quantity_with_unit_repr(fx_quantity_with_unit: Quantity):
    assert (repr(fx_quantity_with_unit) ==
            ("wikidata.quantity.Quantity(610.13, 610.12, 610.14, "
             "<wikidata.entity.Entity Q828224>)"))


def google_search(search_term, api_key, cse_id, **kwargs):

import os
import sys
import glob
import time
from subprocess import check_output
from collections import OrderedDict

from googleapiclient.discovery import build

from leviathan_config import GOOGLE_API_KEY, GOOGLE_CSE_ID, USER_AGENT, BASE_DIR
from utils import id_generator, timeout, printProgressBar

service = build("customsearch", "v1", developerKey=api_key)
    res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
    return res['items']

def link_extract(query, number):
    try:
        print "Extracting URLs from Google for following dork: " + query
        discovery_id = id_generator()
        results = google_search(query, GOOGLE_API_KEY, GOOGLE_CSE_ID, num=number)
        i = 0
        l = len(results)
        for result in results:
            printProgressBar(i, l, prefix='Progress:', suffix='Complete', length=50)
            filename = os.path.join(BASE_DIR, 'assets', 'discovered', 'google_web_' + str(discovery_id) + '.txt')
            with open(filename, "a") as links:
                links.write(result['link'])
                links.write("\n")
            i += 1
            sys.stdout.write(printProgressBar(i, l, prefix='Progress:', suffix='Complete', length=50))
            time.sleep(0.1)
            sys.stdout.flush()
        print "\nFinished"
    except Exception as e:
        print "Link extraction failed! Probably your API limit exceeded"
        print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e), e)

def sqli_scan(discovery_id):
    discovered_file = os.path.join(BASE_DIR, 'assets', 'discovered', 'google_web_' + str(discovery_id) + '.txt')
    output_file = os.path.join(BASE_DIR, 'assets', 'compromised', 'google_web_%s.txt' % discovery_id)
    try:
        with open(discovered_file, "r") as urls_file:
            url_list = urls_file.readlines()
            for url in url_list:
                try:
                    sqli_scan_by_url(url, output_file)
                except KeyboardInterrupt:
                    break
                except:
                    continue

    except IOError:
        print "There is no such file: %s" % discovered_file
    print "\n"
    print "Finished. Returning back.."
    time.sleep(5)

@timeout()
def sqli_scan_by_url(url, output_file):
    print "Trying: " + url
    output = check_output(['python', 'dsss.py', '-u', url.rstrip(), '--user-agent', USER_AGENT],
                          cwd=BASE_DIR + '/lib')
    lines = output.split('\n')
    unique_lines = OrderedDict.fromkeys((line for line in lines if line))
    for line in unique_lines:
        try:
            print "**SQLi Found: " + line
            print "\n"
            with open(output_file, "a") as cracked:
                cracked.write(line)
                cracked.write("\n")
        except IOError:
            print "There is no such file: %s" % output_file

def sqli_scan_all():
    discovered_files_reg = os.path.join(BASE_DIR, 'assets', 'discovered', '*_web_*.txt')
    discovered_files = glob.glob(discovered_files_reg)
    for df in discovered_files:
        discoveryid = df.split("_")[2].split(".")[0]
        sqli_scan(discoveryid)


discovered_files_reg = os.path.join(BASE_DIR, 'assets', 'discovered', '*_web_*.txt')
    discovered_files = glob.glob(discovered_files_reg)
    for df in discovered_files:
        discoveryid = df.split("_")[2].split(".")[0]
        sqli_scan(discoveryid)

import os
import sys
import glob
import time
from subprocess import check_output
from collections import OrderedDict

from googleapiclient.discovery import build

from leviathan_config import GOOGLE_API_KEY, GOOGLE_CSE_ID, USER_AGENT, BASE_DIR
from utils import id_generator, timeout, printProgressBar

def google_search(search_term, api_key, cse_id, **kwargs):
    service = build("customsearch", "v1", developerKey=api_key)
    res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
    return res['items']

def link_extract(query, number):
    try:
        print "Extracting URLs from Google for following dork: " + query
        discovery_id = id_generator()
        results = google_search(query, GOOGLE_API_KEY, GOOGLE_CSE_ID, num=number)
        i = 0
        l = len(results)
        for result in results:
            printProgressBar(i, l, prefix='Progress:', suffix='Complete', length=50)
            filename = os.path.join(BASE_DIR, 'assets', 'discovered', 'google_web_' + str(discovery_id) + '.txt')
            with open(filename, "a") as links:
                links.write(result['link'])
                links.write("\n")
            i += 1
            sys.stdout.write(printProgressBar(i, l, prefix='Progress:', suffix='Complete', length=50))
            time.sleep(0.1)
            sys.stdout.flush()
        print "\nFinished"
    except Exception as e:
        print "Link extraction failed! Probably your API limit exceeded"
        print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e), e)

def sqli_scan(discovery_id):
    discovered_file = os.path.join(BASE_DIR, 'assets', 'discovered', 'google_web_' + str(discovery_id) + '.txt')
    output_file = os.path.join(BASE_DIR, 'assets', 'compromised', 'google_web_%s.txt' % discovery_id)
    try:
        with open(discovered_file, "r") as urls_file:
            url_list = urls_file.readlines()
            for url in url_list:
                try:
                    sqli_scan_by_url(url, output_file)
                except KeyboardInterrupt:
                    break
                except:
                    continue

    except IOError:
        print "There is no such file: %s" % discovered_file
    print "\n"
    print "Finished. Returning back.."
    time.sleep(5)

@timeout()
def sqli_scan_by_url(url, output_file):
    print "Trying: " + url
    output = check_output(['python', 'dsss.py', '-u', url.rstrip(), '--user-agent', USER_AGENT],
                          cwd=BASE_DIR + '/lib')
    lines = output.split('\n')
    unique_lines = OrderedDict.fromkeys((line for line in lines if line))
    for line in unique_lines:
        try:
            print "**SQLi Found: " + line
            print "\n"
            with open(output_file, "a") as cracked:
                cracked.write(line)
                cracked.write("\n")
        except IOError:
            print "There is no such file: %s" % output_file

def sqli_scan_all():