예제 #1
0
파일: show_result.py 프로젝트: khanha2/ren
def show_pose(dataset_model, dataset_image, base_dir, outputs, list_file,
              save_dir, is_flip, gif):
    if list_file is None:
        names = util.load_names(dataset_image)
    else:
        with open(list_file) as f:
            names = [line.strip() for line in f]
    assert len(names) == outputs.shape[0]

    for idx, (name, pose) in enumerate(zip(names, outputs)):
        img = util.load_image(dataset_image,
                              os.path.join(base_dir, name),
                              is_flip=is_flip)
        img = img.astype(np.float32)
        img = (img - img.min()) / (img.max() - img.min()) * 255
        img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        img = util.draw_pose(dataset_model, img, pose)
        cv2.imshow('result', img / 255)
        if save_dir is not None:
            cv2.imwrite(os.path.join(save_dir, '{:>06d}.png'.format(idx)), img)
        ch = cv2.waitKey(25)
        if ch == ord('q'):
            break

    if gif and save_dir is not None:
        os.system(
            'convert -loop 0 -page +0+0 -delay 25 {0}/*.png {0}/output.gif'.
            format(save_dir))
def main():
    if len(sys.argv) < 4:
        print_usage()

    dataset = sys.argv[1]
    base_dir = sys.argv[2]
    out_file = sys.argv[3]
    phase = sys.argv[4]
    names = util.load_names(dataset, phase)
    lbls = util.load_labels(dataset, phase)
    centers = []

    for idx, strin in enumerate(lbls):
        # load label data
        joints = np.asarray(np.reshape(strin.split(), (21, 3)),
                            dtype=np.float32)
        # convert label data from world coordinates to pixel locations
        joints, skel_camcoords = util.world2pixel(joints, dataset)
        # calculate centers
        c = util.get_center_fpad(joints)
        c = np.asarray(c, dtype=np.float32)
        centers.append(c.reshape((1, 3)))
        if idx % 500 == 0:
            print('{}/{}'.format(idx + 1, len(names)))

    util.save_results(centers, out_file)
예제 #3
0
def main():
    if len(sys.argv) < 3:
        print_usage()

    dataset = sys.argv[1]
    out_file = sys.argv[2]
    data_dir_dict = {
        'nyu': config.nyu_data_dir,
        'icvl': config.icvl_data_dir,
        'msra': config.msra_data_dir,
        'hands17': config.hands_data_dir
    }
    base_dir = data_dir_dict[dataset]  #sys.argv[3]
    batch_size = 64
    if len(sys.argv) == 4:
        batch_size = int(sys.argv[3])

    # generate deploy prototxt
    make_baseline_net(
        os.path.join('/home/boonyew/Documents/PoseRen', 'models/'), dataset)
    make_pose_ren_net(
        os.path.join('/home/boonyew/Documents/PoseRen', 'models/'), dataset)

    hand_model = ModelPoseREN(dataset)
    names = util.load_names(dataset)
    centers = util.load_centers(dataset)
    if dataset == 'msra':
        # the last index of frames belong to the same subject
        msra_id_split_range = np.array(
            [8499, 16991, 25403, 33891, 42391, 50888, 59385, 67883, 76375]) - 1
        results = []
        for test_id in range(9):
            hand_model.reset_model(dataset, test_id)
            sidx = msra_id_split_range[test_id - 1] + 1 if test_id else 0
            eidx = msra_id_split_range[test_id]
            sub_names = names[sidx:eidx + 1]
            sub_centers = centers[sidx:eidx + 1]
            print('evaluating for subject {} ...'.format(test_id))
            sub_results = hand_model.detect_files(base_dir,
                                                  sub_names,
                                                  sub_centers,
                                                  max_batch=batch_size)
            if test_id == 0:
                results = sub_results
            else:
                results = np.concatenate((results, sub_results), axis=0)
        util.save_results(results, out_file)
    else:
        results = hand_model.detect_files(base_dir,
                                          names,
                                          centers,
                                          max_batch=batch_size)
        util.save_results(results, out_file)
def main():
    if len(sys.argv) < 5:
        print_usage()

    dataset = sys.argv[1]
    model = sys.argv[2]
    out_file = sys.argv[3]
    base_dir = sys.argv[4]

    hand_model = HandModel(dataset, model)
    names = util.load_names(dataset)
    centers = util.load_centers(dataset)
    results = hand_model.detect_files(base_dir, names, centers)
    save_results(results, out_file)
예제 #5
0
파일: pre.py 프로젝트: ReBoRn8888/Pose-REN
def main():
    if len(sys.argv) < 3:
        print_usage()

    dataset = sys.argv[1]
    out_file = sys.argv[2]
    data_dir_dict = {'nyu': config.nyu_data_dir,
                     'icvl': config.icvl_data_dir + 'test/Depth/',
                     'msra': config.msra_data_dir}
    base_dir = data_dir_dict[dataset] #sys.argv[3]
    batch_size = 64
    if len(sys.argv) == 4:
        batch_size = int(sys.argv[3])

    # generate deploy prototxt
    make_baseline_net(os.path.join(ROOT_DIR, '../models'), dataset)
    make_pose_ren_net(os.path.join(ROOT_DIR, '../models'), dataset)

    hand_model = ModelPoseREN(dataset)
    names = util.load_names(dataset)
    centers = util.load_centers(dataset)
    # print("centers = {}".format(np.shape(centers)))
    # c = centers[0]
    # print("centers[0] = {}".format(list(c)))
    if dataset == 'msra':
        # the last index of frames belong to the same subject
        msra_id_split_range = np.array([8499, 16991, 25403, 33891, 42391, 50888, 59385, 67883, 76375]) - 1
        results = []
        for test_id in xrange(9):
            hand_model.reset_model(dataset, test_id)
            sidx = msra_id_split_range[test_id-1] + 1 if test_id else 0
            eidx = msra_id_split_range[test_id]
            sub_names = names[sidx:eidx+1]
            sub_centers= centers[sidx:eidx + 1]
            print('evaluating for subject {} ...'.format(test_id))
            sub_results = hand_model.detect_files(base_dir, sub_names, sub_centers, max_batch=batch_size)
            if test_id == 0:
                results = sub_results
            else:
                results = np.concatenate((results, sub_results), axis=0)
        util.save_results(results, out_file)
    else:
        results = hand_model.detect_files(base_dir, names, centers, max_batch=batch_size)
        print(results[0])
        img = cv2.imread("/media/reborn/Others/Study/Reborn/Github/Pose-REN/test/image_0000.png", 2).astype("float32")
        img_show = show_results(img, results[0], dataset)
        cv2.imwrite('result.jpg', img_show)
        util.save_results(results, out_file)
예제 #6
0
파일: get_centers.py 프로젝트: khanha2/ren
def main():
    if len(sys.argv) < 4:
        print_usage()

    dataset = sys.argv[1]
    base_dir = sys.argv[2]
    out_file = sys.argv[3]
    names = util.load_names(dataset)
    centers = []
    for idx, name in enumerate(names):
        if dataset == 'nyu':  # use synthetic image to compute center
            name = name.replace('depth', 'synthdepth')
        img = util.load_image(dataset, os.path.join(base_dir, name))
        if dataset == 'icvl':
            center = util.get_center(img, upper=500, lower=0)
        elif dataset == 'nyu':
            center = util.get_center(img, upper=1300, lower=500)
        elif dataset == 'msra':
            center = util.get_center(img, upper=1000, lower=10)
        centers.append(center.reshape((1, 3)))
        if idx % 500 == 0:
            print('{}/{}'.format(idx + 1, len(names)))
    util.save_results(centers, out_file)
예제 #7
0
########################
## This is the preprocessing and validation script for Depth.
## Keep the segment you would like to use and comment out the rest
## this is because there are multiple data files (labels before/after normalization) that will cause conflicts
########################

dataset = 'fpad'
phase = 'train'  ## test/train
root_dir = '/home/bilbeisi/REN/'

############################ Draw pose on depth samples #################################
### draw pose on some depth samples to validate world2pixel and image/label loading
### this segment is only for validation
##############################################################################################
lbls = util.load_labels(dataset, phase)  ### load test/train data
names = util.load_names(dataset, phase)
centers = util.load_centers(dataset, phase).astype(float)

for idx, name in enumerate(names):
    if idx % 1000 == 0:
        lbl = np.asarray(np.reshape(lbls[idx].split(), (21, 3)),
                         dtype=np.float32)
        lbl, skel_camcoords = util.world2pixel(lbl, dataset)
        img = util.load_image(dataset, os.path.join(root_dir, name))
        img /= 1160
        img *= 255
        points = centers[idx]
        img = util.draw_pose(dataset, img, lbl, 3, (255, 0, 0), points)
        cv2.imwrite(
            root_dir + 'samples/depth/' + phase + '_' + str(idx) + '.png', img)
##############################################################################################
예제 #8
0
# Preprocess images before running this script
# Remember to change dataset depending on type of data (fpad for depth, and fpac for rgb)
# Run for both phase='test' and phase='train' to get both datafiles
##################################

################ Confirm dataset before running this script! ###############################
dataset = 'fpad'  # fpac or fpad

DIR = "../labels/"
phase = 'test'  # test/train
h5_fn = os.path.join(DIR, (dataset + '_' + phase + '_data.h5'))

############## Dir containing preprocessed images ###############################
base_dir = '/home/bilbeisi/REN/cropped/'

names = util.load_names(dataset, phase)
labels = util.load_labels(dataset, phase)

if dataset == 'fpad':
    imgs = np.zeros((len(names), 1, 96, 96), dtype=np.float32)  # depth
else:
    imgs = np.zeros((len(names), 3, 96, 96), dtype=np.float32)
lbls = np.zeros((len(labels), 63), dtype=np.float)

for idx, name in enumerate(names):
    if dataset == 'fpac':
        name = name.replace('.jpeg', '.png')
    img = util.load_image(dataset, os.path.join(base_dir, name))
    img = img.astype(float)
    # revert back to normalized -1,1 since images where saved in 0,255 to allow viewing/verifying
    img[:] *= 2
예제 #9
0
import time
import image_loops

##################################
# This script creates hdf5 files for RGB-D data since different processing is required
# Run for both phase='test' and phase='train' to get both datafiles
# colormap and depth2cords cython functions are used here
##################################

DIR = "../labels/"
phase = 'train'
h5_fn = os.path.join(DIR, ('rgbd_' + phase + '_data_.h5'))

base_dir = '/home/bilbeisi/REN/cropped/'

names = util.load_names('fpad', phase)
labels = util.load_labels('fpad', phase)
cnames = util.load_names('fpac', phase)
centers = util.load_centers('fpad', phase).astype(float)

imgs = np.zeros((len(names), 4, 96, 96), dtype=np.float32)
lbls = np.zeros((len(labels), 63), dtype=np.float)

cube_size = 150  # cube size in mm for cropping

for idx, name in enumerate(names):
    cname = cnames[idx]
    img = util.load_image('fpad', os.path.join('/home/bilbeisi/REN/', name))
    img[img == 0] = 1

    cimg = util.load_image('fpac', os.path.join('/home/bilbeisi/REN/', cname))
예제 #10
0
def main():
    if len(sys.argv) < 3:
        print_usage()

    dataset = sys.argv[1]
    out_file = sys.argv[2]
    # data_dir_dict = {'nyu': config.nyu_data_dir,
    #                  'icvl': config.icvl_data_dir + "test/Depth",
    #                  'msra': config.msra_data_dir}
    # base_dir = data_dir_dict[dataset] #sys.argv[3]
    # name = os.path.join(base_dir, names[0])
    batch_size = 64
    if len(sys.argv) == 4:
        batch_size = int(sys.argv[3])

    # generate deploy prototxt
    make_baseline_net(os.path.join(ROOT_DIR, '../models'), dataset)
    make_pose_ren_net(os.path.join(ROOT_DIR, '../models'), dataset)

    names = util.load_names(dataset)
    # centers = util.load_centers(dataset)
    centers = None
    fx, fy, ux, uy = 587.270, 587.270, 326.548, 230.419
    # fx, fy, ux, uy = util.get_param(dataset)
    lower_ = 1
    upper_ = 650
    hand_model = ModelPoseREN(
        dataset,
        lambda img: get_center(img, lower=lower_, upper=upper_),
        param=(fx, fy, ux, uy),
        use_gpu=True)

    if dataset == 'msra':
        hand_model.reset_model(dataset, test_id=0)

    base_dir = "/media/reborn/Others/Study/Reborn/Github/Pose-REN/test"
    # depthName = os.path.join(base_dir, "000000_depth.bin")
    depthName = os.path.join(base_dir, "0.img")
    imgName = os.path.join(base_dir, "image_0000.png")

    if (dataset == "msra"):
        # with open(depthName,"rb") as file:
        # 	data = np.fromfile(file, dtype=np.uint32)
        # 	width, height, left, top, right , bottom = data[:6]
        # 	depth = np.zeros((height, width), dtype=np.float32)
        # 	file.seek(4*6)
        # 	data = np.fromfile(file, dtype=np.float32)
        # depth[top:bottom, left:right] = np.reshape(data, (bottom-top, right-left))
        # depth[depth == 0] = 10000
        # print(depth[depth < depth.max()])
        # cv2.imwrite("img0.jpg",depth)

        with open(depthName, "rb") as file:
            data = np.fromfile(file, dtype=np.uint16)
            height = 480
            width = 640
            # depth = np.zeros((height, width), dtype=np.uint16)
            depth = np.reshape(data, (height, width)).astype(np.float32)
            min = depth.min()
            max = depth.max()
            print("min = {}, max = {}".format(min, max))

            flag = np.logical_xor(depth <= upper_, depth >= lower_)
            depth[flag] = 0

            kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
            depth = cv2.morphologyEx(depth, cv2.MORPH_CLOSE, kernel)
            depth[depth == 0] = 10000
            # depth[depth == 0] = depth.max()

        # with open(depthName,"rb") as file:
        # 	depth = []
        # 	cnt = 0
        # 	contents = iter(partial(file.read, 2), b'')
        # 	for r in contents:
        # 		r_int = int.from_bytes(r, byteorder='big')  #将 byte转化为 int
        # 		cnt += 1
        # 		depth.append(r_int)
        # 	# print("i = {} -- {}".format(cnt,r_int))
        # depth = np.array(depth)
        # depth = np.reshape(depth,(480,640))
        # depth[depth == 0] = 10000
    elif (dataset == "icvl"):
        depth = cv2.imread(imgName, 2)
        depth[depth == 0] = depth.max()  # invalid pixel
        depth = depth.astype(float)

    # depth = np.reshape(depth,(240,320))
    # depth[depth == 0] = depth.max()
    print("np.shape(depth) = {}".format(np.shape(depth)))
    # depth = depth[:, ::-1]
    # print("names = {}".format(imgName))
    # print("np.shape(img) = {}".format(np.shape(img)))
    # results = hand_model.detect_files(base_dir, names, centers, max_batch=batch_size)
    results = hand_model.detect_image(depth)
    print("results = {}".format(results))
    print(np.shape(results))
    img_show = show_results(depth, results, dataset)
    cv2.imwrite('result.jpg', img_show)
    cv2.waitKey()