Exemple #1
0
def main():
    # intrinsic paramters of Intel Realsense SR300
    fx, fy, ux, uy = 628.668, 628.668, 311.662, 231.571
    depth_scale = 0.0010000000474974513
    # paramters
    dataset = 'hands17'
    if len(sys.argv) == 2:
        dataset = sys.argv[1]
    print(dataset)
    lower_ = 1
    upper_ = 435

    # init hand pose estimation model
    try:
        hand_model = ModelPoseREN(
            dataset,
            lambda img: get_center(img, lower=lower_, upper=upper_),
            param=(fx, fy, ux, uy),
            use_gpu=True)
    except:
        print('Model not found')
    # for msra dataset, use the weights for first split
    if dataset == 'msra':
        hand_model.reset_model(dataset, test_id=0)
    # realtime hand pose estimation loop
    frames = read_images(img_array, depth_scale)
    # preprocessing depth
    # # training samples are left hands in icvl dataset,
    # # right hands in nyu dataset and msra dataset,
    # # for this demo you should use your right hand
    if dataset == 'icvl':
        frames = frames[:, ::-1]  # flip
    # get hand pose
    predicted = []
    f = open('results.txt', 'w')
    for idx, depth in enumerate(frames):
        depth = frames[idx, :, :]
        #        depth = np.rot90(depth,2)
        depth[depth == 0] = depth.max()
        results, cropped_image = hand_model.detect_image(depth)
        img_show = show_results(depth, results, cropped_image, dataset)
        # cv2.imshow('result', img_show)
        cv2.imwrite('result_{}.png'.format(idx), img_show)
        f.write('image_{}.png'.format(idx))
        for r in results:
            for i in r:
                f.write(' %s' % i)
        f.write('\n')
        #print(results)
        predicted.append(results)
    f.close()
Exemple #2
0
def main():
    # intrinsic paramters of Intel Realsense D415
    fx, fy, ux, uy = 628.668, 628.668, 311.662, 231.571

    # paramters
    dataset = 'icvl'
    if len(sys.argv) == 2:
        dataset = sys.argv[1]
    print(dataset)
    lower_ = 1
    upper_ = 650

    # init realsense
    pipeline, depth_scale = init_device()
    # init hand pose estimation model
    try:
        hand_model = ModelPoseREN(
            dataset,
            lambda img: get_center(img, lower=lower_, upper=upper_),
            param=(fx, fy, ux, uy),
            use_gpu=True)
    except:
        print('Model not found')
    # for msra dataset, use the weights for first split
    if dataset == 'msra':
        hand_model.reset_model(dataset, test_id=0)
    # realtime hand pose estimation loop
    while True:
        depth = read_frame_from_device(pipeline, depth_scale)
        # preprocessing depth
        depth[depth == 0] = depth.max()
        # training samples are left hands in icvl dataset,
        # right hands in nyu dataset and msra dataset,
        # for this demo you should use your right hand
        if dataset == 'icvl':
            depth = depth[:, ::-1]  # flip
        # get hand pose
        results, cropped_image = hand_model.detect_image(depth)
        img_show = show_results(depth, results, cropped_image, dataset)
        cv2.imshow('result', img_show)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    stop_device(pipeline)
Exemple #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)
Exemple #4
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]
    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)
Exemple #5
0
def main():
    # intrinsic paramters of Intel Realsense SR300
    # fx, fy, ux, uy = 463.889, 463.889, 320, 240
    fx, fy, ux, uy = 385.13, 385.13, 316.802, 241.818
    # paramters
    dataset = 'hands17'
    if len(sys.argv) == 2:
        dataset = sys.argv[1]

    lower_ = 200
    upper_ = 450

    # init realsense
    pipeline, depth_scale = init_device()
    # init hand pose estimation model
    hand_model = ModelPoseREN(
        dataset,
        lambda img: get_center(img, lower=lower_, upper=upper_),
        param=(fx, fy, ux, uy),
        use_gpu=True)
    # for msra dataset, use the weights for first split
    if dataset == 'msra':
        hand_model.reset_model(dataset, test_id=0)

    is_recording = False
    is_testing = False
    d, f = read_trainset()
    rec = []

    # realtime hand pose estimation loop
    while True:
        depth = read_frame_from_device(pipeline, depth_scale)
        # preprocessing depth
        depth[depth == 0] = depth.max()
        # training samples are left hands in icvl dataset,
        # right hands in nyu dataset and msra dataset,
        # for this demo you should use your right hand
        if dataset == 'icvl':
            depth = depth[:, ::-1]  # flip
        # get hand pose
        results, cropped_image = hand_model.detect_image(depth)
        img_show = show_results(depth, results, cropped_image, dataset)
        cv2.imshow('result', img_show)

        if is_recording or is_testing:
            rec.append(results)
        readkey = cv2.waitKey(1) & 0xFF

        if readkey == ord('q'):
            break

        elif readkey == ord('r'):
            if is_testing:
                print('*** cannot record ***')
            if not is_recording:
                print("--- is recording ---")
                is_recording = True
            else:
                print("--- recording stopped ---")
                is_recording = False
                filename = input('filename: ')
                np.save(filename + '.npy', rec)
                rec = []

        elif readkey & 0xFF == ord('t'):
            if is_recording:
                print('*** cannot test ***')
            if not is_testing:
                print('--- is testing ---')
                is_testing = True
            else:
                is_testing = False
                t_feat = get_feature(np.array(rec))
                rec = []
                test(t_feat, f)

    stop_device(pipeline)
Exemple #6
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()