def iou_hi_hp(metric_folder):
    categories = ["jumping"]
    epsilon = '0.0'
    lbd = '1.0E-4'
    detection_types = ["train"]
    #     categories = ["aeroplane" ,"cow", "dog" ,"cat" ,"motorbike" ,"boat" , "horse" , "sofa" ,"diningtable" ,"bicycle"]

    detection_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    gr_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))

    all_positive = True
    all_instance = True

    for scale in ['90']:
        #         for tradeoff in ['0.0','0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9','1.0']:
        for tradeoff in ['0.0']:

            total_IoU = 0

            for category in categories:
                detection_res_3_tuple = [0] * 3
                gr_res_3_tuple = [0] * 3
                for typ in detection_types:
                    detection_filename = '_'.join([
                        "metric", typ, scale,
                        str(tradeoff),
                        str(epsilon),
                        str(lbd), category + '_0.txt'
                    ])
                    #                     detection_filename = 'metric_train_90_0.0_0.0_1.0E-4_jumping_5.0_10_0.txt'
                    fp = os.path.join(metric_folder, detection_filename)
                    f = open(fp)

                    total_gr = 0

                    cnt = 0
                    for line in f:
                        cnt += 1
                        score, yp, yi, hp, hi, filename_root = line.strip(
                        ).split(',')
                        width, height = 1, 1
                        phxmin, phymin, phxmax, phymax = metric_calculate.h2Coor(
                            width, height, hp, int(scale))
                        ghxmin, ghymin, ghxmax, ghymax = metric_calculate.h2Coor(
                            width, height, hi, int(scale))
                        total_IoU += metric_calculate.getIoU(
                            phxmin, phymin, phxmax, phymax, ghxmin, ghymin,
                            ghxmax, ghymax)
                    print total_IoU / cnt
def metric_file_analyse(metric_folder):
    #     categories=["jumping", "phoning", "playinginstrument", "reading", "ridingbike", "ridinghorse", "running", "takingphoto", "usingcomputer", "walking"]
    categories = ["jumping"]
    epsilon = '0.0'
    lbd = '1.0E-4'
    detection_types = ["train"]
    #     categories = ["aeroplane" ,"cow", "dog" ,"cat" ,"motorbike" ,"boat" , "horse" , "sofa" ,"diningtable" ,"bicycle"]

    detection_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    gr_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))

    all_positive = True
    all_instance = True

    for scale in ['90']:
        #         for tradeoff in ['0.0','0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9','1.0']:
        for tradeoff in ['0.0']:

            total_IoU = 0

            for category in categories:
                #     for category in ["dog"]:
                #         for tradeoff in ['0.2']:
                #             for scale in ["50"]:
                detection_res_3_tuple = [0] * 3
                gr_res_3_tuple = [0] * 3
                for typ in detection_types:

                    detection_filename = '_'.join([
                        "metric", typ, scale,
                        str(tradeoff),
                        str(epsilon),
                        str(lbd), category + '_0.txt'
                    ])
                    fp = os.path.join(metric_folder, detection_filename)
                    f = open(fp)

                    #                     f=open("/local/wangxin/results/full_stefan_gaze/glsvm_pos_neg/symil_cccpgaze_posneg_cv_single_split_test/metric/metric_val_50_1.0_0.0_0.0_1.0E-6_jumping.txt")

                    total_gr = 0

                    cnt = 0
                    for line in f:
                        score, yp, yi, hp, hi, filename_root = line.strip(
                        ).split(',')
                        #                         print score, yp, yi, hp, filename_root
                        filename_root = filename_root.split('/')[-1].strip()
                        #                         if True:
                        if yi == '1':
                            cnt += 1
                            grid_1, grid_2 = metric_calculate.h2GridCoor(
                                hp, int(scale))
                            gaze_file_root = filename_root
                            #                             ratio_file = VOC2012_OBJECT_ETLOSS+category+'/'+str(metric_calculate.convert_scale(int(scale)))+'/'+gaze_file_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                            #                             ratio_file = "/local/wangxin/Data/gaze_voc_actions_stefan/ETLoss_ratio/"+str(metric_calculate.convert_scale(int(scale))) + '/'+gaze_file_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                            #
                            #                             with open(ratio_file) as ratio_f:
                            #                                 ratio = float(ratio_f.readline().strip())
                            #                                 total_gr+=ratio

                            xml_filename_root = filename_root
                            #Obj#
                            #                             bbs = ground_truth_bb(VOC2012_TRAIN_ANNOTATIONS+xml_filename_root,category)
                            #                             bbs = ground_truth_bb_all(VOC2012_TRAIN_ANNOTATIONS+xml_filename_root)
                            #action
                            #                             bbs = ground_truth_bb_all_stefan_given_action(VOC2012_TRAIN_ANNOTATIONS+xml_filename_root,category)
                            bbs = ground_truth_bb_all_stefan(
                                VOC2012_TRAIN_ANNOTATIONS + xml_filename_root)
                            im = Image.open(VOC2012_TRAIN_IMAGES +
                                            xml_filename_root + '.jpg')
                            width, height = im.size
                            hxmin, hymin, hxmax, hymax = metric_calculate.h2Coor(
                                width, height, hp, int(scale))
                            #                             print hxmin, hymin, hxmax, hymax
                            IoU = metric_calculate.getTopIoU(
                                hxmin, hymin, hxmax, hymax, bbs)
                            #                             print filename_root, IoU, hp
                            total_IoU += IoU
                    total_IoU /= cnt
                    print category, scale, total_IoU
                    #                     total_gr /= cnt
                    detection_res_3_tuple[detection_types.index(
                        typ)] = total_IoU
#                     gr_res_3_tuple[detection_types.index(typ)] = total_gr
                detection_res[scale][tradeoff][
                    category] = detection_res_3_tuple


#                 gr_res[scale][tradeoff][category]= gr_res_3_tuple
    return detection_res, gr_res
def metric_file_analyse(metric_folder, categories):
    #     categories=["jumping", "phoning", "playinginstrument", "reading", "ridingbike", "ridinghorse", "running", "takingphoto", "usingcomputer", "walking"]
    epsilon = '0.001'
    lbd = '1.0E-4'
    detection_types = ["train", "valval", "valtest"]

    detection_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    gr_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))

    all_positive = True
    all_instance = True

    for category in categories:
        #     for category in ["dog"]:
        for tradeoff in [
                '0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8',
                '0.9', '1.0'
        ]:
            #             for scale in ["50"]:
            for scale in ['50']:
                detection_res_3_tuple = [0] * 3
                gr_res_3_tuple = [0] * 3
                for typ in detection_types:

                    detection_filename = '_'.join([
                        "metric", typ, tradeoff, scale,
                        str(epsilon),
                        str(lbd), category + '.txt'
                    ])
                    fp = os.path.join(metric_folder, str(scale),
                                      detection_filename)
                    f = open(fp)

                    total_gr = 0
                    total_IoU = 0

                    cnt = 0
                    for line in f:

                        yp, yi, hp, filename_root = line.strip().split(',')
                        filename_root = filename_root.split('/')[-1].strip()
                        if True:
                            #                         if yi=='1':
                            cnt += 1

                            grid_1, grid_2 = metric_calculate.h2GridCoor(
                                hp, int(scale))
                            gaze_file_root = filename_root
                            #                             ratio_file = VOC2012_OBJECT_ETLOSS+category+'/'+str(metric_calculate.convert_scale(int(scale)))+'/'+gaze_file_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                            #                             ratio_file = "/local/wangxin/Data/gaze_voc_actions_stefan/ETLoss_ratio/"+str(metric_calculate.convert_scale(int(scale))) + '/'+gaze_file_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                            #
                            #                             with open(ratio_file) as ratio_f:
                            #                                 ratio = float(ratio_f.readline().strip())
                            #                                 total_gr+=ratio

                            xml_filename_root = filename_root
                            bbs = ground_truth_bb_all_stefan(
                                VOC2012_TRAIN_ANNOTATIONS + xml_filename_root)
                            #                             bbs = ground_truth_bb_all(VOC2012_TRAIN_ANNOTATIONS+xml_filename_root)
                            im = Image.open(VOC2012_TRAIN_IMAGES +
                                            xml_filename_root + '.jpg')
                            width, height = im.size
                            hxmin, hymin, hxmax, hymax = metric_calculate.h2Coor(
                                width, height, hp, int(scale))
                            IoU = metric_calculate.getTopIoU(
                                hxmin, hymin, hxmax, hymax, bbs)
                            total_IoU += IoU
                    total_IoU /= cnt
                    #                     total_gr /= cnt
                    detection_res_3_tuple[detection_types.index(
                        typ)] = total_IoU
#                     gr_res_3_tuple[detection_types.index(typ)] = total_gr
                detection_res[scale][tradeoff][
                    category] = detection_res_3_tuple


#                 gr_res[scale][tradeoff][category]= gr_res_3_tuple
    return detection_res, gr_res
def metric_file_analyse_detection_positive(metric_folder, categories):

    epsilon = '0.001'
    lbd = '1.0E-4'
    detection_types = ["train", "valval", "valtest"]
    scale_cv = ["50"]
    tradeoff_cv = [
        '0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9',
        '1.0'
    ]
    only_tp = True
    if data_typ == "ferrari":
        etloss_root = VOC2012_OBJECT_ETLOSS
    elif data_typ == "stefan:":
        etloss_root = VOC2012_ACTION_ETLOSS

    detection_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    gr_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    for category in categories:
        #     for category in ["dog"]:
        for tradeoff in tradeoff_cv:
            #             for scale in ["50"]:
            for scale in scale_cv:
                detection_res_3_tuple = [0] * 3
                gr_res_3_tuple = [0] * 3
                for typ in detection_types:

                    detection_filename = '_'.join([
                        "metric", typ, tradeoff, scale,
                        str(epsilon),
                        str(lbd), category + '.txt'
                    ])
                    fp = os.path.join(metric_folder, str(scale),
                                      detection_filename)
                    f = open(fp)

                    total_gr = 0
                    total_IoU = 0

                    cnt = 0
                    for line in f:

                        yp, yi, hp, filename_root = line.strip().split(',')
                        filename_root = filename_root.split('/')[-1].strip()
                        if yi == '1' and (only_tp and yp == '1'):
                            cnt += 1
                            grid_1, grid_2 = metric_calculate.h2GridCoor(
                                hp, int(scale))
                            gaze_file_root = filename_root
                            ratio_file = etloss_root + category + '/' + str(
                                metric_calculate.convert_scale(int(scale))
                            ) + '/' + gaze_file_root + '_' + str(
                                grid_1) + '_' + str(grid_2) + '.txt'

                            with open(ratio_file) as ratio_f:
                                ratio = float(ratio_f.readline().strip())
                                total_gr += ratio

                            xml_filename_root = filename_root
                            bbs = ground_truth_bb_all_stefan(
                                VOC2012_TRAIN_ANNOTATIONS + xml_filename_root)
                            #                             bbs = ground_truth_bb_all(VOC2012_TRAIN_ANNOTATIONS+xml_filename_root)
                            im = Image.open(VOC2012_TRAIN_IMAGES +
                                            xml_filename_root + '.jpg')
                            width, height = im.size
                            hxmin, hymin, hxmax, hymax = metric_calculate.h2Coor(
                                width, height, hp, int(scale))
                            IoU = metric_calculate.getTopIoU(
                                hxmin, hymin, hxmax, hymax, bbs)
                            total_IoU += IoU
                    total_IoU /= cnt
                    total_gr /= cnt
                    detection_res_3_tuple[detection_types.index(
                        typ)] = total_IoU
                    gr_res_3_tuple[detection_types.index(typ)] = total_gr
                detection_res[scale][tradeoff][
                    category] = detection_res_3_tuple
                gr_res[scale][tradeoff][category] = gr_res_3_tuple
    return detection_res, gr_res
def vis_region_proposals(fixation_path, lsvm_region_dict, glsvm_region_dict):

    for root, dirs, files in os.walk(fixation_path):
        for file in files:
            cls, year, id = file.split('_')
            if cls != 'dog':
                continue

#             year='2011'
#             id='001642.ggg'
            scale = 6
            id = id[:-4]
            filename_root = '_'.join([year, id])
            fixation = []
            f = open(root + cls + '_' + year + '_' + id + '.txt')
            for line in f:
                x, y = line.strip().split(',')
                fixation.append([int(x), int(y)])
            f.close()

            img = VOC2012_TRAIN_IMAGES + year + '_' + id + '.jpg'
            img = cv2.imread(img, -1)
            rows = img.shape[0]
            cols = img.shape[1]
            out = np.zeros((rows, cols, 3), dtype='uint8')
            out = np.dstack([img])
            for x, y in fixation:
                cv2.circle(out, (x, y), 3, [255, 255, 0], 2)
            try:
                lhxmin, lhymin, lhxmax, lhymax = metric_calculate.h2Coor(
                    cols, rows, lsvm_region_dict[filename_root], 30)
                ghxmin, ghymin, ghxmax, ghymax = metric_calculate.h2Coor(
                    cols, rows, glsvm_region_dict[filename_root], 30)
            except KeyError:
                continue
            lstart = (int(lhxmin), int(lhymin))
            lend = (int(lhxmax), int(lhymax))
            gstart = (int(ghxmin), int(ghymin))
            gend = (int(ghxmax), int(ghymax))
            cv2.rectangle(out, lstart, lend, (0, 0, 255), thickness=3)
            #             cv2.putText(out, 'lsvm', lstart, cv2.FONT_HERSHEY_SIMPLEX,0.5,(100,0,100),1)
            #             cv2.putText(out, 'glsvm', gstart, cv2.FONT_HERSHEY_SIMPLEX,0.5,(100,0,100),1)

            cv2.rectangle(out, gstart, gend, (255, 0, 0), thickness=3)

            bbs = ground_truth_bb(VOC2012_TRAIN_ANNOTATIONS + filename_root,
                                  cls)
            for xmin, ymin, xmax, ymax in bbs:
                cv2.rectangle(out, (xmin, ymin), (xmax, ymax), (0, 255, 255),
                              thickness=3)


#             if scale ==1:
#                 gaze_ratio_file = open(VOC2012_OBJECT_ETLOSS+cls+'/'+str(scale*scale)+'/'+cls+'_'+filename_root+'.txt')
#             else:
#                 gaze_ratio_file = open(VOC2012_OBJECT_ETLOSS+cls+'/'+str(scale*scale)+'/'+cls+'_'+filename_root+'_'+str(sy)+'_'+str(sx)+'.txt')
#
#
#             gaze_ratio=gaze_ratio_file.readline().strip()
#             gaze_ratio_file.close()
#
#             cv2.putText(out, str(gaze_ratio[:4])+','+str(sy*scale+sx),\
#                         (int(0.5*(start[0]+end[0])),int(0.5*(start[1]+end[1]))),\
#                         cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,0,255),2
#                         )
            cv2.imshow(cls + ' ' + filename_root, out)

            k = cv2.waitKey(0)
            #space to next image
            cv2.destroyAllWindows()
            if k == 1048608:
                continue
            else:
                continue
def vis_region_proposals(fixation_path, lsvm_region_dict, glsvm_region_dict):

    for root, dirs, files in os.walk(fixation_path):

        for file in files:
            class_index = -1
            filename_root = file[:-5]  #-5 because of .json
            fixations = json.load(open(fixation_path + file)).values()

            xmin, ymin, xmax, ymax, cls = ground_truth_bb(
                VOC2012_TRAIN_ANNOTATIONS + filename_root)
            image_path = (VOC2012_TRAIN_IMAGES + filename_root) + '.jpg'
            image_res_x, image_res_y = Image.open(image_path).size
            if cls != 'ridinghorse':
                continue


#             year='2011'
#             id='001642.ggg'
            scale = 8

            img = VOC2012_TRAIN_IMAGES + filename_root + '.jpg'

            img = cv2.imread(img, -1)
            rows = img.shape[0]
            cols = img.shape[1]
            out = np.zeros((rows, cols, 3), dtype='uint8')
            out = np.dstack([img])
            colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']

            #             for cnt, subj in enumerate(fixations):
            #                 for fix in subj:
            #                     cv2.circle(out, (fix[0],fix[1]), 3, [255,255,0], 2)
            for fix in fixations[0]:
                cv2.circle(out, (fix[0], fix[1]), 1, [255, 255, 0], 2)

            try:
                lhxmin, lhymin, lhxmax, lhymax = metric_calculate.h2Coor(
                    cols, rows, lsvm_region_dict[filename_root], 30)
                ghxmin, ghymin, ghxmax, ghymax = metric_calculate.h2Coor(
                    cols, rows, glsvm_region_dict[filename_root], 30)

            except KeyError:
                continue

            lstart = (int(lhxmin), int(lhymin))
            lend = (int(lhxmax), int(lhymax))
            gstart = (int(ghxmin), int(ghymin))
            gend = (int(ghxmax), int(ghymax))
            cv2.rectangle(out, lstart, lend, (0, 0, 255), thickness=3)
            #             cv2.putText(out, 'lsvm', lstart, cv2.FONT_HERSHEY_SIMPLEX,0.5,(100,0,100),1)
            #             cv2.putText(out, 'glsvm', gstart, cv2.FONT_HERSHEY_SIMPLEX,0.5,(100,0,100),1)

            cv2.rectangle(out, gstart, gend, (255, 0, 0), thickness=3)

            bbs = ground_truth_bb(VOC2012_TRAIN_ANNOTATIONS + filename_root)
            print bbs
            xmin, ymin, xmax, ymax, cls = bbs
            cv2.rectangle(out, (xmin, ymin), (xmax, ymax), (0, 255, 255),
                          thickness=3)

            #             if scale ==1:
            #                 gaze_ratio_file = open(VOC2012_OBJECT_ETLOSS+cls+'/'+str(scale*scale)+'/'+cls+'_'+filename_root+'.txt')
            #             else:
            #                 gaze_ratio_file = open(VOC2012_OBJECT_ETLOSS+cls+'/'+str(scale*scale)+'/'+cls+'_'+filename_root+'_'+str(sy)+'_'+str(sx)+'.txt')
            #
            #
            #             gaze_ratio=gaze_ratio_file.readline().strip()
            #             gaze_ratio_file.close()
            #
            #             cv2.putText(out, str(gaze_ratio[:4])+','+str(sy*scale+sx),\
            #                         (int(0.5*(start[0]+end[0])),int(0.5*(start[1]+end[1]))),\
            #                         cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,0,255),2
            #                         )
            cv2.imshow(cls + ' ' + filename_root, out)

            k = cv2.waitKey(0)
            #space to next image
            cv2.destroyAllWindows()
            if k == 1048608:
                continue
            else:
                continue
def metric_file_analyse(metric_folder, typ):
    epsilon = '0.0'
    lbd = '1.0E-4'
    action_categories = [
        "jumping", "phoning", "playinginstrument", "reading", "ridingbike",
        "ridinghorse", "running", "takingphoto", "usingcomputer", "walking"
    ]

    detection_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    gr_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))

    for category in action_categories:
        for tradeoff in ['0.0', '0.1', '0.5', '1.0', '10.0', '100.0']:
            for scale in ["50"]:

                detection_filename = '_'.join([
                    "metric", typ, scale, tradeoff,
                    str(epsilon),
                    str(lbd), category + '_0.txt'
                ])
                fp = os.path.join(metric_folder, detection_filename)
                f = open(fp)

                total_gr = 0
                total_IoU_positive = 0

                total_IoU_negative = 0
                cnt = 0
                tn = 0
                tp = 1
                for line in f:
                    score, yp, yi, hp, image_path = line.strip().split(',')
                    grid_1, grid_2 = metric_calculate.h2GridCoor(
                        hp, int(scale))
                    filename_root = image_path.split("/")[-1].split('.')[0]

                    if yi == '1' and yp == '1':
                        tp += 1
                        cnt += 1
                        bbs = ground_truth_bb_all_action(
                            VOC2012_TRAIN_ANNOTATIONS + filename_root)

                        #                         ratio_file = VOC2012_ACTION_ETLOSS_ACTION+category+'/'+str(metric_calculate.convert_scale(scale))+'/'+filename_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                        #                         ratio_f = open(ratio_file)
                        #                         ratio = float(ratio_f.readline().strip())
                        #                         ratio_f.close()

                        #                         fixation_ratio+=ratio
                        im = Image.open(VOC2012_TRAIN_IMAGES + filename_root +
                                        '.jpg')
                        width, height = im.size
                        hxmin, hymin, hxmax, hymax = metric_calculate.h2Coor(
                            width, height, hp, int(scale))
                        IoU = metric_calculate.getTopIoU(
                            hxmin, hymin, hxmax, hymax, bbs)
                        total_IoU_positive += IoU
                    if yi == '-1' and yp == '-1':
                        tn += 1
                        cnt += 1
                        bbs = ground_truth_bb_all_action(
                            VOC2012_TRAIN_ANNOTATIONS + filename_root)

                        #                         ratio_file = VOC2012_ACTION_ETLOSS_ACTION+category+'/'+str(metric_calculate.convert_scale(scale))+'/'+filename_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                        #                         ratio_f = open(ratio_file)
                        #                         ratio = float(ratio_f.readline().strip())
                        #                         ratio_f.close()

                        #                         fixation_ratio+=ratio
                        im = Image.open(VOC2012_TRAIN_IMAGES + filename_root +
                                        '.jpg')
                        width, height = im.size
                        hxmin, hymin, hxmax, hymax = metric_calculate.h2Coor(
                            width, height, hp, int(scale))
                        IoU = metric_calculate.getTopIoU(
                            hxmin, hymin, hxmax, hymax, bbs)
                        total_IoU_negative += IoU
                print "tp:%d, scale:%s, tradeoff:%s, totalIOU:%s" % (
                    tp, scale, tradeoff, total_IoU_positive / tp)
                print "tn:%d, scale:%s, tradeoff:%s, totalIOU:%s" % (
                    tn, scale, tradeoff, total_IoU_negative / tn)