예제 #1
0
def load_pose_data(json_filename, video_dir):
    print('Loading pose data...')

    track = {}

    with open(json_filename, 'r') as f:
        pose_data = json.load(f)

        for single_pose_data in tqdm(pose_data):
            # get image id
            image_id = single_pose_data['image_id']
            # make new dict if image not exist
            if image_id not in track.keys():
                track[image_id] = {}

            # get number of boxes
            number_of_boxes = len(track[image_id].keys())
            # make new dict of box
            track[image_id][number_of_boxes + 1] = {}
            track[image_id][number_of_boxes +
                            1]['box_score'] = single_pose_data['score']
            track[image_id][number_of_boxes + 1]['box_pos'] = get_box(
                single_pose_data['keypoints'],
                '{}/frames/{}'.format(video_dir, image_id))
            track[image_id][number_of_boxes + 1]['box_pose_pos'] = np.array(
                single_pose_data['keypoints']).reshape(-1, 3)[:, 0:2].tolist()
            track[image_id][number_of_boxes + 1]['box_pose_score'] = np.array(
                single_pose_data['keypoints']).reshape(-1, 3)[:, -1].tolist()

        # Get number of boxes of each frame and creat a field for that data
        for image_id in track.keys():
            track[image_id]['num_boxes'] = len(track[image_id])

    print('---> Done.')
    return track
예제 #2
0
    def show_camera(self):
        flag, self.image = self.cap.read()

        if not flag:
            self.timer_video.stop()
            self.cap.release()
            self.g_binary.clear()
            self.g_events.clear()
            self.g_gray.clear()
            self.analyzer.clean()

        self.current_frame += 1

        show = cv2.resize(self.image, IMG_SIZE)
        showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0],
                                 QtGui.QImage.Format_RGB888)
        self.g_events.setPixmap(QtGui.QPixmap.fromImage(showImage))

        gray = cv2.cvtColor(show, cv2.COLOR_BGR2GRAY)
        showImage = QtGui.QImage(gray.data, gray.shape[1], gray.shape[0],
                                 QtGui.QImage.Format_Indexed8)
        self.g_gray.setPixmap(QtGui.QPixmap.fromImage(showImage))

        if self.current_frame <= self.history:
            fg_mask = self.bs.apply(self.image)
            fg_mask = np.zeros_like(fg_mask)
        else:
            fg_mask = self.bs.apply(self.image, learningRate=0.01)

        th = cv2.threshold(fg_mask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
        th = cv2.erode(th,
                       cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)),
                       iterations=2)
        dilated = cv2.dilate(th,
                             cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                                       (8, 3)),
                             iterations=2)
        image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL,
                                                 cv2.CHAIN_APPROX_SIMPLE)
        status = self.analyzer.add(image)
        self.m.update_figure(self.analyzer.ys)
        if status:
            self.l_status.setText("FALL!")
        else:
            self.l_status.setText("NORMAL")
        info = utils.get_box(image)
        if info:
            x_min, y_min, x_max, y_max = info
            image = cv2.rectangle(image, (x_min, y_min), (x_max, y_max), 255,
                                  2)
        show = cv2.resize(image, IMG_SIZE)
        showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0],
                                 QtGui.QImage.Format_Indexed8)
        self.g_binary.setPixmap(QtGui.QPixmap.fromImage(showImage))
예제 #3
0
def find_pairs(csp, removals):
    """Custom implementation of hidden pairs inference method"""
    for v1 in csp.variables:
        d1 = set(csp.curr_domains[v1])
        for v2 in csp.neighbors[v1]:
            d2 = set(csp.curr_domains[v2])
            if len(d1) == 2 and d1 == d2:
                others = []
                if same_row(v1, v2):
                    others = get_row(v1)
                elif same_col(v1, v2):
                    others = get_col(v1)
                if same_box(v1, v2):
                    others += get_box(v1)
                for v3 in set(others):
                    if v3 != v1 and v3 != v2:
                        for d in csp.curr_domains[v3]:
                            if d in d1:
                                csp.prune(v3, d, removals)
예제 #4
0
def main():
    #K parameter for map@k
    k = 10

    # Get images and denoise query set.
    print("Getting and denoising images...")
    qs = get_imgs("datasets/qst1_w5")
    db = get_imgs("datasets/DDBB")
    #gt_boxes = utils.get_pickle("datasets/qsd1_w5/frames.pkl")
    qs_denoised = [utils.denoise_image(img, "Median") for img in tqdm(qs)]

    print("Generating background masks")
    bg_masks = [utils.get_painting_mask(img, 0.1) for img in tqdm(qs)]
    frame_rectangles = [utils.get_frames_from_mask(mask) for mask in bg_masks]
    #Stan's method
    img_lines = [ag.get_all_lines(img) for img in qs]
    angles = [ag.get_horiz_angle(lines) for lines in img_lines]
    corrected_angles = [
        ag.get_GTFORMAT_angle(single_angle) for single_angle in angles
    ]
    #Marc's method
    angles_opencv = [
        utils.get_median_angle(image_rects) for image_rects in frame_rectangles
    ]
    boxes = [[utils.get_box(rectangle) for rectangle in image]
             for image in frame_rectangles]
    boxes_result = [[[angle, box] for box in image]
                    for angle, image in zip(corrected_angles, boxes)]
    print("Recovering subimages")
    qs_split = [
        utils.get_paintings_from_frames(img, rects)
        for img, rects in tqdm(zip(qs_denoised, frame_rectangles))
    ]

    if SHOW_IMGS:
        for i, img in enumerate(tqdm(qs_split)):
            for j, painting in enumerate(img):
                #s = cv.imwrite(r"outputs\0%d%d.jpg"%(i,j), painting)
                cv.imshow("I: " + str(i) + " P: " + str(j),
                          cv.resize(painting, (256, 256)))
                cv.waitKey(0)
                #print(s)

    # Get masks without background and without text box of query sets.
    print("\nGetting text bounding box masks...")
    qs_txt_infos = [[get_text_bb_info(painting) for painting in img]
                    for img in tqdm(qs_split)]
    qs_txt_masks = [[single.mask for single in qs_txt_info]
                    for qs_txt_info in qs_txt_infos]

    for qs_mask in qs_txt_masks:
        for single_mask in qs_mask:
            single_mask[single_mask < 255] = 0
            single_mask[single_mask > 255] = 255

    qs_masks = [[single_mask.astype("uint8") for single_mask in qs_mask]
                for qs_mask in qs_txt_masks]

    # Detect and describe keypoints in images.
    print("\nDetecting and describing keypoints...")
    dt_type = cv.ORB_create()
    qs_kps = [[
        detect_keypoints(dt_type, painting, painting_mask)
        for painting, painting_mask in zip(img, mask)
    ] for img, mask in zip(qs_split, qs_masks)]
    qs_dps = [[
        describe_keypoints(dt_type, painting, painting_kp)
        for painting, painting_kp in zip(img, kp)
    ] for img, kp in zip(qs_split, qs_kps)]

    db_kps = [detect_keypoints(dt_type, img) for img in tqdm(db)]
    db_dps = [
        describe_keypoints(dt_type, img, kp)
        for img, kp in tqdm(zip(db, db_kps))
    ]

    # Match images
    print("\nMatching images...")

    class Match:
        def __init__(self, summed_dist, idx):
            self.summed_dist = summed_dist
            self.idx = idx

    tops = []
    dists = []

    # For all query images
    dst_thr = 35
    for qs_dp in tqdm(qs_dps):
        # Get all descriptor matches between a query image and all database images.
        matches_s = [[
            match_descriptions(qs_single_painting_dp, db_dp)
            for qs_single_painting_dp in qs_dp
        ] for db_dp in db_dps]
        # Evaluate quality of matches
        matches_s_ev = [[
            evaluate_matches(painting_match) for painting_match in match
        ] for match in matches_s]
        # Sort for lowest
        matches_s_cl = [[
            Match(painting_summed_dist, idx)
            for painting_summed_dist in summed_dist
        ] for idx, summed_dist in enumerate(matches_s_ev)]
        partial_tops, partial_dists = utils.get_tops_from_matches(
            qs_dp, matches_s_cl, dst_thr, k)
        tops.append(partial_tops)
        dists.append(partial_dists)

    utils.dump_pickle("outputs/frames.pkl", boxes_result)
    utils.dump_pickle("outputs/result.pkl", tops)
    exit()
    comparing_with_ground_truth(tops, qs_txt_infos, k)
예제 #5
0
def main():
  
  ROOT.gROOT.SetBatch()

  limits = [ 
    ( 'ExclusionContours_2HDMTypeI_tanb_vs_cba.root',  '#bf{2HDM Type I, #it{m_{H}} = 200 GeV}',      1,   10, 26, -0.9, 0.9, 'cos(#beta-#alpha)',    utils.h_to_zz_to + utils.lepp + utils.lepm + utils.lepp + utils.lepm, None ),  
    ( 'ExclusionContours_2HDMTypeII_tanb_vs_cba.root', '#bf{2HDM Type II, #it{m_{H}} = 200 GeV}',     1,   10, 26, -0.9, 0.9, 'cos(#beta-#alpha)',    utils.h_to_zz_to + utils.lepp + utils.lepm + utils.lepp + utils.lepm, None ),  
    ( 'ExclusionContours_2HDMTypeI_tanb_vs_mH.root',   '#bf{2HDM Type I, cos(#beta-#alpha) = -0.1}',  0.5, 18, 80,     200,  400, '#it{m_{H}} [GeV]', utils.h_to_zz_to + utils.lepp + utils.lepm + utils.lepp + utils.lepm + ' + ' + utils.lepp + utils.lepm + utils.nu + utils.nubar, ( 320, 324, 31 ) ),  
    ( 'ExclusionContours_2HDMTypeII_tanb_vs_mH.root',  '#bf{2HDM Type II, cos(#beta-#alpha) = -0.1}', 0.5, 10, 35,     200,  400, '#it{m_{H}} [GeV]', utils.h_to_zz_to + utils.lepp + utils.lepm + utils.lepp + utils.lepm + ' + ' + utils.lepp + utils.lepm + utils.nu + utils.nubar, ( 320, 324, 15.6 ) ),  
  ]

  for file_name, mod_type, minY, maxY, max_maxY, minX, maxX, x_title, channel, patch_vals in limits:
    file = ROOT.TFile( 'inputs/' + file_name )
    first_base_hist = file.Get( 'h_med' ) 
    base_hist = ROOT.TH2F( first_base_hist.GetName() + '_extended', ';%s;tan#beta' % x_title, first_base_hist.GetXaxis().GetNbins(), first_base_hist.GetXaxis().GetXmin(), first_base_hist.GetXaxis().GetXmax(), 100 * first_base_hist.GetYaxis().GetNbins(), first_base_hist.GetYaxis().GetXmin(), 1000 )
    obs_name = 'obs'
    if 'tanb_vs_mH' in file_name:
      obs_name = 'obsc'
    types = [ 
      utils.graph_info( 'n2sig', file, 5, 1, 1001 ),
      utils.graph_info( 'n1sig', file, 3, 1, 1001 ),
      utils.graph_info( 'p1sig', file, 5, 1, 1001 ),
      utils.graph_info( 'p2sig', file, ROOT.kWhite, 1, 1001 ),
      #utils.graph_info( 'med', file, ROOT.kWhite, 1001 ), 
      utils.graph_info( 'medc', file, ROOT.kAzure + 2, 2 ), 
      utils.graph_info( 'obsf', file, ROOT.kOrange + 10, 1, 3844 ),
      utils.graph_info( obs_name, file, ROOT.kBlack, 1 ),
    ]
    graphs = {}
    single_graphs = {}
    for item in types:
      for index in range( item.number ):
        full_name = 'h_%s_contour_%d' % ( item.name, index )
        graphs[ full_name ] = ( utils.add_points( file.Get( full_name ), [] ), item )
        single_graphs[ item.name ] = graphs[ full_name ]
    canvas = ROOT.TCanvas( 'plot_' + file_name.replace( '.root', '' ), '', 600, 600 )
    base_hist.Draw( 'axis' )
    order = [ 'n2sig', 'n1sig', 'p1sig', 'p2sig', 'med', 'medc', 'obsf', obs_name ]
    for key1 in order:
      for key2, ( g, g_info ) in graphs.iteritems():
        if key1 in key2:
          g.SetLineColor( g_info.color )
          g.SetLineStyle( g_info.line )
          g.SetLineWidth( 2 )
          option = 'c'
          if g_info.fill:
            g.SetFillColor( g_info.color )
            g.SetFillStyle( g_info.fill )
            g.SetLineStyle( 1 )
            g.SetLineColor( ROOT.kBlack )
            option = 'fc'
            #if 'sig' in g_info.name:
            #  option = '3'
          g.Draw( option )
    base_hist.GetXaxis().SetRangeUser( minX, maxX )
    base_hist.GetYaxis().SetRangeUser( minY, max_maxY ) #utils.get_bound( minY, maxY ) )
    canvas.Update()
    the_box = utils.get_box( ROOT.gPad.GetUxmin(), ROOT.gPad.GetUxmax(), maxY, ROOT.gPad.GetUymax() )
    base_hist.Draw( 'axis same' )
    the_box.Draw( 'l' )
    x_l1, y_l1, latex1 = utils.draw_latex( utils.lumi, False )
    latex1.DrawLatex( x_l1, y_l1 - 0.105, channel )
    latex1.SetTextSize( 20 )
    #latex1.DrawLatex( x_l1, y_l1 - 0.16, '2HDM Type I, #it{m_{H}}=200 GeV' )
    latex1.SetTextAlign( 22 )
    latex1.DrawLatex( 0.535, y_l1 - 0.16, mod_type )
    xLeg = 0.62
    yLeg = 0.89
    leg = utils.create_legend_2hdm( 3 ) 
    leg.AddEntry( single_graphs[ obs_name ][ 0 ], 'Observed', 'l' )
    leg.AddEntry( single_graphs[ 'n1sig' ][ 0 ], '#pm1#sigma band', 'f' )
    leg.AddEntry( single_graphs[ 'medc' ][ 0 ], 'Expected', 'l' )
    leg.AddEntry( single_graphs[ 'n2sig' ][ 0 ], '#pm2#sigma band', 'f' )
    leg.AddEntry( None, '', '' )
    leg.AddEntry( single_graphs[ 'obsf' ][ 0 ], 'Excluded', 'f' )
    leg.Draw()
    #if patch_vals:
    #  p_x1, p_x2, p_y = patch_vals 
    #  utils.patch_bar( p_x1, p_x2, p_y, p_y ) 
    canvas.SetLogy()
    utils.save( canvas )
예제 #6
0
def main(args):

    for sub in args.sub_dir:
        path = os.path.join(args.root_dir, sub)
        print(path)
        # remove existing txt files for annotation
        # utils.remove_annot_files(path)
        count_json = 0
        count_img = 0
        for _, _, f in os.walk(os.path.join(path, 'Annotations')):
            for file in f:
                if args.file_ext in file:
                    if not file.endswith(('.json', '.xml')):
                        continue
                    # annotation_folder = Path(os.path.join(path, 'Annotation'))
                    # annotation_folder.mkdir(exist_ok=True)
                    #
                    # image_folder = Path(os.path.join(path, 'JPEGImages'))
                    # image_folder.mkdir(exist_ok=True)

                    filename = file.replace(args.file_ext, '')

                    if args.dataset_name == 'taco':
                        if (args.file_ext == '.json'):
                            filename = file.replace('.json', '')
                            taco_list = utils.get_box_taco(
                                os.path.join(path, 'Annotations'), filename)

                            for file_name, box_list in taco_list:
                                print(file_name)
                                print(file_name[-4:])
                                success = utils.cut_label_taco(
                                    path, file_name, box_list,
                                    '{}_cut'.format(sub))
                                # print(success)
                                # count_json += 1

                                if success:
                                    #     utils.move_file_to_directories(path, 'Annotations', 'Annotations_success', file)
                                    #     utils.move_file_to_directories(path, 'JPEGImages', 'JPEGImages_success', file.replace('.json', '.jpg'))
                                    count_img += 1
                                    print(count_img)
                        if (args.file_ext == '.xml'):
                            pass

                    else:
                        if (args.file_ext == '.json'):
                            filename = file.replace('.json', '')
                            box_list = utils.get_box(
                                os.path.join(path, 'Annotations'), filename)

                        if (args.file_ext == '.xml'):
                            box_list = utils.get_box_x(
                                os.path.join(path, 'Annotations'), filename,
                                args.file_ext)

                        print(box_list)
                        success = utils.cut_label_x(path, filename + '.jpg',
                                                    box_list, 'images',
                                                    '{}_cut'.format(sub))
                        print(success)
                        count_json += 1
                        if success:
                            # utils.move_file_to_directories(path, 'Annotations', 'Annotations_success', file)
                            # utils.move_file_to_directories(path, 'JPEGImages', 'JPEGImages_success', file.replace('.json', '.jpg'))
                            count_img += 1
예제 #7
0
def main():

    ROOT.gROOT.SetBatch()

    line_width = 2

    file = ROOT.TFile('inputs/graphs_llvv_graviton.root')
    exp = file.Get('median')
    exp.SetLineStyle(2)
    exp.SetLineWidth(line_width)
    exp.SetLineColor(ROOT.kBlack)
    exp.SetMarkerColor(ROOT.kBlack)
    obs = file.Get('observed')
    obs.SetLineStyle(1)
    obs.SetLineWidth(line_width)
    obs.SetLineColor(ROOT.kBlack)
    obs.SetMarkerColor(ROOT.kBlack)
    obs.SetMarkerSize(0.8)
    theory = file.Get('sm')
    #for i in range( 2 ):
    #  theory.RemovePoint( 0 )
    theory.SetLineStyle(1)
    theory.SetLineWidth(line_width)
    theory.SetLineColor(ROOT.kRed + 1)
    theory.SetMarkerColor(ROOT.kRed)
    band_1 = file.Get('1sigma')
    band_1.SetFillColor(3)
    band_1.SetFillStyle(1001)
    band_2 = file.Get('2sigma')
    band_2.SetFillColor(5)
    band_2.SetFillStyle(1001)
    canvas = ROOT.TCanvas('plot_graviton', '', 800, 600)
    band_2.Draw('af')
    #band_2.GetYaxis().SetRangeUser( 1, 100000 )
    band_2.GetYaxis().SetRangeUser(1, 30000)
    band_2.GetXaxis().SetTitle('#it{m(G_{#it{KK}})} [TeV]')
    band_2.GetYaxis().SetTitle('95% C.L. limit on ' + utils.grav_axis +
                               '  [fb]')
    band_2 = utils.re_style(band_2)
    band_1.Draw('f')
    exp.Draw('l')
    obs.Draw('pl')
    theory.Draw('l')
    band_2.GetXaxis().SetRangeUser(600, 2)
    box = utils.get_box(500, 1000, 200, 500)
    box.Draw()
    x_l1, y_l1, latex1 = utils.draw_latex(utils.lumi, False)
    latex1.DrawLatex(
        x_l1, y_l1 - 0.105,
        utils.g_to_zz_to + utils.lepp + utils.lepm + utils.nu + utils.nubar)
    latex1.DrawLatex(x_l1, y_l1 - 0.16, '#it{k/#bar{M}_{Pl}} = 1')
    xLeg = 0.62
    yLeg = 0.89
    leg = utils.create_legend_limit(5)
    leg.AddEntry(obs, 'Observed ' + utils.cls + ' limit', 'pl')
    leg.AddEntry(exp, 'Expected ' + utils.cls + ' limit', 'l')
    leg.AddEntry(band_1, 'Expected #pm 1#sigma', 'f')
    leg.AddEntry(band_2, 'Expected #pm 2#sigma', 'f')
    leg.AddEntry(theory, utils.grav_axis, 'l')
    leg.Draw()
    #utils.patch_bar( 240. / 566., 247. / 566., 329. / 407., 329. / 407., True )
    canvas.SetLogy()
    utils.save(canvas)