def read_dataset_random(path=None, number=1, verbosity=0, vid_list=None):
    """reads "number" different random .mat files present at the specified path. Note that those .mat files MUST be created using
    the create_dataset method
    :param verbosity: setting this parameter to 1 will make the method print the number of .mat files read
    every time it reads one
    :param path: path where the .mat files will be looked for. If left to its default value of None, the default path
    /resources/hands_bounding_dataset/hands_rgbd_transformed folder will be used
    :param number: number of elements to read
    :param vid_list: list of videos from which samples will be taken
    """
    if path is None:
        basedir = resources_path(
            os.path.join("hands_bounding_dataset", "egohands_tranformed"))
    else:
        basedir = path
    samples = os.listdir(basedir)
    if vid_list is not None:
        samples = [s for s in samples if __matches(s, vid_list)]
    tot = len(samples)
    if number > tot:
        raise ValueError("number must be smaller than the number of samples")
    random.shuffle(samples)
    samples = samples[:number]
    frames = []
    heatmaps = []
    iterator = trange(number, file=sys.stdout,
                      unit='frms') if verbosity == 1 else range(number)
    for i in iterator:
        realpath = os.path.join(basedir, samples[i])
        readcuts, readheats = __read_frame(realpath)
        frames.append(readcuts)
        heatmaps.append(readheats)
    return frames, heatmaps
def read_dataset(path=None, verbosity=0, leave_out=None):
    """reads the .mat files present at the specified path. Note that those .mat files MUST be created using
    the create_dataset method
    :param verbosity: setting this parameter to True will make the method print the number of .mat files read
    every time it reads one
    :param path: path where the .mat files will be looked for. If left to its default value of None, the default path
    /resources/hands_bounding_dataset/hands_rgbd_transformed folder will be used
    :param leave_out: list of videos whose elements will be put in the test set. Note that is this parameter is not
    provided, only 2 arrays will be returned (frames, heatmaps). If this is provided, 4 arrays are returned
    (frames, heatmaps, test_frames, test_heatmaps)
    """
    if path is None:
        basedir = resources_path(
            os.path.join("hands_bounding_dataset", "egohands_tranformed"))
    else:
        basedir = path
    samples = os.listdir(basedir)
    i = 0
    tot = len(samples)
    frames = []
    heatmaps = []
    t_frames = []
    t_heatmaps = []
    for name in samples:
        if verbosity == 1:
            print("Reading image: ", i, " of ", tot)
            i += 1
        realpath = os.path.join(basedir, name)
        readframes, readheats = __read_frame(realpath)
        if leave_out is None or not __matches(name, leave_out):
            frames.append(readframes)
            heatmaps.append(readheats)
        else:
            t_frames.append(readframes)
            t_heatmaps.append(readheats)
    if leave_out is None:
        return frames, heatmaps
    return frames, heatmaps, t_frames, t_heatmaps
Exemplo n.º 3
0
def showimages(images):
    """being images an array of images, displays them all"""
    for image in images:
        showimage(image)


def showimage(image):
    """displays a single image"""
    mplt.figure()
    mplt.imshow(image)
    mplt.show()


if __name__ == '__main__':
    imagep = resources_path("hands_bounding_dataset", "hands_dataset", "train",
                            "images", "Poselet_186.jpg")
    matp = resources_path("hands_bounding_dataset", "hands_dataset", "train",
                          "annotations", "Poselet_186.mat")
    image1 = read_image(imagep)
    showimage(image1)
    # showimages(cropimage(imagep, matp))
    heatmap1 = get_heatmap_from_mat(image1, matp)
    showimage(heatmap_to_rgb(heatmap1))

    # showimages(get_crops_from_heatmap(image1, heatmap1, enlarge=0.2))


    def timetest():
        get_crops_from_heatmap(image1, heatmap1, precision=0.7)

    print(time.timeit(stmt=timetest, number=1))
def default_test_annotations_path():
    return resources_path("hands_bounding_dataset", "hands_dataset", "test", "annotations")
def default_test_images_path():
    return resources_path("hands_bounding_dataset", "hands_dataset", "test", "images")
def create_dataset(videos_list=None,
                   savepath=None,
                   resize_rate=1.0,
                   heigth_shrink_rate=10,
                   width_shrink_rate=10,
                   im_reg=reg.Regularizer(),
                   heat_reg=reg.Regularizer()):
    """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(s)
    THE PROCESS:
        - image is resized
        - heatmap is produced with dimensions resized w.r.t. the resized image
        - regularizers are applied (BE CAREFUL WITH REGULARIZERS, YOU MAY ALTER DIMENSION RATIOS BETWEEN IMAGES AND
            HEATMAPS)
    :param width_shrink_rate: shrink rate of heatmaps width wrt the resized image
    :param heigth_shrink_rate: shrink rate of heatmaps height wrt the resized image
    :param resize_rate: resize rate of the images (1 (default) is the original image)
    :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/egohands_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 im_reg: object used to regularize the images
    :param heat_reg: object used to regularize the heatmaps"""
    # approximation_ratio = resize_rate / max(heigth_shrink_rate, width_shrink_rate)
    approximation_ratio = 0.05
    if savepath is None:
        basedir = resources_path(
            os.path.join("hands_bounding_dataset", "egohands_tranformed"))
    else:
        basedir = savepath
    if not os.path.exists(basedir):
        os.makedirs(basedir)
    framesdir = resources_path(
        os.path.join("hands_bounding_dataset", "egohands"))
    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(vids):
        frames, labels = __load_egohand_video(
            os.path.join(framesdir, vid),
            one_out_of=int(min(width_shrink_rate, heigth_shrink_rate)) /
            resize_rate)
        fr_num = len(frames)
        for i in tqdm(range(0, fr_num)):
            fr_to_save = {}
            frame = frames[i]
            frame = imresize(frame, [480, 640])
            frame = imresize(frame, resize_rate)
            frame = __add_padding(
                frame, frame.shape[1] -
                (frame.shape[1] // width_shrink_rate) * width_shrink_rate,
                frame.shape[0] -
                (frame.shape[0] // heigth_shrink_rate) * heigth_shrink_rate)
            heat = __create_ego_heatmap(frame, labels[i], heigth_shrink_rate,
                                        width_shrink_rate, resize_rate,
                                        approximation_ratio)
            frame = im_reg.apply(frame)
            heat = heat_reg.apply(heat)
            fr_to_save['frame'] = frame
            fr_to_save['heatmap'] = __heatmap_to_uint8(heat)
            path = os.path.join(basedir, vid + "_" + str(i))
            scio.savemat(path, fr_to_save)
def default_train_annotations_path():
    return resources_path(
        os.path.join("hands_bounding_dataset", "egohands_squares",
                     "annotations"))
            pass
        try:
            while True:
                l.remove([])
        except ValueError:
            pass
    return labels


def __heatmap_to_uint8(heat):
    heat = heat * 255
    heat.astype(np.uint8, copy=False)
    return heat


def __heatmap_uint8_to_float32(heat):
    heat.astype(np.float32, copy=False)
    heat = heat / 255
    return heat


if __name__ == '__main__':
    create_dataset(videos_list=[
        'CARDS_COURTYARD_B_T', 'CARDS_COURTYARD_S_H', 'CARDS_LIVINGROOM_H_S',
        'CARDS_LIVINGROOM_T_B', 'CHESS_OFFICE_B_S', 'JENGA_OFFICE_T_H'
    ],
                   savepath=resources_path("datasets/dataset_for_report2"),
                   resize_rate=0.5,
                   width_shrink_rate=1,
                   heigth_shrink_rate=1)