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) # visualize(img, proc_param, joints[0], verts[0], cams[0]) # np.set_printoptions(threshold=np.inf) # print(np.shape(joints)) # print(np.shape(verts)) # print(np.shape(cams)) # print(np.shape(joints3d)) # print(np.shape(theta)) # np.savetxt('../lab/hks/src/finaldefense/aaa1.csv',verts[0],delimiter=',') # np.savetxt('../lab/hks/src/finaldefense/aaa2.csv',joints3d[0],delimiter=',') np.savetxt('../smpl/smpl_webuser/hello_world/theta.csv', theta[0], delimiter=',')
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) params = { 'proc_param': proc_param, 'joints': joints[0], 'cam': cams[0], 'pose': theta[:, 3:75], 'shape': theta[:, 75:] } cur_path = os.getcwd() os.chdir("..") cur_path1 = os.getcwd() os.chdir(cur_path) with open( cur_path1 + "/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])
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 # 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 joints_export.to_csv("hmr/output/csv/" + os.path.splitext(os.path.basename(img_path))[0] + ".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) visualize(img_path, img, proc_param, joints[0], verts[0], cams[0])
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 main(img_path, json_path=None): sess = tf.Session() model = RunModel(config, sess=sess) path_dec = img_path.split("/") ls = os.listdir(img_path) result_smpl = np.empty((0, nsmpl)) result_3d_joints = np.empty((0, 3 * njoints)) for img in sorted(ls): if is_image(img): image_name = os.path.join(img_path, img) input_img, proc_param, img = preprocess_image( image_name, 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) result_3d_joints = np.append(result_3d_joints, [joints3d.flatten()], axis=0) print(cams) print(theta) print(joints) result_smpl = np.append(result_smpl, [theta.flatten()], axis=0) #visualize(img, proc_param, joints[0], verts[0], cams[0]) np.save(os.path.join('../coords_kazanawa', path_dec[-1] + '.npy'), result_3d_joints)
def main(dir_path, json_path=None): if not os.path.exists('../3D-Person-reID/3DDuke'): os.mkdir('../3D-Person-reID/3DDuke') 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])
def main(img_path, height, json_path=None): # renderer = vis_util.SMPLRenderer(face_path='src/tf_smpl/smpl_faces.npy') sess = tf.Session() model = RunModel(sess=sess) # cv2.imshow('input image for measurement extraction',img_path) # cv2.waitKey(0) 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]) # face_path = './src/tf_smpl/smpl_faces.npy' # faces = np.load(face_path) # obj_mesh_name = 'test.obj' extract_measurements.extract_measurements(height,verts[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("[***] prediction done !") cam_for_render = visualize(img, proc_param, joints[0], verts[0], cams[0]) """ save parameters to json files """ json_fname = 'results/pred_params.json' dict_to_save = {} dict_to_save['joints2d_pd'] = joints[ 0] # [0] is along batch_size dimension, here we just set batch_size = 1; dict_to_save['joints3d_pd'] = joints3d[ 0] # [0] is along batch_size dimension, here we just set batch_size = 1; dict_to_save['cam_pd_orig'] = cam_for_render dict_to_save['cam_pd'] = cams[0] dict_to_save['pose_pd'] = theta[0, 3:75] dict_to_save['shape_pd'] = theta[0, 75:85] save_to_json(dict_to_save, json_fname)
def main(img_path, json_path=None): sess = tf.Session() model = RunModel(config, sess=sess) print(img_path) images = glob.glob('.\data\*.PNG') print(images) for input_img in images: filename, file_extension = os.path.splitext(input_img) print(filename) pass input_img, proc_param, img = preprocess_image(input_img, 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 start = datetime.datetime.now() joints, verts, cams, joints3d, theta = model.predict(input_img, get_theta=True) end = datetime.datetime.now() delta = end - start print("took:", delta) # print("joints" + joints) # print("verts" , verts) save(verts[0], filename)
def main(dir_path, json_path=None): if not os.path.exists('./3DMarket_hmr_mask'): os.mkdir('./3DMarket_hmr_mask') sess = tf.Session() model = RunModel(config, sess=sess) face_path = './src/tf_smpl/smpl_faces.npy' faces = np.load(face_path) count = 0 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: count +=1 if not count % 9 ==1: continue 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 since = time.time() joints, verts, cams, joints3d, theta = model.predict( input_img, get_theta=True) # scaling and translation print(time.time()-since) save_mesh(img, img_path, split, proc_param, joints[0], verts[0], cams[0], faces)
def main(): sess = tf.Session() model = RunModel(config, sess=sess) for walk_type in sorted(os.listdir('../data/preproc')): thetas = [] outfn = '../data/hmr/{}.pkl'.format(walk_type) if os.path.exists(outfn): continue for fn in sorted(os.listdir(os.path.join('../data/preproc', walk_type))): img_path = os.path.join('../data/preproc', walk_type, fn) jfn = fn.split('.')[0] + '_keypoints.json' json_path = os.path.join('../data/json', walk_type, jfn) 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) thetas += [theta[0]] with open(outfn, 'wb') as f: pickle.dump(np.stack(thetas), f)
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]) # obj file create part vert = tf.squeeze(verts) result = sess.run(vert) model_path = './models/neutral_smpl_with_cocoplus_reg.pkl' outmesh_path = './smpl_tf.obj' with open(model_path, 'r') as f: params = pickle.load(f) faces = params['f'] with open(outmesh_path, 'w') as fp: for v in result: fp.write('v %f %f %f\n' % (v[0], v[1], v[2])) for f in faces + 1: fp.write('f %d %d %d\n' % (f[0], f[1], f[2]))
def main(folder_path, json_path=None): sess = tf.Session() model = RunModel(config, sess=sess) folder_path = "/Users/Akash_Sengupta/Documents/4th_year_project_datasets/up-s31/s31_padded_small_glob_rot/val_masks/val" images = [f for f in sorted(listdir(folder_path)) if f.endswith('.png')] print('Predicting on all png images in folder.') squared_errors = [] 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) fnumber = image[:5] gt_smpl_fname = fnumber + "_body.pkl" gt_pose_no_glob_rot = load_gt_pose(gt_smpl_fname) pred_pose_no_glob_rot = theta[0, 6:75] error = np.square(gt_pose_no_glob_rot - pred_pose_no_glob_rot) squared_errors.append(error) squared_errors = np.concatenate(squared_errors) print(squared_errors.shape) print("MSE pose params", np.mean(squared_errors))
def main(img_path, json_path=None): sess = tf.Session() model = RunModel(config, sess=sess) # imgpath = '/User/akase/lab/hks/src/finaldefense/image/'+img_path imgpath = 'exp/imgdata/' + img_path input_img, proc_param, img = preprocess_image(imgpath, 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) if True: visualize(img, proc_param, joints[0], verts[0], cams[0], img_path[:-4]) np.savetxt('exp/theta/theta' + img_path[:-4] + '.csv', theta[0], delimiter=',') np.savetxt('exp/joint/joint' + img_path[:-4] + '.csv', joints3d[0], delimiter=',') np.savetxt('exp/verts/verts' + img_path[:-4] + '.csv', verts[0], delimiter=',')
def rec_human(pipe_img_2, pipe_center, pipe_scale): 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) print(config.smpl_face_path) rec_human_count = 0 rec_human_time = time.time() while True: img = pipe_img_2.recv() center = pipe_center.recv() scale = pipe_scale.recv() input_img, proc_param = img_util.scale_and_crop( img, scale, center, config.img_size) 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]) #print(cam_for_render.shape) 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_file, json_path_file=None, output_path=None): sess = tf.Session() model = RunModel(config, sess=sess) assert img_path_file is not None and json_path_file is not None and output_path is not None if not os.path.isdir(output_path): os.makedirs(output_path) with open(img_path_file) as f: img_paths = [line.strip() for line in f] with open(json_path_file) as f: json_paths = [line.strip() for line in f] assert len(img_paths) == len(json_paths) count = 0 for img_path, json_path in zip(img_paths, json_paths): count += 1 print('processing image {} / {}'.format(count, len(img_paths))) 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) rendered = visualize(img, proc_param, joints[0], verts[0], cams[0]) output_file = os.path.join(output_path, os.path.basename(img_path[:-4]) + '.png') io.imsave(output_file, rendered)
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) # theta_out = theta.tolist() # with open('results/ddxk.json', 'w') as outfile: # json.dump(theta[0], outfile) # with open('results/ddxk.csv', 'wb') as myfile: # wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) # wr.writerow(theta) np.save('theta_and_beta_coco2.npy', theta[0][0, 3:]) # print(theta[0][0, 3:]) exit() visualize(img, proc_param, joints[0], verts[0], cams[0])
def main(save_path, json_path=None): sess = tf.Session() model = RunModel(config, sess=sess) cap = cv2.VideoCapture(0) frame_num = 0 while True: # Capture frame-by-frame ret, img = cap.read() # Operations on frame input_img, proc_param, img = preprocess_image(img, 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_and_save(img, proc_param, joints[0], verts[0], cams[0], save_path, frame_num) frame_num += 1 if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
def multiple(img_path, json_path=None): sess = tf.Session() model = RunModel(config, sess=sess) all_joints = [] all_verts = [] all_cams = [] #array of [input_img, proc_param, img] img_params = preprocess_multiple(img_path, json_path) for input_img, proc_param, img in img_params: # 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) all_joints.append(joints) all_verts.append(verts) all_cams.append(cams) imgs = [tup[2] for tup in img_params] proc_params = [tup[1] for tup in img_params] visualize_all(imgs, proc_params, all_joints, all_verts, all_cams)
def main(vid_path, json_path=None): sess = tf.Session() model = RunModel(config, sess=sess) # Video capture cap = cv2.VideoCapture(vid_path) # verts_anim = [] while True: ret, frame = cap.read() input_img, proc_param, img = preprocess_image(frame, 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) # verts_anim.append(verts) # print (verts) cam_for_render, vert_shifted, joints_orig = vis_util.get_original( proc_param, verts, cams[0], joints[0], img_size=img.shape[:2]) rend_img_overlay = renderer(vert_shifted, cam=cam_for_render, img=img, do_alpha=True) cv2.imshow('processed', rend_img_overlay) cv2.imshow('processed1', frame)
def main(img_path): sess = tf.compat.v1.Session() # meva_sample_1: person_bboxes = [[171, 102, 225, 244], [63, 71, 104, 199]] # meva sample 2: person_bboxes = [[95, 132, 429, 551], [0, 2, 245, 485], [319, 43, 539, 427]] # meva_sample 3: person_bboxes = [[155, 224, 381, 508], [19, 112, 238, 499], [305, 158, 508, 404]] person_bboxes = [[319, 43, 539, 427], [0, 2, 245, 485], [95, 132, 429, 551]] num_persons = len(person_bboxes) # Demo only processes one image at a time config.batch_size = num_persons model = RunModel(config, sess=sess) input_array = np.zeros( shape=[num_persons, config.img_size, config.img_size, 3]) proc_params = [] for person_idx, person_bbox in enumerate(person_bboxes): input_img, proc_param, img = preprocess_image(img_path, person_bbox) proc_params.append(proc_param) # Add batch dimension: 1 x D x D x 3 input_array[person_idx] = input_img #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_array, get_theta=True) visualize_all(num_persons, img, proc_params, joints, verts, cams)
def main(img_fname, out_dir): sess = tf.Session() model = RunModel(config, sess=sess) input_img, proc_param, img = preprocess_image(img_fname) input_img = np.expand_dims(input_img, 0) joints, verts, cams, joints3d, theta \ = model.predict(input_img, get_theta=True) cam_s = cams[0][0] cam_t = cams[0][1:] theta = theta[0] verts = verts[0] img_dir, img_name = os.path.split(img_fname) with open(os.path.join(out_dir, img_name + '.smpl_vertex.txt'), 'w') as fp: for p in verts: fp.write('%f %f %f\n' % ((p[0] + cam_t[0]) * cam_s, (p[1] + cam_t[1]) * cam_s, p[2] * cam_s)) with open(os.path.join(out_dir, img_name + '.cam_param.txt'), 'w') as fp: fp.write('%f %f %f' % (cam_s, cam_t[0], cam_t[1])) with open(os.path.join(out_dir, img_name + '.smpl_param.txt'), 'w') as fp: for p in theta[3:75]: fp.write('%f ' % p) fp.write('\n') for p in theta[75:]: fp.write('%f ' % p)
def main(h36m_eval_path, protocol=2, paired=True): """ This function isn't really doing evaluation on H3.6M - it just runs HMR on each H3.6M 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(h36m_eval_path, 'cropped_frames') save_path = '/data/cvfs/as2562/hmr/evaluations/h36m_protocol{}'.format( str(protocol)) if not paired: save_path += '_unpaired' if not os.path.isdir(save_path): os.makedirs(save_path) eval_data = np.load( os.path.join(h36m_eval_path, 'h36m_with_smpl_valid_protocol{}.npz').format( str(protocol))) 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)
def main(img_path, json_path=None): sess = tf.Session() model = RunModel(config, sess=sess) images = glob.glob('./data/*.jpg') print("Prediction started") print(len(images)) d = {} for n, j in enumerate(images): img_arr = [] pose_arr = [] # imgs = natsorted(glob.glob(j+'/*.jpg')) print(j) # for i in imgs : # print (i) # print ('{}/{} Done'.format(n,len(images))) input_img, proc_param, img = preprocess_image(j, json_path) # Add batch dimension: 1 x D x D x 3 input_img = np.expand_dims(input_img, 0) # k = '/'.join(i.split('.')[0].split('/')[-2:]) # 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 (joints3d.shape) pose_3d = joints3d.reshape((24, 3)) po = modified_mat(pose_3d, '3d') poses_3d = np.squeeze( augment_pose_seq(np.expand_dims(po, 0), z_limit=(-90, -90), y_limit=(0, 0))) fig = vis.get_figure() ax = vis.get_ax(poses_3d, fig) vis.plot_skeleton_and_scatter(poses_3d, ax) # print ('/data/vcl/sid/benedict/yt_videos/17j_poses_3d/'+k+'.mat') # mat_path = '/data/vcl/sid/benedict/yt_videos/17j_poses_3d/'+k+'.mat' # if not os.path.exists('/'.join(mat_path.split('/')[:-1])): # os.makedirs('/'.join(mat_path.split('/')[:-1])) # img_arr.append(img) # pose_arr.append(poses_3d) # mat_path = j+'/data.mat' if ".png" in j: mat_path = j.replace('.png', '.mat') ske_plot = j.replace('.png', '_ske.png') if ".jpg" in j: mat_path = j.replace('.jpg', '.mat') ske_plot = j.replace('.jpg', '_ske.jpg') plt.savefig(ske_plot, bbox_inches='tight', transparent=True) print(mat_path) sio.savemat(mat_path, {"pose_3d": poses_3d})
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)
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('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 joints_export.to_csv("hmr/output/csv/"+os.path.splitext(os.path.basename(img_path))[0]+".csv")
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])
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)
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))
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])