Exemplo n.º 1
0
def main(img_path, json_path=None, video_name=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                         get_theta=True)

    #     print('JOINTS 3D:')
    #     print(joints3d.shape)
    #     print(joints3d)

    joints_names = [
        'Ankle.R_x', 'Ankle.R_y', 'Ankle.R_z', 'Knee.R_x', 'Knee.R_y',
        'Knee.R_z', 'Hip.R_x', 'Hip.R_y', 'Hip.R_z', 'Hip.L_x', 'Hip.L_y',
        'Hip.L_z', 'Knee.L_x', 'Knee.L_y', 'Knee.L_z', 'Ankle.L_x',
        'Ankle.L_y', 'Ankle.L_z', 'Wrist.R_x', 'Wrist.R_y', 'Wrist.R_z',
        'Elbow.R_x', 'Elbow.R_y', 'Elbow.R_z', 'Shoulder.R_x', 'Shoulder.R_y',
        'Shoulder.R_z', 'Shoulder.L_x', 'Shoulder.L_y', 'Shoulder.L_z',
        'Elbow.L_x', 'Elbow.L_y', 'Elbow.L_z', 'Wrist.L_x', 'Wrist.L_y',
        'Wrist.L_z', 'Neck_x', 'Neck_y', 'Neck_z', 'Head_x', 'Head_y',
        'Head_z', 'Nose_x', 'Nose_y', 'Nose_z', 'Eye.L_x', 'Eye.L_y',
        'Eye.L_z', 'Eye.R_x', 'Eye.R_y', 'Eye.R_z', 'Ear.L_x', 'Ear.L_y',
        'Ear.L_z', 'Ear.R_x', 'Ear.R_y', 'Ear.R_z'
    ]

    joints_export = pd.DataFrame(joints3d.reshape(1, 57), columns=joints_names)
    joints_export.index.name = 'frame'

    joints_export.iloc[:, 1::3] = joints_export.iloc[:, 1::3] * -1
    joints_export.iloc[:, 2::3] = joints_export.iloc[:, 2::3] * -1

    #     col_list = list(joints_export)

    #     col_list[1::3], col_list[2::3] = col_list[2::3], col_list[1::3]

    #     joints_export = joints_export[col_list]

    hipCenter = joints_export.loc[:][[
        'Hip.R_x', 'Hip.R_y', 'Hip.R_z', 'Hip.L_x', 'Hip.L_y', 'Hip.L_z'
    ]]

    joints_export['hip.Center_x'] = hipCenter.iloc[0][::3].sum() / 2
    joints_export['hip.Center_y'] = hipCenter.iloc[0][1::3].sum() / 2
    joints_export['hip.Center_z'] = hipCenter.iloc[0][2::3].sum() / 2

    if not os.path.exists("hmr/output/csv/" + video_name):
        os.mkdir("hmr/output/csv/" + video_name)

    joints_export.to_csv("hmr/output/csv/" + video_name + "/" +
                         os.path.splitext(os.path.basename(img_path))[0] +
                         ".csv")
Exemplo n.º 2
0
def main(pw3d_eval_path, paired=True):
    """
    This function isn't really doing evaluation on 3DPW - it just runs HMR on each 3DPW frame and stores the output.
    There is (or will be) a separate script in the pytorch_indirect_learning repo that will do the evaluation and metric
    computations.
    """
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    cropped_frames_dir = os.path.join(pw3d_eval_path, 'cropped_frames')
    save_path = '/data/cvfs/as2562/hmr/evaluations/3dpw'
    if not paired:
        save_path += '_unpaired'
    if not os.path.isdir(save_path):
        os.makedirs(save_path)
    eval_data = np.load(os.path.join(pw3d_eval_path, '3dpw_test.npz'))
    frame_fnames = eval_data['imgname']

    fname_per_frame = []
    pose_per_frame = []
    shape_per_frame = []
    verts_per_frame = []
    cam_per_frame = []

    for frame in tqdm(frame_fnames):
        image_path = os.path.join(cropped_frames_dir, frame)
        input_img, proc_param, img = preprocess_image(image_path)
        # Add batch dimension: 1 x D x D x 3
        input_img = np.expand_dims(input_img, 0)

        joints, verts, cams, joints3d, thetas = model.predict(input_img,
                                                              get_theta=True)
        poses = thetas[:, 3:3 + 72]
        shapes = thetas[:, 3 + 72:]

        fname_per_frame.append(frame)
        pose_per_frame.append(poses)
        shape_per_frame.append(shapes)
        verts_per_frame.append(verts)
        cam_per_frame.append(cams)

    fname_per_frame = np.array(fname_per_frame)
    np.save(os.path.join(save_path, 'fname_per_frame.npy'), fname_per_frame)

    pose_per_frame = np.concatenate(pose_per_frame, axis=0)
    np.save(os.path.join(save_path, 'pose_per_frame.npy'), pose_per_frame)

    shape_per_frame = np.concatenate(shape_per_frame, axis=0)
    np.save(os.path.join(save_path, 'shape_per_frame.npy'), shape_per_frame)

    verts_per_frame = np.concatenate(verts_per_frame, axis=0)
    np.save(os.path.join(save_path, 'verts_per_frame.npy'), verts_per_frame)

    cam_per_frame = np.concatenate(cam_per_frame, axis=0)
    np.save(os.path.join(save_path, 'cam_per_frame.npy'), cam_per_frame)
Exemplo n.º 3
0
def main(img_path, json_path, n):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    # TODO get multiple cropped images and get estimation for each of them
    input_imgs, proc_params, img = preprocess_image(img_path, json_path, n)

    for i in range(n):
        input_imgs[i] = np.expand_dims(input_imgs[i], 0)

        joints, verts, cams, joints3d, theta = model.predict(input_imgs[i],
                                                             get_theta=True)

        x, z = move(proc_params[i])
        center = [0.5 * img.shape[1], 0.5 * img.shape[0]]
        x = (x - center[0]) * 0.01
        z = (z - center[1]) * 0.01
        joints3d[0] += np.array([[x, 0, z]] * 19)

        joints_names = [
            'Ankle.R_x', 'Ankle.R_y', 'Ankle.R_z', 'Knee.R_x', 'Knee.R_y',
            'Knee.R_z', 'Hip.R_x', 'Hip.R_y', 'Hip.R_z', 'Hip.L_x', 'Hip.L_y',
            'Hip.L_z', 'Knee.L_x', 'Knee.L_y', 'Knee.L_z', 'Ankle.L_x',
            'Ankle.L_y', 'Ankle.L_z', 'Wrist.R_x', 'Wrist.R_y', 'Wrist.R_z',
            'Elbow.R_x', 'Elbow.R_y', 'Elbow.R_z', 'Shoulder.R_x',
            'Shoulder.R_y', 'Shoulder.R_z', 'Shoulder.L_x', 'Shoulder.L_y',
            'Shoulder.L_z', 'Elbow.L_x', 'Elbow.L_y', 'Elbow.L_z', 'Wrist.L_x',
            'Wrist.L_y', 'Wrist.L_z', 'Neck_x', 'Neck_y', 'Neck_z', 'Head_x',
            'Head_y', 'Head_z', 'Nose_x', 'Nose_y', 'Nose_z', 'Eye.L_x',
            'Eye.L_y', 'Eye.L_z', 'Eye.R_x', 'Eye.R_y', 'Eye.R_z', 'Ear.L_x',
            'Ear.L_y', 'Ear.L_z', 'Ear.R_x', 'Ear.R_y', 'Ear.R_z'
        ]

        joints_export = pd.DataFrame(joints3d.reshape(1, 57),
                                     columns=joints_names)
        joints_export.index.name = 'frame'

        joints_export.iloc[:, 1::3] = joints_export.iloc[:, 1::3] * -1
        joints_export.iloc[:, 2::3] = joints_export.iloc[:, 2::3] * -1

        hipCenter = joints_export.loc[:][[
            'Hip.R_x', 'Hip.R_y', 'Hip.R_z', 'Hip.L_x', 'Hip.L_y', 'Hip.L_z'
        ]]

        joints_export['hip.Center_x'] = hipCenter.iloc[0][::3].sum() / 2
        joints_export['hip.Center_y'] = hipCenter.iloc[0][1::3].sum() / 2
        joints_export['hip.Center_z'] = hipCenter.iloc[0][2::3].sum() / 2

        print("hmr/output/csv/" +
              os.path.splitext(os.path.basename(img_path))[0] +
              "_%02d.csv" % i)

        joints_export.to_csv("hmr/output/csv/" +
                             os.path.splitext(os.path.basename(img_path))[0] +
                             "_%02d.csv" % i)
Exemplo n.º 4
0
def main(img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    joints, verts, cams, joints3d, theta = model.predict(
        input_img, get_theta=True)

    visualize(img, proc_param, joints[0], verts[0], cams[0])
Exemplo n.º 5
0
def main(img_path):
    sess = tf.Session()
    model = RunModel(config, sess=sess)
    kps = get_people(img_path)

    input_img, proc_param, img = preprocess_image(img_path, kps)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                         get_theta=True)
    print(joints3d.shape)

    p3d(joints3d, cams[0], proc_param)
Exemplo n.º 6
0
def main(t_img_path, q_img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    input_t_img, proc_param, t_img = preprocess_image(t_img_path, json_path)
    input_q_img, proc_param, q_img = preprocess_image(q_img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_t_img = np.expand_dims(input_t_img, 0)
    input_q_img = np.expand_dims(input_q_img, 0)

    t_joints, t_verts, t_cams, t_joints3d, t_theta = model.predict(
        input_t_img, get_theta=True)
    q_joints, q_verts, q_cams, q_joints3d, q_theta = model.predict(
        input_q_img, get_theta=True)

    print("Joints shape :" + str(t_joints3d.shape))
    print("Joints shape 3d:" + str(t_joints3d.shape))
    print("Joints shape 0 :" + str(t_joints3d.shape[0]))
    print("Joints shape 1 :" + str(t_joints3d.shape[1]))
    print("Joints shape 2 :" + str(t_joints3d.shape[2]))
    visualize_3d3(q_img, proc_param, q_joints[0], q_verts[0], q_cams[0],
                  q_joints3d, t_img, proc_param, t_joints[0], t_verts[0],
                  t_cams[0], t_joints3d)
Exemplo n.º 7
0
def main(img_path, json_path=None, out_dir="hmr/output"):
    if config.img_path.endswith('.csv'):
        csv = pd.read_csv(config.img_path)
    else:
        raise NotImplementedError

    sess = tf.Session()
    model = RunModel(config, sess=sess)

    for ind, item in tqdm(csv.iterrows(), desc='Creating avatars'):
        tqdm.write('Creating avatar for %s' % item.img_path)
        out_dir = Path(out_dir)
        img_path = Path(item.img_path)
        json_path = Path(item.annot_path)
        dump_path = out_name(out_dir, img_path, suffix='_verts.pkl')

        if Path(dump_path).exists():
            tqdm.write('Avatar is already created')
            continue

        input_img, proc_param, img = preprocess_image(img_path, str(json_path))
        # Add batch dimension: 1 x D x D x 3
        input_img = np.expand_dims(input_img, 0)

        joints, verts, cams, joints3d, theta = model.predict(
            input_img, get_theta=True)

        # Write outputs
        joints_csv = os.path.join(str(out_dir), "csv/", os.path.splitext(os.path.basename(str(img_path)))[0] + ".csv")
        export_joints(joints3d, joints_csv)
        #     pose = pd.DataFrame(theta[:, 3:75])

        #     pose.to_csv("hmr/output/theta_test.csv", header=None, index=None)

        #     print('THETA:', pose.shape, pose)

        #     import cv2
        #     rotations = [cv2.Rodrigues(aa)[0] for aa in pose.reshape(-1, 3)]
        #     print('ROTATIONS:', rotations)
        out_images_dir = os.path.join(str(out_dir), "images")

        # measure(theta[0][0], verts[0][0])  # view, batch

        # Write avatar
        with open(str(dump_path), 'wb') as f:
            tqdm.write('Vertices dump was written to %s' % dump_path)
            pickle.dump(verts, f)

        visualize(str(img_path), img, proc_param, joints[0], verts[0], cams[0], output=str(out_images_dir))
Exemplo n.º 8
0
    def initialzation(self, img_path, json_path=None):
        sess = tf.Session()
        model = RunModel(self.config, sess=sess)

        input_img, proc_param, img = self.preprocess_image(img_path, json_path)
        # Add batch dimension: 1 x D x D x 3
        input_img = np.expand_dims(input_img, 0)

        # Theta is the 85D vector holding [camera, pose, shape]
        # where camera is 3D [s, tx, ty]
        # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
        # shape is 10D shape coefficients of SMPL
        joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                             get_theta=True)
        self.visualize(img, proc_param, joints[0], verts[0], cams[0])
Exemplo n.º 9
0
def main(folder_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    images = sorted([f for f in listdir(folder_path) if f.endswith('image.png')])
    annotated_joints = sorted([f for f in listdir(folder_path) if f.endswith('.npy')])
    abs_errors = []
    num_joints = 0
    num_accepted = 0

    images_joints_dict = dict(zip(images, annotated_joints))
    print('Predicting on all png images in folder.')
    for image in images:
        print('Image:', image)
        img_path = join(folder_path, image)
        input_img, proc_param, img = preprocess_image(img_path, json_path)
        # Add batch dimension: 1 x D x D x 3
        input_img = np.expand_dims(input_img, 0)

        joints, verts, cams, joints3d, theta = model.predict(
            input_img, get_theta=True)

        cam_for_render, vert_shifted, joints_orig = vis_util.get_original(
            proc_param, verts[0], cams[0], joints[0], img_size=img.shape[:2])

        annotation_array = np.load(join(folder_path, images_joints_dict[image]))
        annotation_array = zip(annotation_array[0], annotation_array[1])
        annotation_array = (np.rint(annotation_array)).astype(int)
        joints_orig = (np.rint(joints_orig)).astype(int)
        joints_orig = joints_orig[:14]

        for i in range(len(annotation_array)):
            annotation = (annotation_array[i][0], annotation_array[i][1])
            prediction = (joints_orig[i][0], joints_orig[i][1])
            # cv2.circle(img, annotation, 2,
            #            (0, 255, 0), thickness=5)
            # cv2.circle(img, prediction, 2,
            #            (0, 0, 255), thickness=5)
            # cv2.imshow('win', img)
            # cv2.waitKey(0)
            error = np.linalg.norm(np.subtract(annotation, prediction))
            # print(error)
            abs_errors.append(error)
            if error < 30:
                num_accepted += 1
            num_joints += 1

    print("MAE", np.mean(abs_errors), 'Fraction Accepted', float(num_accepted)/num_joints)
Exemplo n.º 10
0
def rec_human(pipe_img_2, pipe_center, pipe_scale, pipe_shape, pipe_kp):
    global last_person
    config = flags.FLAGS
    config(sys.argv)
    config.load_path = src.config.PRETRAINED_MODEL
    config.batch_size = 1
    sess = tf.Session()
    model = RunModel(config, sess=sess)
    rec_human_count = 0
    rec_human_time = time.time()
    #num_render = 1

    while True:

        img = pipe_img_2.recv()
        center = pipe_center.recv()
        scale = pipe_scale.recv()
        person_shape = pipe_shape.recv()
        kp = pipe_kp.recv()

        input_img, proc_param, last_person = img_util.scale_and_crop(
            img, scale, center, person_shape, 0.25, config.img_size,
            last_person)

        cv2.imwrite('/media/ramdisk/input.jpg', input_img)
        print(np.mean(input_img))

        input_img = ((input_img / 255.))

        # input_img = 2 * ((input_img / 255.) - 0.5)

        input_img = np.expand_dims(input_img, 0)
        joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                             get_theta=True)
        #cam_for_render, vert_shifted, joints_orig = vis_util.get_original(proc_param, verts[0], cams[0], joints[0], img_size=img.shape[:2])
        write_obj(smpl_model_used, theta, outmesh_path)
        str_1 = open(outmesh_path, 'rb').read()
        message_id = queue2.sendMessage(delay=0).message(str_1).execute()
        msg2.append(message_id)
        if len(msg2) > 1:
            rt = queue2.deleteMessage(id=msg2[0]).execute()
            del msg2[0]

        rec_human_count = rec_human_count + 1
        if rec_human_count == 100:
            print('rec FPS:', 1.0 / ((time.time() - rec_human_time) / 100.0))
            rec_human_count = 0
            rec_human_time = time.time()
def main(img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                         get_theta=True)

    visualize(img, proc_param, joints[0], verts[0], cams[0], img_path)

    theta_out = theta.tolist()
    with open('results/PhotoWakeUpNormals.json', 'w') as outfile:
        json.dump([theta_out], outfile)
Exemplo n.º 12
0
def main(img_path):
    sess = tf.Session()
    model = RunModel(config, sess=sess)
    img = io.imread(img_path)
    kps = multipose_get_people(img)

    input_img, proc_param, img = preprocess_image(img_path, kps)

    plot_crop(input_img)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                         get_theta=True)

    p3d(joints3d, cams[0], proc_param)
def main(img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    joints, verts, cams, joints3d, theta = model.predict(
        input_img, get_theta=True)
    print(theta)
    params = {'proc_param': proc_param, 'joints': joints[0], 'cam': cams[0], 'pose': theta[:, 3:75], 'shape': theta[:, 75:]}
    print(img_path.split('/')[-2])
    with open("/home/ankur/GUI_project/frames/FRAMES_PICKLE/"+ img_path.split('/')[-2] + '/' + img_path.split('/')[-1][:-4]+".pkl", 'wb') as f:
        pickle.dump(params, f)
    print(verts.shape)
    visualize(img_path, img, proc_param, joints[0], verts[0], cams[0], folder_name = img_path.split('/')[-2])
Exemplo n.º 14
0
def main(img_path, json_path=None):
    config_tf = tf.ConfigProto(device_count={'GPU': 0})
    sess = tf.Session(config=config_tf)
    model = RunModel(config, sess=sess)

    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    # Theta is the 85D vector holding [camera, pose, shape]
    # where camera is 3D [s, tx, ty]
    # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
    # shape is 10D shape coefficients of SMPL
    joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                         get_theta=True)

    visualize(img, proc_param, joints[0], verts[0], cams[0])
Exemplo n.º 15
0
def main(img_path, result_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    if not os.path.isdir(result_path):
        os.makedirs(result_path)

    if os.path.isdir(img_path):
        imgs = os.listdir(img_path)
        jsons = [None] * len(imgs)
        for i, im in enumerate(imgs):
            json = os.path.join(json_path, im.replace('.jpg',
                                                      '_keypoints.json'))
            if os.path.isfile(json):
                jsons[i] = json

        imgs = [os.path.join(img_path, item) for item in imgs]
    else:
        imgs = [img_path]
        jsons = [None] if json_path is None else [json_path]

    for _IMG, _JSON in tqdm(zip(imgs, jsons)):
        input_img, proc_param, img = preprocess_image(_IMG, _JSON)
        # Add batch dimension: 1 x D x D x 3
        input_img = np.expand_dims(input_img, 0)

        # Theta is the 85D vector holding [camera, pose, shape]
        # where camera is 3D [s, tx, ty]
        # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
        # shape is 10D shape coefficients of SMPL
        joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                             get_theta=True)

        pic = {
            'joints': joints[0],
            'verts': verts[0],
            'cams': cams[0],
            'joints3d': joints3d[0],
            'theta_cam': theta[0][:3],
            'pose': theta[0][3:-10],
            'shape': theta[0][-10:]
        }
        name = _IMG.split('/')[-1].split('.')[0] + '.pickle'
        with open(os.path.join(result_path, name), 'wb') as file:
            pickle.dump(pic, file)
Exemplo n.º 16
0
def main(img_path):
    sess = tf.Session()
    model = RunModel(config, sess=sess)
    kps = get_people(img_path)

    input_img, proc_param, img = preprocess_image(img_path, kps)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                         get_theta=True)

    theta = theta[0, 3:75]
    q = quaternion.from_rotation_vector(theta[3:6])
    print(q)
    q = quaternion.from_rotation_vector(theta[6:9])
    print(q)
    get_angles(joints3d, cams[0], proc_param)
Exemplo n.º 17
0
def main(img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                         get_theta=True)

    cams = theta[:, :model.num_cam]
    poses = theta[:, model.num_cam:(model.num_cam + model.num_theta)]
    shapes = theta[:, (model.num_cam + model.num_theta):]

    visualize(img, proc_param, joints[0], verts[0], cams[0])
    '''
    Start adjusting the shape
    '''
    shape_adjuster = torch.load("./trained/model_save1.57873062595")
    smpl = SMPL("./models/neutral_smpl_with_cocoplus_reg.pkl")

    beta = torch.from_numpy(shapes).float().cuda()
    theta = torch.zeros((1, 72)).float().cuda()
    heights = torch.from_numpy(np.asarray([1.9]))
    volume = torch.from_numpy(np.asarray([90 / 1000.]))

    verts, joints3d, Rs = smpl.forward(beta, theta, True)
    flatten_joints3d = joints3d.view(1, -1)
    heights = torch.unsqueeze(heights, -1).float().cuda()
    volumes = torch.unsqueeze(volume, -1).float().cuda()
    input_to_net = torch.cat((flatten_joints3d, heights, volumes), 1)

    adjusted_betas = shape_adjuster.forward(input_to_net)

    adjusted_verts, adjusted_joints3d, Rs = smpl.forward(
        adjusted_betas, theta, True)
    adjusted_heights = measure.compute_height(adjusted_verts)
    adjusted_volumes = measure.compute_volume(adjusted_verts, smpl.f)

    print(adjusted_heights, adjusted_volumes)

    debug_display_cloud(verts[0], joints3d[0], adjusted_verts[0],
                        adjusted_joints3d[0])
Exemplo n.º 18
0
def main(folder_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    images = [f for f in listdir(folder_path) if f.endswith('.png')]
    print('Predicting on all png images in folder.')
    for image in images:
        print('Image:', image)
        img_path = join(folder_path, image)
        input_img, proc_param, img = preprocess_image(img_path, json_path)
        # Add batch dimension: 1 x D x D x 3
        input_img = np.expand_dims(input_img, 0)

        joints, verts, cams, joints3d, theta = model.predict(
            input_img, get_theta=True)



        visualize(img, proc_param, joints[0], verts[0], cams[0], img_path)
Exemplo n.º 19
0
def main(img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    joints, verts, cams, joints3d, theta, weights = model.predict(
        input_img, get_theta=True, get_weights=True)

    PhotoWakeUpSilhouettes.visualize(img, proc_param, joints[0], verts[0], cams[0], img_path)
    PhotoWakeUpNormals.visualize(img, proc_param, joints[0], verts[0], cams[0], img_path)
    PhotoWakeUpDepthMaps.visualize(img, proc_param, joints[0], verts[0], cams[0], img_path)
    PhotoWakeUpWeights.visualize(img, proc_param, joints[0], verts[0], weights[0], cams[0], img_path)

    print(theta)
    theta_out = theta.tolist()
    with open('results/HMR_value_out.json', 'w') as outfile: 
	    json.dump([theta_out], outfile)
Exemplo n.º 20
0
def main(img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    # Theta is the 85D vector holding [camera, pose, shape]
    # where camera is 3D [s, tx, ty]
    # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
    # shape is 10D shape coefficients of SMPL
    joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                         get_theta=True)

    # results_np = verts
    np.save('ronaldo/hmr_output.npy', theta[:, 3:])
    # exit()
    # np.save('/Users/test/Desktop/hmr/basketbolist_smpl.npy', results_np)
    visualize(img, proc_param, joints[0], verts[0], cams[0])
Exemplo n.º 21
0
def main(img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    for fliplr in [False]:
        input_img, proc_param, img = preprocess_image(img_path, json_path, fliplr=fliplr)
        # Add batch dimension: 1 x D x D x 3
        input_img = np.expand_dims(input_img, 0)

        # Theta is the 85D vector holding [camera, pose, shape]
        # where camera is 3D [s, tx, ty]
       # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
        # shape is 10D shape coefficients of SMPL
        joints, verts, cams, joints3d, theta = model.predict(
            input_img, get_theta=True)


    # scaling and translation
    save_mesh(img, img_path, proc_param, joints[0], verts[0], cams[0])
    visualize(img, proc_param, joints[0], verts[0], cams[0])
Exemplo n.º 22
0
def main(img_path, data_path):
    sess = tf.Session()
    model = RunModel(config, sess=sess)
    imgs = open(img_path)
    input_imgs = json.load(imgs)

    i = 0
    wrists = {}
    shift = [-20, -10, 0, 10, 20]
    from tqdm import tqdm
    for img_name in tqdm(input_imgs):
        input_path = os.path.join(data_path, img_name)
        input_imgs, proc_params, imgs = preprocess_image(input_path, shift)
        # Add batch dimension: 1 x D x D x 3
        for i in range(len(imgs)):
            input_img = input_imgs[i]
            proc_param = proc_params[i]
            img = imgs[i]
            input_img = np.expand_dims(input_img, 0)
            # print (input_img.shape)

            # Theta is the 85D vector holding [camera, pose, shape]
            # where camera is 3D [s, tx, ty]
            # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
            # shape is 10D shape coefficients of SMPL
            joints, verts, cams, joints3d, theta = model.predict(
                input_img, get_theta=True)
            # print(type(joints[0][11]))
            wrist = {}
            # wrist['filename'] = img_name

            cam_for_render, vert_shifted, joints_orig = vis_util.get_original(
            proc_param, verts[0], cams[0], joints[0], img_size=img.shape[:2])

            # record original position
            wrist['left_wrist'] = joints_orig[11].tolist()
            wrist['right_wrist'] = joints_orig[6].tolist()
            wrists[img_name[:-4] + '_' + str(shift[i]) + img_name[-4:]] = wrist

            visualize(img, proc_param, joints[0], verts[0], cams[0], img_name[:-4].split('/')[-1] + '_' + str(shift[i]) + img_name[-4:])
    gen_json.genHMRWrist(wrists)
Exemplo n.º 23
0
def main(img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    # Theta is the 85D vector holding [camera, pose, shape]
    # where camera is 3D [s, tx, ty]
    # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
    # shape is 10D shape coefficients of SMPL
    joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                         get_theta=True)

    print(theta)
    theta_out = theta.tolist()
    with open('results/HMR_value_out.json', 'w') as outfile:
        json.dump([theta_out], outfile)

    visualize(img, proc_param, joints[0], verts[0], cams[0])
Exemplo n.º 24
0
def main(img_dir):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    print(img_dir)
    onlyfiles = [
        f for f in os.listdir(img_dir)
        if os.path.isfile(os.path.join(img_dir, f))
    ]
    for file in onlyfiles:
        img_path = os.path.join(img_dir, file)
        kps = get_people(img_path)

        input_img, proc_param, img = preprocess_image(img_path, kps)
        # Add batch dimension: 1 x D x D x 3
        input_img = np.expand_dims(input_img, 0)

        joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                             get_theta=True)

        p3d(img, joints3d, joints[0], verts[0], cams[0], proc_param, file)
Exemplo n.º 25
0
def main(img_path, model_type='ResNet50-HMR', json_path=None):
    config = flags.FLAGS
    config(sys.argv)

    config.load_path = src.config.FULL_PRETRAINED_MODEL
    if model_type == 'ResNet50-HMR':
        second_load_path = None
    elif model_type == 'ResNet50-ImageNet':
        second_load_path = src.config.RESNET_IMAGENET_PRETRAINED_MODEL
    else:
        print('Error. Model type {} is currently not implemented'.format(
            model_type))

    config.batch_size = 1

    tf.reset_default_graph()
    sess = tf.Session()
    model = RunModel(config, second_load_path, sess=sess)

    img_name = str.split(os.path.basename(img_path), '.')[0]
    input_img, proc_param, img = preprocess_image(img_path, config.img_size,
                                                  json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    # Theta is the 85D vector holding [camera, pose, shape]
    # where camera is 3D [s, tx, ty]
    # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
    # shape is 10D shape coefficients of SMPL
    joints, verts, cams, joints3d, theta, layer_activations = model.predict(
        input_img, get_theta=True)

    renderer = vis_util.SMPLRenderer(face_path=config.smpl_face_path)
    pdb.set_trace()
    fig = visualize(img, proc_param, renderer, joints[0], verts[0], cams[0])
    save_dir = 'reconstructions-' + model_type
    make_path(save_dir)
    fig.savefig(os.path.join(save_dir, img_name + '.png'))

    return layer_activations
Exemplo n.º 26
0
def main(img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)

    joints, verts, cams, joints3d, theta = model.predict(input_img,
                                                         get_theta=True)

    joints_names = [
        'Ankle.R_x', 'Ankle.R_y', 'Ankle.R_z', 'Knee.R_x', 'Knee.R_y',
        'Knee.R_z', 'Hip.R_x', 'Hip.R_y', 'Hip.R_z', 'Hip.L_x', 'Hip.L_y',
        'Hip.L_z', 'Knee.L_x', 'Knee.L_y', 'Knee.L_z', 'Ankle.L_x',
        'Ankle.L_y', 'Ankle.L_z', 'Wrist.R_x', 'Wrist.R_y', 'Wrist.R_z',
        'Elbow.R_x', 'Elbow.R_y', 'Elbow.R_z', 'Shoulder.R_x', 'Shoulder.R_y',
        'Shoulder.R_z', 'Shoulder.L_x', 'Shoulder.L_y', 'Shoulder.L_z',
        'Elbow.L_x', 'Elbow.L_y', 'Elbow.L_z', 'Wrist.L_x', 'Wrist.L_y',
        'Wrist.L_z', 'Neck_x', 'Neck_y', 'Neck_z', 'Head_x', 'Head_y',
        'Head_z', 'Nose_x', 'Nose_y', 'Nose_z', 'Eye.L_x', 'Eye.L_y',
        'Eye.L_z', 'Eye.R_x', 'Eye.R_y', 'Eye.R_z', 'Ear.L_x', 'Ear.L_y',
        'Ear.L_z', 'Ear.R_x', 'Ear.R_y', 'Ear.R_z'
    ]

    joints_export = pd.DataFrame(joints3d.reshape(1, 57), columns=joints_names)
    joints_export.index.name = 'frame'

    joints_export.iloc[:, 1::3] = joints_export.iloc[:, 1::3] * -1
    joints_export.iloc[:, 2::3] = joints_export.iloc[:, 2::3] * -1

    hipCenter = joints_export.loc[:][[
        'Hip.R_x', 'Hip.R_y', 'Hip.R_z', 'Hip.L_x', 'Hip.L_y', 'Hip.L_z'
    ]]

    joints_export['hip.Center_x'] = hipCenter.iloc[0][::3].sum() / 2
    joints_export['hip.Center_y'] = hipCenter.iloc[0][1::3].sum() / 2
    joints_export['hip.Center_z'] = hipCenter.iloc[0][2::3].sum() / 2

    joints_export.to_csv("hmr.csv")
Exemplo n.º 27
0
class hmr_predictor():
    def __init__(self):
        self.config = flags.FLAGS
        self.config([1, "main"])
        self.config.load_path = src.config.PRETRAINED_MODEL
        self.config.batch_size = 1
        self.sess = tf.Session()
        self.model = RunModel(self.config, sess=self.sess)

    def predict(self, img, sil_bbox=False, sil=None):
        if sil_bbox is True:
            std_img, proc_para = preproc_img(img, True, sil)
        else:
            std_img, proc_para = preproc_img(img)
        std_img = np.expand_dims(std_img, 0)  # Add batch dimension
        _, verts, ori_cam, _ = self.model.predict(std_img)
        shf_vert = shift_verts(proc_para, verts[0], ori_cam[0])
        cam = np.array([500, 112.0, 112.0])
        return shf_vert, cam, proc_para, std_img[0]

    def close(self):
        self.sess.close()
Exemplo n.º 28
0
Arquivo: demo.py Projeto: Yucao42/hmr
def main(img_path, json_path=None):
    sess = tf.Session()
    model = RunModel(config, sess=sess)

    start = dt.now()
    input_img, proc_param, img = preprocess_image(img_path, json_path)
    # Add batch dimension: 1 x D x D x 3
    input_img = np.expand_dims(input_img, 0)
    print('Preprocessing time {:.2f} s'.format((dt.now()- start).total_seconds()))

    # Theta is the 85D vector holding [camera, pose, shape]
    # where camera is 3D [s, tx, ty]
    # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
    # shape is 10D shape coefficients of SMPL
    start = dt.now()
    joints, verts, cams, joints3d, theta = model.predict(
        input_img, get_theta=True)
    print('Model inference time {:.2f} s'.format((dt.now()- start).total_seconds()))

    start = dt.now()
    visualize(img, proc_param, joints[0], verts[0], cams[0])
    print('Visualization time {:.2f} s'.format((dt.now()- start).total_seconds()))
Exemplo n.º 29
0
def main(img_dir):
    sess = tf.Session()
    model = RunModel(config, sess=sess)
    num_features = 72
    num_cam = 3
    i = 0
    print(img_dir)
    files = [
        f for f in os.listdir(img_dir)
        if os.path.isfile(os.path.join(img_dir, f))
    ]
    files = sorted(files, key=lambda f: int(f.rsplit('.')[0].split('_')[-1]))

    nbr_img = len(files)
    print(nbr_img)
    poses = np.zeros((nbr_img, num_features))
    cams = np.zeros((nbr_img, num_cam))
    trans = np.zeros((nbr_img, 3))
    j2d = np.zeros((nbr_img, 19, 2))
    #proc_params = [{}] * nbr_img
    for file in files:
        img_path = os.path.join(img_dir, file)
        print(img_path)

        input_img, proc_param, img = preprocess_image(img_path)
        # Add batch dimension: 1 x D x D x 3
        input_img = np.expand_dims(input_img, 0)

        joints, verts, cam, joints3d, theta = model.predict(input_img,
                                                            get_theta=True)
        poses[i] = theta[:, num_cam:(num_cam + num_features)]
        j2d[i] = joints
        cams[i] = cam
        #proc_params[i] = proc_param
        save(img, proc_param, joints[0], verts[0], cams[0], i)
        i += 1

    #print(proc_param)
    return poses, cams, j2d
Exemplo n.º 30
0
def main(dir_path, json_path=None):
    if not os.path.exists('../3D-Person-reID/3DMarket'):
        os.mkdir('../3D-Person-reID/3DMarket')
    sess = tf.Session()
    model = RunModel(config, sess=sess)
    for split in ['train', 'train_all', 'val', 'gallery', 'query']:
        for root, dirs, files in os.walk(dir_path+split, topdown=True):
            for img_path in files:
                if not img_path[-3:]=='jpg':
                    continue 
                img_path = root +'/' + img_path
                print(img_path)
                input_img, proc_param, img = preprocess_image(img_path, json_path)
                input_img = np.expand_dims(input_img, 0)

                # Theta is the 85D vector holding [camera, pose, shape]
                # where camera is 3D [s, tx, ty]
                # pose is 72D vector holding the rotation of 24 joints of SMPL in axis angle format
                # shape is 10D shape coefficients of SMPL
                joints, verts, cams, joints3d, theta = model.predict(
                    input_img, get_theta=True)
                # scaling and translation
                save_mesh(img, img_path, split, proc_param, joints[0], verts[0], cams[0])