Exemple #1
0
def create_dataset(videos_list=None,
                   savepath=joints_path(),
                   im_regularizer=reg.Regularizer(),
                   heat_regularizer=reg.Regularizer(),
                   fillgaps=False,
                   cross_radius=3,
                   enlarge=0.2,
                   shade=False):
    """reads the videos specified as parameter and for each frame produces and saves a .mat file containing
    the frame, the corresponding heatmap indicating the position of the hand and the modified depth.
    :param fillgaps: set to True to also get interpolated frames
    :param im_regularizer: object used to regularize the images
    :param heat_regularizer: object used to regularize the heatmaps
    :param savepath: path of the folder where the produces .mat files will be saved. If left to the default value None,
    the /resources/hands_bounding_dataset/hands_rgbd_transformed folder will be used
    :param videos_list: list of videos you need the .mat files of. If left to the default value None, all videos will
    be exploited
    :param cross_radius: radius of the crosses of the heatmaps
    :param enlarge: crops enlarge factor
    :param shade: set to true to shade the pixels that identify a junction in a heatmap according to their
    distance with the center (real position of junction"""
    if savepath is None:
        basedir = joints_path()
    else:
        basedir = savepath
    if not os.path.exists(basedir):
        os.makedirs(basedir)
    framesdir = resources_path("framedata")
    if videos_list is None:
        vids = os.listdir(framesdir)
        vids = [x for x in vids if os.path.isdir(os.path.join(framesdir, x))]
    else:
        vids = videos_list
    for vid in tqdm.tqdm(vids):
        frames, labels = load_labelled_videos(vid, fillgaps=fillgaps)
        # depths, _ = load_labelled_videos(vid, getdepth=True, fillgaps=fillgaps)
        fr_num = frames.shape[0]
        for i in tqdm.tqdm(range(0, fr_num)):
            if labels[i] is not None:
                try:
                    frame = frames[i]
                    label = labels[i][:, 0:2]
                    visible = labels[i][:, 2:3]
                    label *= [frame.shape[1], frame.shape[0]]
                    label = np.array(label, dtype=np.int32).tolist()
                    label = [[p[1], p[0]] for p in label]
                    coords = __get_coord_from_labels(label)
                    cut = u.crop_from_coords(frame, coords, enlarge)
                    heatmaps = __create_21_heatmaps(label, coords,
                                                    np.shape(frame),
                                                    cross_radius, enlarge,
                                                    shade)
                    cut = im_regularizer.apply(cut)
                    heatmaps = heat_regularizer.apply_on_batch(heatmaps)
                    # heatmaps = __heatmaps_dim_reducer(heatmaps)
                    heatmaps = __stack_heatmaps(heatmaps)
                    path = os.path.join(basedir, vid + "_" + str(i))
                    __persist_frame(path, cut, heatmaps, visible)
                except ValueError as e:
                    print("Error " + e + " on vid " + vid + str(i))
Exemple #2
0
def create_dataset(videos_list=None,
                   savepath=None,
                   im_regularizer=reg.Regularizer(),
                   fillgaps=False,
                   enlarge=0.5):
    """reads the videos specified as parameter and for each frame produces and saves a .mat file containing
    the frame, the corresponding heatmap indicating the position of the hand and the modified depth.
    :param fillgaps: set to True to also get interpolated frames
    :param im_regularizer: object used to regularize the cuts
    :param savepath: path of the folder where the produces .mat files will be saved. If left to the default value None,
    the /resources/hands_bounding_dataset/hands_rgbd_transformed folder will be used
    :param videos_list: list of videos you need the .mat files of. If left to the default value None, all videos will
    be exploited
    :param enlarge: crops enlarge factor"""
    if savepath is None:
        basedir = resources_path("palm_back_classification_dataset")
    else:
        basedir = savepath
    if not os.path.exists(basedir):
        os.makedirs(basedir)
    framesdir = resources_path("framedata")
    if videos_list is None:
        vids = os.listdir(framesdir)
        vids = [x for x in vids if os.path.isdir(os.path.join(framesdir, x))]
    else:
        vids = videos_list
    for vid in tqdm.tqdm(vids):
        frames, labels = load_labelled_videos(vid, fillgaps=fillgaps)
        fr_num = frames.shape[0]
        lr = get_right_left(vid)
        if lr == LEFT:
            lr = -1
        for i in tqdm.tqdm(range(0, fr_num)):
            if labels[i] is not None:
                try:
                    frame = frames[i]
                    label = labels[i][:, 0:2]

                    # conf is a real in [-1.0, 1.0] such that -1.0 is full back, +1.0 is full palm
                    # middle values express partial confidence, but all info is in one single value
                    conf = pb.leftright_to_palmback(
                        hand=hand_format(label),
                        side=pb.RIGHT if lr == RIGHT else pb.LEFT)
                    # if you want the crisp result, there it is:
                    result = PALM if conf >= 0 else BACK
                    if result < 0:
                        result = 0
                    # and the confidence on that result is in [0..1]:
                    conf = abs(conf)

                    label *= [frame.shape[1], frame.shape[0]]
                    label = np.array(label, dtype=np.int32).tolist()
                    label = [[p[1], p[0]] for p in label]
                    coords = __get_coord_from_labels(label)
                    cut = u.crop_from_coords(frame, coords, enlarge)
                    cut = im_regularizer.apply(cut)
                    path = os.path.join(basedir, vid + "_" + str(i))
                    __persist_frame(path, cut, result, conf)
                except ValueError as e:
                    print("Error " + str(e) + " on vid " + vid + str(i))
Exemple #3
0
def create_dataset(videos_list=None,
                   savepath=None,
                   im_regularizer=reg.Regularizer(),
                   fillgaps=False,
                   enlarge=0.5,
                   data_augment=False):
    """reads the videos specified as parameter and for each frame produces and saves a .mat file containing
    the frame, the corresponding heatmap indicating the position of the hand and the modified depth.
    :param fillgaps: set to True to also get interpolated frames
    :param im_regularizer: object used to regularize the cuts
    :param savepath: path of the folder where the produces .mat files will be saved. If left to the default value None,
    the /resources/hands_bounding_dataset/hands_rgbd_transformed folder will be used
    :param videos_list: list of videos you need the .mat files of. If left to the default value None, all videos will
    be exploited
    :param enlarge: crops enlarge factor"""
    if savepath is None:
        basedir = resources_path("left_right_classification_dataset")
    else:
        basedir = savepath
    if not os.path.exists(basedir):
        os.makedirs(basedir)
    framesdir = resources_path("framedata")
    if videos_list is None:
        vids = os.listdir(framesdir)
        vids = [x for x in vids if os.path.isdir(os.path.join(framesdir, x))]
    else:
        vids = videos_list
    for vid in tqdm.tqdm(vids):
        frames, labels = load_labelled_videos(vid, fillgaps=fillgaps)
        fr_num = frames.shape[0]
        result = get_right_left(vid)
        for i in tqdm.tqdm(range(0, fr_num)):
            if labels[i] is not None:
                try:
                    frame = frames[i]
                    label = labels[i][:, 0:2]
                    label *= [frame.shape[1], frame.shape[0]]
                    label = np.array(label, dtype=np.int32).tolist()
                    label = [[p[1], p[0]] for p in label]
                    coords = __get_coord_from_labels(label)
                    cut = u.crop_from_coords(frame, coords, enlarge)
                    cut = im_regularizer.apply(cut)
                    path = os.path.join(basedir, vid + "_" + str(i))
                    __persist_frame(path, cut, result)
                    if data_augment:
                        path = os.path.join(basedir, vid + "_t_" + str(i))
                        transp = cut.squeeze().transpose()
                        transp = transp.reshape(
                            [transp.shape[0], transp.shape[1], 1])
                        __persist_frame(path, transp, 1 - result)
                except ValueError as e:
                    print("Error " + str(e) + " on vid " + vid + str(i))
Exemple #4
0
def __create_21_heatmaps(label,
                         coords_for_cut,
                         original_shape,
                         cross_radius,
                         enlarge,
                         shade=False):
    heatmaps = []
    for l in label:
        heat = np.zeros([original_shape[0], original_shape[1]])
        heat = __set_cross(heat, l, cross_radius, shade)
        heat = u.crop_from_coords(heat, coords_for_cut, enlarge)
        heatmaps.append(heat)
    return np.array(heatmaps)