def get_3d_pkl_lrw( pkl, root, bbb=0 ): # the first cell is video path the last cell is the key frame nnuumber # ---- init PRN # os.environ['CUDA_VISIBLE_DEVICES'] = '0' # GPU number, -1 for CPU prn = PRN(is_dlib=True) _file = open(pkl, "rb") data = pickle.load(_file) _file.close() gg = len(data) data = data[int(gg * 1 * (bbb)):int(gg * 1 * (bbb + 1))] for kk, item in enumerate(data): print(kk) print(item) if os.path.exists(item[0] + '_original.obj'): continue target_id = item[-1] img_path = item[0] + '_%05d.png' % target_id print(img_path) target_frame = cv2.imread(img_path) target_frame = cv2.cvtColor(target_frame, cv2.COLOR_BGR2RGB) image = target_frame # read image [h, w, c] = image.shape pos = prn.process(image) # use dlib to detect face image = image / 255. if pos is None: print('+++++') continue # landmark kpt = prn.get_landmarks(pos) kpt[:, 1] = h - kpt[:, 1] np.save(item[0] + '_prnet.npy', kpt) # 3D vertices vertices = prn.get_vertices(pos) save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] # corresponding colors colors = prn.get_colors(image, vertices) # print (colors.shape) # print ('=========') # cv2.imwrite('./mask.png', colors * 255) write_obj_with_colors(item[0] + '_original.obj', save_vertices, prn.triangles, colors) #save 3d face(can open with meshlab)
def get_3d_single(video_path=None, target_id=None, img_path=None): # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = '0' # GPU number, -1 for CPU prn = PRN(is_dlib=True) if video_path != None: if not os.path.exists(video_path): print(video_path) print('+++++') if os.path.exists(video_path[:-4] + '.obj'): print('-----') cap = cv2.VideoCapture(video_path) for i in range(target_id): ret, frame = cap.read() ret, target_frame = cap.read() cv2.imwrite(video_path[:-4] + '_%05d.png' % target_id, target_frame) elif img_path != None: target_frame = cv2.imread(img_path) target_frame = cv2.cvtColor(target_frame, cv2.COLOR_BGR2RGB) image = target_frame # read image [h, w, c] = image.shape pos = prn.process(image) # use dlib to detect face image = image / 255. # landmark kpt = prn.get_landmarks(pos) kpt[:, 1] = h - kpt[:, 1] if video_path != None: np.save(video_path[:-4] + '_prnet.npy', kpt) else: np.save(img_path[:-4] + '_prnet.npy', kpt) # 3D vertices vertices = prn.get_vertices(pos) # save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] # corresponding colors colors = prn.get_colors(image, vertices) if video_path != None: write_obj_with_colors(video_path[:-4] + '_original.obj', save_vertices, prn.triangles, colors) #save 3d face(can open with meshlab) print('The generated 3d mesh model is stored in ' + video_path[:-4] + '_original.obj') else: write_obj_with_colors(img_path[:-4] + '_original.obj', save_vertices, prn.triangles, colors) #save 3d face(can open with meshlab) print('The generated 3d mesh model is stored in ' + img_path[:-4] + '_original.obj')
def get_3d_single_video( img_path): # you need the image path of the most visible frame. # root = # ---- init PRN # os.environ['CUDA_VISIBLE_DEVICES'] = '0' # GPU number, -1 for CPU prn = PRN(is_dlib=True) # _file = open(pkl, "rb") # data = pickle.load(_file) # _file.close() # gg = len(data) # data = data[int(gg * 0.2 *( bbb) ): int(gg * 0.2 * (bbb + 1) ) ] # for kk ,item in enumerate(data) : print(img_path) target_frame = cv2.imread(img_path) target_frame = cv2.cvtColor(target_frame, cv2.COLOR_BGR2RGB) image = target_frame # read image [h, w, c] = image.shape pos = prn.process(image) # use dlib to detect face image = image / 255. if pos is None: print('No pos') # landmark kpt = prn.get_landmarks(pos) kpt[:, 1] = h - kpt[:, 1] np.save(img_path[:-11] + '__prnet.npy', kpt) # 3D vertices vertices = prn.get_vertices(pos) save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] # corresponding colors colors = prn.get_colors(image, vertices) # print (colors.shape) # print ('=========') # cv2.imwrite('./mask.png', colors * 255) write_obj_with_colors(img_path[:-11] + '__original.obj', save_vertices, prn.triangles, colors) #save 3d face(can open with meshlab)
def extract_param(checkpoint_fp, root='', filelists=None, num_classes=62, device_ids=[0], batch_size=1, num_workers=0): map_location = {f'cuda:{i}': 'cuda:0' for i in range(8)} # checkpoint = torch.load(checkpoint_fp, map_location=map_location)['state_dict'] torch.cuda.set_device(device_ids[0]) model = PRN(checkpoint_fp) # model = nn.DataParallel(model, device_ids=device_ids).cuda() # model.load_state_dict(checkpoint) dataset = DDFATestDataset(filelists=filelists, root=root, transform=transforms.Compose([ToTensorGjz(), NormalizeGjz(mean=127.5, std=128)])) data_loader = data.DataLoader(dataset, batch_size=batch_size, num_workers=num_workers) cudnn.benchmark = True # model.eval() end = time.time() outputs = [] with torch.no_grad(): for _, inputs in enumerate(data_loader): inputs = inputs.cuda() # Get the output landmarks pos = model.net_forward(inputs) out = pos.cpu().detach().numpy() pos = np.squeeze(out) cropped_pos = pos * 255 pos = cropped_pos.transpose(1, 2, 0) if pos is None: continue # print(pos.shape) output = model.get_landmarks(pos) # print(output.shape) outputs.append(output) outputs = np.array(outputs, dtype=np.float32) print("outputs",outputs.shape) print(f'Extracting params take {time.time() - end: .3f}s') return outputs
def get_3d_folder( pkl ): # the first cell is video path the last cell is the key frame nnuumber # ---- init PRN # os.environ['CUDA_VISIBLE_DEVICES'] = '0' # GPU number, -1 for CPU prn = PRN(is_dlib=True) _file = open(pkl, "rb") data = pickle.load(_file) _file.close() gg = len(data) print(len(data)) # data = data[int(gg * 0.1 *bbb ): int(gg * 0.1 * (bbb + 1) ) ] for kk, item in enumerate(data): print(kk) target_id = item[-1] video_path = os.path.join(root, 'unzip', item[0]) if not os.path.exists(video_path): print(video_path) print('+++++') continue if os.path.exists(video_path[:-4] + '.obj'): print('-----') continue cap = cv2.VideoCapture(video_path) for i in range(target_id): ret, frame = cap.read() ret, target_frame = cap.read() cv2.imwrite(video_path[:-4] + '_%05d.png' % target_id, target_frame) target_frame = cv2.cvtColor(target_frame, cv2.COLOR_BGR2RGB) image = target_frame # read image [h, w, c] = image.shape pos = prn.process(image) # use dlib to detect face image = image / 255. if pos is None: continue # landmark kpt = prn.get_landmarks(pos) kpt[:, 1] = 224 - kpt[:, 1] np.save(video_path[:-4] + '_prnet.npy', kpt) # 3D vertices vertices = prn.get_vertices(pos) # save_vertices, p = frontalize(vertices) # np.save(video_path[:-4] + '_p.npy', p) # if os.path.exists(video_path[:-4] + '.obj'): # continue save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] # corresponding colors colors = prn.get_colors(image, vertices) # print (colors.shape) # print ('=========') # cv2.imwrite('./mask.png', colors * 255) write_obj_with_colors(video_path[:-4] + '_original.obj', save_vertices, prn.triangles, colors) #save 3d face(can open with meshlab)
def get_3d(bbb): # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = '0' # GPU number, -1 for CPU prn = PRN(is_dlib=True) # ------------- load data # frame_id = "test_video/id00419/3U0abyjM2Po/00024" # mesh_file = os.path.join(root, frame_id + ".obj") # rt_file = os.path.join(root, frame_id + "_sRT.npy") # image_path # _file = open(os.path.join(root, 'txt', "front_rt.pkl"), "rb") # data = pickle._Unpickler(_file) # data.encoding = 'latin1' # data = data.load() _file = open(os.path.join(root, 'txt', "front_rt.pkl"), "rb") data = pickle.load(_file) _file.close() gg = len(data) print(len(data)) data = data[int(gg * 0.1 * bbb):int(gg * 0.1 * (bbb + 1))] for kk, item in enumerate(data): print(kk) target_id = item[-1] video_path = os.path.join(root, 'unzip', item[0] + '.mp4') if not os.path.exists(video_path): print(video_path) print('+++++') continue if os.path.exists(video_path[:-4] + '.obj'): print('-----') continue cap = cv2.VideoCapture(video_path) for i in range(target_id): ret, frame = cap.read() ret, target_frame = cap.read() cv2.imwrite(video_path[:-4] + '_%05d.png' % target_id, target_frame) target_frame = cv2.cvtColor(target_frame, cv2.COLOR_BGR2RGB) image = target_frame # read image [h, w, c] = image.shape pos = prn.process(image) # use dlib to detect face image = image / 255. if pos is None: continue # landmark kpt = prn.get_landmarks(pos) kpt[:, 1] = 224 - kpt[:, 1] np.save(video_path[:-4] + '_prnet.npy', kpt) # 3D vertices vertices = prn.get_vertices(pos) # save_vertices, p = frontalize(vertices) # np.save(video_path[:-4] + '_p.npy', p) # if os.path.exists(video_path[:-4] + '.obj'): # continue save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] # corresponding colors colors = prn.get_colors(image, vertices) # print (colors.shape) # print ('=========') # cv2.imwrite('./mask.png', colors * 255) write_obj_with_colors(video_path[:-4] + '_original.obj', save_vertices, prn.triangles, colors) # save 3d face(can open with meshlab)
def main(_): # init os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu set_memory_growth() # load PRNet model cfg = load_yaml(FLAGS.cfg_path) model = PRN(cfg, is_dlib=True) # evaluation if not FLAGS.use_cam: # on test-img print( "[*] Processing on images in {}. Press 's' to save result.".format( FLAGS.img_path)) img_paths = glob.glob(os.path.join(FLAGS.img_path, '*')) for img_path in img_paths: img = cv2.imread(img_path) pos = model.process(img_path) if pos is None: continue vertices = model.get_vertices(pos) kpt = model.get_landmarks(pos) camera_matrix, _ = estimate_pose(vertices) cv2.imshow('Input', img) cv2.imshow('Sparse alignment', plot_kpt(img, kpt)) cv2.imshow('Dense alignment', plot_vertices(img, vertices)) cv2.imshow('Pose', plot_pose_box(img, camera_matrix, kpt)) cv2.moveWindow('Input', 0, 0) cv2.moveWindow('Sparse alignment', 500, 0) cv2.moveWindow('Dense alignment', 1000, 0) cv2.moveWindow('Pose', 1500, 0) key = cv2.waitKey(0) if key == ord('q'): exit() elif key == ord('s'): cv2.imwrite( os.path.join(FLAGS.save_path, os.path.basename(img_path)), plot_kpt(img, kpt)) print("Result saved in {}".format(FLAGS.save_path)) else: # webcam demo cap = cv2.VideoCapture(0) start_time = time.time() count = 1 while (True): _, image = cap.read() pos = model.process(image) fps_str = 'FPS: %.2f' % (1 / (time.time() - start_time)) start_time = time.time() cv2.putText(image, fps_str, (25, 25), cv2.FONT_HERSHEY_DUPLEX, 0.75, (0, 255, 0), 2) cv2.imshow('Input', image) cv2.moveWindow('Input', 0, 0) key = cv2.waitKey(1) if pos is None: cv2.waitKey(1) cv2.destroyWindow('Sparse alignment') cv2.destroyWindow('Dense alignment') cv2.destroyWindow('Pose') if key & 0xFF == ord('q'): break continue else: vertices = model.get_vertices(pos) kpt = model.get_landmarks(pos) camera_matrix, _ = estimate_pose(vertices) result_list = [ plot_kpt(image, kpt), plot_vertices(image, vertices), plot_pose_box(image, camera_matrix, kpt) ] cv2.imshow('Sparse alignment', result_list[0]) cv2.imshow('Dense alignment', result_list[1]) cv2.imshow('Pose', result_list[2]) cv2.moveWindow('Sparse alignment', 500, 0) cv2.moveWindow('Dense alignment', 1000, 0) cv2.moveWindow('Pose', 1500, 0) if key & 0xFF == ord('s'): image_name = 'prnet_cam_' + str(count) save_path = FLAGS.save_path cv2.imwrite( os.path.join(save_path, image_name + '_result.jpg'), np.concatenate(result_list, axis=1)) cv2.imwrite( os.path.join(save_path, image_name + '_image.jpg'), image) count += 1 print("Result saved in {}".format(FLAGS.save_path)) if key & 0xFF == ord('q'): break
def main(args): if args.isShow: args.isOpencv = True from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box if args.isObj: from utils.write import write_obj if args.isPose: from utils.estimate_pose import estimate_pose # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # GPU number, -1 for CPU prn = PRN(is_dlib=args.isDlib, is_opencv=args.isOpencv) # ------------- load data image_folder = args.inputFolder save_folder = args.outputFolder if not os.path.exists(save_folder): os.mkdir(save_folder) types = ('*.jpg', '*.png') image_path_list = [] for files in types: image_path_list.extend(glob(os.path.join(image_folder, files))) total_num = len(image_path_list) for i, image_path in enumerate(image_path_list): name = image_path.strip().split('/')[-1][:-4] # read image image = imread(image_path) # the core: regress position map pos = prn.process(image) # use dlib to detect face if args.isObj or args.isShow: # 3D vertices vertices = prn.get_vertices(pos) # corresponding colors colors = prn.get_colors(image, vertices) write_obj(os.path.join(save_folder, name + '.obj'), vertices, colors, prn.triangles) #save 3d face(can open with meshlab) if args.isKpt or args.isShow: # get landmarks kpt = prn.get_landmarks(pos) np.savetxt(os.path.join(save_folder, name + '_kpt.txt'), kpt) if args.isPose or args.isShow: # estimate pose camera_matrix, pose = estimate_pose(vertices) np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose) if args.isShow: # ---------- Plot image_pose = plot_pose_box(image, camera_matrix, kpt) cv2.imshow('sparse alignment', plot_kpt(image, kpt)) cv2.imshow('dense alignment', plot_vertices(image, vertices)) cv2.imshow('pose', plot_pose_box(image, camera_matrix, kpt)) cv2.waitKey(0)
def get_3d_single(video_path=None, target_id=None, img_path=None, device_id='3'): # ---- init PRN target_id = count_frames(video_path) os.environ['CUDA_VISIBLE_DEVICES'] = device_id # GPU number, -1 for CPU prn = PRN(is_dlib=True) # if video_path != None: # if not os.path.exists(video_path): # print (video_path) # print ('+++++') # if os.path.exists(video_path[:-4] + '.obj'): # print ('-----') # cap = cv2.VideoCapture(video_path) # for i in range(target_id): # ret, frame = cap.read() # ret, target_frame = cap.read() # cv2.imwrite(video_path[:-4] + '_%05d.png'%target_id,target_frame) # elif img_path != None: # target_frame = cv2.imread(img_path) # target_frame = cv2.cvtColor(target_frame, cv2.COLOR_BGR2RGB) cap = cv2.VideoCapture(video_path) for i in range(target_id): ret, frame = cap.read() print(target_path[:-4] + '_%05d.png' % i) cv2.imwrite(target_path[:-4] + '_%05d.png' % i, frame) # tt = cv2.imread(target_path[:-4] + '_%05d.png' % i) # target_frame = cv2.cvtColor(cv2.imread(target_path[:-4] + '_%05d.png' % i), cv2.COLOR_BGR2RGB) target_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = target_frame [h, w, c] = image.shape pos = prn.process(image) image = image / 255 kpt = prn.get_landmarks(pos) kpt[:, 1] = 224 - kpt[:, 1] if video_path is not None: print(target_path[:-4] + '_%05d' % i + '_prnet.npy') np.save(target_path[:-4] + '_%05d' % i + '_prnet.npy', kpt) else: np.save(img_path[:-4] + '_%05d' % i + '_prnet.npy', kpt) vertices = prn.get_vertices(pos) # save_vertices, p = frontalize(vertices) # np.save(video_path[:-4] + '_p.npy', p) # if os.path.exists(video_path[:-4] + '.obj'): # continue save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] # corresponding colors colors = prn.get_colors(image, vertices) # print (colors.shape) # print ('=========') # cv2.imwrite('./mask.png', colors * 255) if video_path != None: write_obj_with_colors( target_path[:-4] + '_%05d' % i + '_original.obj', save_vertices, prn.triangles, colors) # save 3d face(can open with meshlab) else: write_obj_with_colors( img_path[:-4] + '_%05d' % i + '_original.obj', save_vertices, prn.triangles, colors) # save 3d face(can open with meshlab)
image_path_list.extend(glob(os.path.join(image_folder, files))) total_num = len(image_path_list) for i, image_path in enumerate(image_path_list): # read image image = imread(image_path) # the core: regress position map if 'AFLW2000' in image_path: mat_path = image_path.replace('jpg', 'mat') info = sio.loadmat(mat_path) kpt = info['pt3d_68'] pos = prn.process(image, kpt) # kpt information is only used for detecting face and cropping image else: pos = prn.process(image) # use dlib to detect face # -- Basic Applications # get landmarks kpt = prn.get_landmarks(pos) # 3D vertices vertices = prn.get_vertices(pos) # corresponding colors colors = prn.get_colors(image, vertices) # -- save name = image_path.strip().split('/')[-1][:-4] np.savetxt(os.path.join(save_folder, name + '.txt'), kpt) write_obj(os.path.join(save_folder, name + '.obj'), vertices, colors, prn.triangles) #save 3d face(can open with meshlab) sio.savemat(os.path.join(save_folder, name + '_mesh.mat'), {'vertices': vertices, 'colors': colors, 'triangles': prn.triangles})
def main(args): if args.isShow or args.isTexture: import cv2 from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # GPU number, -1 for CPU prn = PRN(is_dlib = args.isDlib) # ------------- load data image_folder = args.inputDir print(image_folder) save_folder = args.outputDir print(save_folder) if not os.path.exists(save_folder): os.mkdir(save_folder) meta_save_folder = os.path.join(save_folder, 'meta') if not os.path.exists(meta_save_folder): os.mkdir(meta_save_folder) types = ('*.jpg', '*.png', '*,JPG') image_path_list= find_files(image_folder, ('.jpg', '.png', '.JPG')) total_num = len(image_path_list) print(image_path_list) for i, image_path in enumerate(image_path_list): name = image_path.strip().split('/')[-1][:-4] print(image_path) # read image image = imread(image_path) [h, w, c] = image.shape if c>3: image = image[:,:,:3] # the core: regress position map if args.isDlib: max_size = max(image.shape[0], image.shape[1]) if max_size> 1000: image = rescale(image, 1000./max_size) image = (image*255).astype(np.uint8) pos = prn.process(image) # use dlib to detect face else: if image.shape[1] == image.shape[2]: image = resize(image, (256,256)) pos = prn.net_forward(image/255.) # input image has been cropped to 256x256 else: box = np.array([0, image.shape[1]-1, 0, image.shape[0]-1]) # cropped with bounding box pos = prn.process(image, box) image = image/255. if pos is None: continue if args.is3d or args.isMat or args.isPose or args.isShow: # 3D vertices vertices = prn.get_vertices(pos) if args.isFront: save_vertices = frontalize(vertices) else: save_vertices = vertices.copy() save_vertices[:,1] = h - 1 - save_vertices[:,1] if args.isImage: imsave(os.path.join(save_folder, name + '.jpg'), image) if args.is3d: # corresponding colors colors = prn.get_colors(image, vertices) if args.isTexture: if args.texture_size != 256: pos_interpolated = resize(pos, (args.texture_size, args.texture_size), preserve_range = True) else: pos_interpolated = pos.copy() texture = cv2.remap(image, pos_interpolated[:,:,:2].astype(np.float32), None, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT,borderValue=(0)) if args.isMask: vertices_vis = get_visibility(vertices, prn.triangles, h, w) uv_mask = get_uv_mask(vertices_vis, prn.triangles, prn.uv_coords, h, w, prn.resolution_op) uv_mask = resize(uv_mask, (args.texture_size, args.texture_size), preserve_range = True) texture = texture*uv_mask[:,:,np.newaxis] write_obj_with_texture(os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, texture, prn.uv_coords/prn.resolution_op)#save 3d face with texture(can open with meshlab) else: write_obj_with_colors(os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, colors) #save 3d face(can open with meshlab) if args.isDepth: depth_image = get_depth_image(vertices, prn.triangles, h, w, True) depth = get_depth_image(vertices, prn.triangles, h, w) imsave(os.path.join(save_folder, name + '_depth.jpg'), depth_image) sio.savemat(os.path.join(meta_save_folder, name + '_depth.mat'), {'depth':depth}) if args.isMat: sio.savemat(os.path.join(meta_save_folder, name + '_mesh.mat'), {'vertices': vertices, 'colors': colors, 'triangles': prn.triangles}) if args.isKpt or args.isShow: # get landmarks kpt = prn.get_landmarks(pos) # pdb.set_trace() np.save(os.path.join(meta_save_folder, name + '_kpt.npy'), kpt) # cv2.imwrite(os.path.join(save_folder, name + '_skpt.jpg'), plot_kpt(image, kpt)) if args.isPose or args.isShow: # estimate pose camera_matrix, pose = estimate_pose(vertices) np.savetxt(os.path.join(meta_save_folder, name + '_pose.txt'), pose) np.savetxt(os.path.join(meta_save_folder, name + '_camera_matrix.txt'), camera_matrix) if args.isShow: # ---------- Plot image = imread(os.path.join(save_folder, name + '.jpg')) image_pose = plot_pose_box(image, camera_matrix, kpt) #cv2.imwrite(os.path.join(save_folder, name + '_pose.jpg'), plot_kpt(image, kpt)) #cv2.imwrite(os.path.join(save_folder, name + '_camera_matrix.jpg'), plot_vertices(image, vertices)) #cv2.imwrite(os.path.join(save_folder, name + '_pose.jpg'), plot_pose_box(image, camera_matrix, kpt)) image = imread(os.path.join(save_folder, name + '.jpg')) b, g, r = cv2.split(image) image = cv2.merge([r,g,b])
image = imread(image_path) # the core: regress position map if 'AFLW2000' in image_path: mat_path = image_path.replace('jpg', 'mat') info = sio.loadmat(mat_path) kpt = info['pt3d_68'] pos = prn.process( image, kpt ) # kpt information is only used for detecting face and cropping image else: pos = prn.process(image) # use dlib to detect face # -- Basic Applications # get landmarks kpt = prn.get_landmarks(pos) # 3D vertices vertices = prn.get_vertices(pos) # corresponding colors colors = prn.get_colors(image, vertices) # -- save name = image_path.strip().split('/')[-1][:-4] np.savetxt(os.path.join(save_folder, name + '.txt'), kpt) write_obj(os.path.join(save_folder, name + '.obj'), vertices, colors, prn.triangles) #save 3d face(can open with meshlab) sio.savemat(os.path.join(save_folder, name + '_mesh.mat'), { 'vertices': vertices, 'colors': colors, 'triangles': prn.triangles
def main(args): if args.isShow or args.isTexture or args.isCamera: import cv2 from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # GPU number, -1 for CPU prn = PRN(is_dlib=args.isDlib) # ------------- load data image_folder = args.inputDir save_folder = args.outputDir if not os.path.exists(save_folder): os.mkdir(save_folder) types = ('*.jpg', '*.png') image_path_list = [] for files in types: image_path_list.extend(glob(os.path.join(image_folder, files))) total_num = len(image_path_list) if args.isCamera: # Create a VideoCapture object and read from input file # If the input is the camera, pass 0 instead of the video file name cap = cv2.VideoCapture(0) # Check if camera opened successfully if (cap.isOpened() == False): print("Error opening video stream or file") # Read until video is completed while (cap.isOpened()): # Capture frame-by-frame ret, frame = cap.read() if ret == True: if args.isDlib: max_size = max(frame.shape[0], frame.shape[1]) if max_size > 1000: frame = rescale(frame, 1000. / max_size) frame = (frame * 255).astype(np.uint8) pos = prn.process(frame) # use dlib to detect face else: if frame.shape[0] == frame.shape[1]: frame = resize(frame, (256, 256)) pos = prn.net_forward( frame / 255.) # input frame has been cropped to 256x256 else: box = np.array( [0, frame.shape[1] - 1, 0, frame.shape[0] - 1]) # cropped with bounding box pos = prn.process(frame, box) # Normalizing the frame and skiping if there was no one in the frame frame = frame / 255. if pos is None: continue # Get landmarks in frame kpt = prn.get_landmarks(pos) # Display the resulting frame cv2.imshow('sparse alignment', plot_kpt(frame, kpt)) # Press Q on keyboard to exit if cv2.waitKey(25) & 0xFF == ord('q'): break # Break the loop else: break # When everything done, release the video capture object cap.release() # Closes all the frames cv2.destroyAllWindows() else: for i, image_path in enumerate(image_path_list): name = image_path.strip().split('/')[-1][:-4] # read image image = imread(image_path) [h, w, c] = image.shape if c > 3: image = image[:, :, :3] # the core: regress position map if args.isDlib: max_size = max(image.shape[0], image.shape[1]) if max_size > 1000: image = rescale(image, 1000. / max_size) image = (image * 255).astype(np.uint8) pos = prn.process(image) # use dlib to detect face else: if image.shape[0] == image.shape[1]: image = resize(image, (256, 256)) pos = prn.net_forward( image / 255.) # input image has been cropped to 256x256 else: box = np.array( [0, image.shape[1] - 1, 0, image.shape[0] - 1]) # cropped with bounding box pos = prn.process(image, box) image = image / 255. if pos is None: continue if args.is3d or args.isMat or args.isPose or args.isShow: # 3D vertices vertices = prn.get_vertices(pos) if args.isFront: save_vertices = frontalize(vertices) else: save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] if args.isImage: imsave(os.path.join(save_folder, name + '.jpg'), image) if args.is3d: # corresponding colors colors = prn.get_colors(image, vertices) if args.isTexture: if args.texture_size != 256: pos_interpolated = resize( pos, (args.texture_size, args.texture_size), preserve_range=True) else: pos_interpolated = pos.copy() texture = cv2.remap(image, pos_interpolated[:, :, :2].astype( np.float32), None, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0)) if args.isMask: vertices_vis = get_visibility(vertices, prn.triangles, h, w) uv_mask = get_uv_mask(vertices_vis, prn.triangles, prn.uv_coords, h, w, prn.resolution_op) uv_mask = resize( uv_mask, (args.texture_size, args.texture_size), preserve_range=True) texture = texture * uv_mask[:, :, np.newaxis] write_obj_with_texture( os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, texture, prn.uv_coords / prn.resolution_op ) #save 3d face with texture(can open with meshlab) else: write_obj_with_colors( os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, colors) #save 3d face(can open with meshlab) if args.isDepth: depth_image = get_depth_image(vertices, prn.triangles, h, w, True) depth = get_depth_image(vertices, prn.triangles, h, w) imsave(os.path.join(save_folder, name + '_depth.jpg'), depth_image) sio.savemat(os.path.join(save_folder, name + '_depth.mat'), {'depth': depth}) if args.isMat: sio.savemat( os.path.join(save_folder, name + '_mesh.mat'), { 'vertices': vertices, 'colors': colors, 'triangles': prn.triangles }) if args.isKpt or args.isShow: # get landmarks kpt = prn.get_landmarks(pos) np.savetxt(os.path.join(save_folder, name + '_kpt.txt'), kpt) if args.isPose or args.isShow: # estimate pose camera_matrix, pose = estimate_pose(vertices) np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose) np.savetxt( os.path.join(save_folder, name + '_camera_matrix.txt'), camera_matrix) np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose) if args.isShow: # ---------- Plot image_pose = plot_pose_box(image, camera_matrix, kpt) cv2.imshow( 'sparse alignment', cv2.cvtColor(np.float32(plot_kpt(image, kpt)), cv2.COLOR_RGB2BGR)) cv2.imshow( 'dense alignment', cv2.cvtColor(np.float32(plot_vertices(image, vertices)), cv2.COLOR_RGB2BGR)) cv2.imshow( 'pose', cv2.cvtColor( np.float32(plot_pose_box(image, camera_matrix, kpt)), cv2.COLOR_RGB2BGR)) cv2.waitKey(0)
def main(data_dir): # 1) Create Dataset of 300_WLP & Dataloader. wlp300 = PRNetDataset(root_dir=data_dir, transform=transforms.Compose([ToTensor(), ToNormalize(FLAGS["normalize_mean"], FLAGS["normalize_std"])])) wlp300_dataloader = DataLoader(dataset=wlp300, batch_size=FLAGS['batch_size'], shuffle=True, num_workers=0) # 2) Intermediate Processing. transform_img = transforms.Compose([ # transforms.ToTensor(), transforms.Normalize(FLAGS["normalize_mean"], FLAGS["normalize_std"]) ]) # # 3) Create PRNet model. # start_epoch, target_epoch = FLAGS['start_epoch'], FLAGS['target_epoch'] # model = ResFCN256() # # # Load the pre-trained weight # if FLAGS['resume'] and os.path.exists(os.path.join(FLAGS['images'], "3channels.pth")): # state = torch.load(os.path.join(FLAGS['images'], "3channels.pth")) # model.load_state_dict(state['prnet']) # start_epoch = state['start_epoch'] # INFO("Load the pre-trained weight! Start from Epoch", start_epoch) # # model.to("cuda") prn = PRN(os.path.join(FLAGS['images'], "3channels.pth")) bar = tqdm(wlp300_dataloader) nme_list = [] for i, sample in enumerate(bar): uv_map, origin = sample['uv_map'].to(FLAGS['device']), sample['origin'].to(FLAGS['device']) # print(origin.shape) # Inference. # origin = cv2.resize(origin, (256, 256)) # origin = transform_img(origin) # origin = origin.unsqueeze(0) uv_map_result = prn.net_forward(origin.cuda()) out = uv_map_result.cpu().detach().numpy() uv_map_result = np.squeeze(out) cropped_pos = uv_map_result * 255 uv_map_result = cropped_pos.transpose(1, 2, 0) out = uv_map.cpu().detach().numpy() uv_map = np.squeeze(out) cropped_pos = uv_map * 255 uv_map = cropped_pos.transpose(1, 2, 0) kpt_predicted = prn.get_landmarks(uv_map_result)[:, :2] kpt_gt = prn.get_landmarks(uv_map)[:, :2] nme_sum = 0 for j in range(kpt_gt.shape[0]): x = kpt_gt[j][0] - kpt_predicted[j][0] y = kpt_gt[j][1] - kpt_predicted[j][1] L2_norm = math.sqrt(math.pow(x, 2) + math.pow(y, 2)) # bounding box size has been fixed to 256x256 d = 256*256 error = L2_norm/d nme_sum += error nme_list.append(nme_sum/68) print(np.mean(nme_list))
def main(args): if args.isShow or args.isTexture: import cv2 from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # GPU number, -1 for CPU prn = PRN(is_dlib=args.isDlib, is_faceboxes=args.isFaceBoxes) # ---- load data image_folder = args.inputDir save_folder = args.outputDir if not os.path.exists(save_folder): os.mkdir(save_folder) types = ('*.jpg', '*.png') image_path_list = [] for files in types: image_path_list.extend(glob(os.path.join(image_folder, files))) total_num = len(image_path_list) for i, image_path in enumerate(image_path_list): name = image_path.strip().split('/')[-1][:-4] # read image image = imread(image_path) [h, w, c] = image.shape if c > 3: image = image[:, :, :3] # RGBA图中,去除A通道 # the core: regress position map if args.isDlib: max_size = max(image.shape[0], image.shape[1]) if max_size > 1000: image = rescale(image, 1000. / max_size) image = (image * 255).astype(np.uint8) pos = prn.process(image) # use dlib to detect face elif args.isFaceBoxes: pos, cropped_img = prn.process( image) # use faceboxes to detect face else: if image.shape[0] == image.shape[1]: image = resize(image, (256, 256)) pos = prn.net_forward( image / 255.) # input image has been cropped to 256x256 else: box = np.array([0, image.shape[1] - 1, 0, image.shape[0] - 1 ]) # cropped with bounding box pos = prn.process(image, box) image = image / 255. if pos is None: continue if args.is3d or args.isMat or args.isPose or args.isShow: # 3D vertices vertices = prn.get_vertices(pos) if args.isFront: save_vertices = frontalize(vertices) else: save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] # 三维人脸旋转对齐方法 # if args.isImage: # vertices = prn.get_vertices(pos) # scale_init = 180 / (np.max(vertices[:, 1]) - np.min(vertices[:, 1])) # colors = prn.get_colors(image, vertices) # triangles = prn.triangles # camera_matrix, pose = estimate_pose(vertices) # yaw, pitch, roll = pos * ANGULAR # vertices1 = vertices - np.mean(vertices, 0)[np.newaxis, :] # # obj = {'s': scale_init, 'angles': [-pitch, yaw, -roll + 180], 't': [0, 0, 0]} # camera = {'eye':[0, 0, 256], 'proj_type':'perspective', 'at':[0, 0, 0], # 'near': 1000, 'far':-100, 'fovy':30, 'up':[0,1,0]} # # image1 = transform_test(vertices1, obj, camera, triangles, colors, h=256, w=256) * 255 # image1 = image1.astype(np.uint8) # imsave(os.path.join(save_folder, name + '.jpg'), image1) if args.is3d: # corresponding colors colors = prn.get_colors(image, vertices) if args.isTexture: if args.texture_size != 256: pos_interpolated = resize( pos, (args.texture_size, args.texture_size), preserve_range=True) else: pos_interpolated = pos.copy() texture = cv2.remap(image, pos_interpolated[:, :, :2].astype( np.float32), None, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0)) if args.isMask: vertices_vis = get_visibility(vertices, prn.triangles, h, w) uv_mask = get_uv_mask(vertices_vis, prn.triangles, prn.uv_coords, h, w, prn.resolution_op) uv_mask = resize(uv_mask, (args.texture_size, args.texture_size), preserve_range=True) texture = texture * uv_mask[:, :, np.newaxis] write_obj_with_texture( os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, texture, prn.uv_coords / prn.resolution_op ) #save 3d face with texture(can open with meshlab) else: write_obj_with_colors( os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, colors) #save 3d face(can open with meshlab) if args.isDepth: depth_image = get_depth_image(vertices, prn.triangles, h, w, True) depth = get_depth_image(vertices, prn.triangles, h, w) imsave(os.path.join(save_folder, name + '_depth.jpg'), depth_image) sio.savemat(os.path.join(save_folder, name + '_depth.mat'), {'depth': depth}) if args.isMat: sio.savemat(os.path.join(save_folder, name + '_mesh.mat'), { 'vertices': vertices, 'colors': colors, 'triangles': prn.triangles }) if args.isKpt: # get landmarks kpt = prn.get_landmarks(pos) np.savetxt(os.path.join(save_folder, name + '_kpt.txt'), kpt) if args.is2dKpt and args.is68Align: ori_kpt = prn.get_landmarks_2d(pos) dlib_aligner = DlibAlign() dst_img = dlib_aligner.dlib_68_align(image, ori_kpt, 256, 0.5) imsave(os.path.join(save_folder, name + '.jpg'), dst_img) if args.isPose: # estimate pose camera_matrix, pose, rot = estimate_pose(vertices) np.savetxt(os.path.join(save_folder, name + '_pose.txt'), np.array(pose) * ANGULAR) np.savetxt(os.path.join(save_folder, name + '_camera_matrix.txt'), camera_matrix) if args.isShow: kpt = prn.get_landmarks(pos) cv2.imshow('sparse alignment', plot_kpt(image, kpt)) # cv2.imshow('dense alignment', plot_vertices(image, vertices)) # cv2.imshow('pose', plot_pose_box(image, camera_matrix, kpt)) cv2.waitKey(1)
def main(args): if args.isShow or args.isTexture: import cv2 from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box # ---- transform transform_img = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(FLAGS["normalize_mean"], FLAGS["normalize_std"]) ]) # ---- init PRN prn = PRN(args.model) # ------------- load data image_folder = args.inputDir save_folder = args.outputDir if not os.path.exists(save_folder): os.mkdir(save_folder) types = ('*.jpg', '*.png') image_path_list = [] for files in types: image_path_list.extend(glob(os.path.join(image_folder, files))) total_num = len(image_path_list) print("#" * 25) print("[PRNet Inference] {} picture were under processing~".format( total_num)) print("#" * 25) for i, image_path in enumerate(image_path_list): name = image_path.strip().split('/')[-1][:-4] # read image image = cv2.imread(image_path) [h, w, c] = image.shape # the core: regress position map image = cv2.resize(image, (256, 256)) image_t = transform_img(image) image_t = image_t.unsqueeze(0) pos = prn.net_forward( image_t) # input image has been cropped to 256x256 out = pos.cpu().detach().numpy() pos = np.squeeze(out) cropped_pos = pos * 255 pos = cropped_pos.transpose(1, 2, 0) if pos is None: continue if args.is3d or args.isMat or args.isPose or args.isShow: # 3D vertices vertices = prn.get_vertices(pos) if args.isFront: save_vertices = frontalize(vertices) else: save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] if args.isImage: cv2.imwrite(os.path.join(save_folder, name + '.jpg'), image) if args.is3d: # corresponding colors colors = prn.get_colors(image, vertices) if args.isTexture: if args.texture_size != 256: pos_interpolated = cv2.resize( pos, (args.texture_size, args.texture_size), preserve_range=True) else: pos_interpolated = pos.copy() texture = cv2.remap(image, pos_interpolated[:, :, :2].astype( np.float32), None, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0)) if args.isMask: vertices_vis = get_visibility(vertices, prn.triangles, h, w) uv_mask = get_uv_mask(vertices_vis, prn.triangles, prn.uv_coords, h, w, prn.resolution_op) uv_mask = cv2.resize( uv_mask, (args.texture_size, args.texture_size), preserve_range=True) texture = texture * uv_mask[:, :, np.newaxis] write_obj_with_texture( os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, texture, prn.uv_coords / prn.resolution_op ) # save 3d face with texture(can open with meshlab) else: write_obj_with_colors( os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, colors) # save 3d face(can open with meshlab) # if args.isDepth: # depth_image = get_depth_image(vertices, prn.triangles, h, w, True) # depth = get_depth_image(vertices, prn.triangles, h, w) # cv2.imwrite(os.path.join(save_folder, name + '_depth.jpg'), depth_image) # sio.savemat(os.path.join(save_folder, name + '_depth.mat'), {'depth': depth}) if args.isKpt or args.isShow: # get landmarks kpt = prn.get_landmarks(pos) np.savetxt(os.path.join(save_folder, name + '_kpt.txt'), kpt) if args.isPose or args.isShow: # estimate pose camera_matrix, pose = estimate_pose(vertices) np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose) np.savetxt(os.path.join(save_folder, name + '_camera_matrix.txt'), camera_matrix) np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose) if args.isShow: # ---------- Plot image_pose = plot_pose_box(image, camera_matrix, kpt) cv2.imshow('sparse alignment', plot_kpt(image, kpt)) cv2.imshow('dense alignment', plot_vertices(image, vertices)) cv2.imshow('pose', plot_pose_box(image, camera_matrix, kpt)) cv2.waitKey(0)
def main(args): if args.isShow or args.isTexture: import cv2 from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # GPU number, -1 for CPU prn = PRN(is_dlib = args.isDlib) # ------------- load data image_folder = args.inputDir save_folder = args.outputDir if not os.path.exists(save_folder): os.mkdir(save_folder) # types = ('*.jpg', '*.png') # image_path_list= [] # for files in types: # image_path_list.extend(glob(os.path.join(image_folder, files))) # total_num = len(image_path_list) for dir, dirs, files in sorted(os.walk(image_folder)): for file in files: image_path = os.path.join(dir, file) dir = dir.replace("\\", "/") new_dir = dir.replace(image_folder, save_folder) if not os.path.isdir(new_dir): os.mkdir(new_dir) name = image_path.replace(image_folder, save_folder) print('data path:', name) # read image image = imread(image_path) [h, w, c] = image.shape if c>3: image = image[:,:,:3] # the core: regress position map if args.isDlib: max_size = max(image.shape[0], image.shape[1]) if max_size> 1000: image = rescale(image, 1000./max_size) image = (image*255).astype(np.uint8) pos = prn.process(image) # use dlib to detect face else: # if image.shape[0] == image.shape[1]: # image = resize(image, (256,256)) # pos = prn.net_forward(image/255.) # input image has been cropped to 256x256 # else: box = np.array([0, image.shape[1]-1, 0, image.shape[0]-1]) # cropped with bounding box pos = prn.process(image, box) image = image/255. if pos is None: continue if args.is3d or args.isMat or args.isPose or args.isShow: # 3D vertices vertices = prn.get_vertices(pos) if args.isFront: save_vertices = frontalize(vertices) else: save_vertices = vertices.copy() save_vertices[:,1] = h - 1 - save_vertices[:,1] if args.isImage: imsave(name, image) if args.is3d: # corresponding colors colors = prn.get_colors(image, vertices) if args.isTexture: if args.texture_size != 256: pos_interpolated = resize(pos, (args.texture_size, args.texture_size), preserve_range = True) else: pos_interpolated = pos.copy() texture = cv2.remap(image, pos_interpolated[:,:,:2].astype(np.float32), None, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT,borderValue=(0)) if args.isMask: vertices_vis = get_visibility(vertices, prn.triangles, h, w) uv_mask = get_uv_mask(vertices_vis, prn.triangles, prn.uv_coords, h, w, prn.resolution_op) uv_mask = resize(uv_mask, (args.texture_size, args.texture_size), preserve_range = True) texture = texture*uv_mask[:,:,np.newaxis] write_obj_with_texture(name.replace('.jpg', '.obj'), save_vertices, prn.triangles, texture, prn.uv_coords/prn.resolution_op)#save 3d face with texture(can open with meshlab) else: write_obj_with_colors(name.replace('.jpg', '.obj'), save_vertices, prn.triangles, colors) #save 3d face(can open with meshlab) filepath = name.replace('.jpg', '.obj') filepath = filepath.replace("\\", "/") print('filepath:', filepath) new_dir = dir.replace(args.inputDir, args.renderDir) # print(new_dir + '/' + file) if not os.path.isdir(new_dir): os.mkdir(new_dir) color_image1, _ = render_scene(filepath, 4.0, 0.0, 3.0) color_image2, _ = render_scene(filepath, 4.0, np.pi / 18.0, 3.0) color_image3, _ = render_scene(filepath, 4.0, np.pi / 9.0, 3.0) if color_image1 is None or color_image2 is None: continue new_path = filepath.replace(args.outputDir, args.renderDir) # print('new_path:', new_path) save_image(new_path, '_40_', color_image1) save_image(new_path, '_50_', color_image2) save_image(new_path, '_60_', color_image3) os.remove(name.replace('.jpg', '.obj')) if args.isDepth: depth_image = get_depth_image(vertices, prn.triangles, h, w, True) depth = get_depth_image(vertices, prn.triangles, h, w) imsave(os.path.join(name.replace('.jpg', '_depth.jpg')), depth_image) sio.savemat(name.replace('.jpg', '_depth.mat'), {'depth': depth}) if args.isMat: sio.savemat(name.replace('.jpg', '_mesh.mat'), {'vertices': vertices, 'colors': colors, 'triangles': prn.triangles}) if args.isKpt or args.isShow: # get landmarks kpt = prn.get_landmarks(pos) np.savetxt(name.replace('.jpg', '_kpt.txt'), kpt) if args.isPose or args.isShow: # estimate pose camera_matrix, pose = estimate_pose(vertices) np.savetxt(name.replace('.jpg', '_pose.txt'), pose) np.savetxt(name.replace('.jpg', '_camera_matrix.txt'), camera_matrix) np.savetxt(name.replace('.jpg', '_pose.txt'), pose) if args.isShow: # ---------- Plot image_pose = plot_pose_box(image, camera_matrix, kpt) cv2.imshow('sparse alignment', plot_kpt(image, kpt)) cv2.imshow('dense alignment', plot_vertices(image, vertices)) cv2.imshow('pose', plot_pose_box(image, camera_matrix, kpt)) cv2.waitKey(0)
def main(args): if args.isShow or args.isTexture: import cv2 from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # GPU number, -1 for CPU prn = PRN(is_dlib = args.isDlib) # ------------- load data image_folder = args.inputDir save_folder = args.outputDir if not os.path.exists(save_folder): os.mkdir(save_folder) types = ('*.jpg', '*.png') image_path_list= [] for files in types: image_path_list.extend(glob(os.path.join(image_folder, files))) total_num = len(image_path_list) for i, image_path in enumerate(image_path_list): name = image_path.strip().split('/')[-1][:-4] # read image image = imread(image_path) [h, w, _] = image.shape # the core: regress position map if args.isDlib: max_size = max(image.shape[0], image.shape[1]) if max_size> 1000: image = rescale(image, 1000./max_size) pos = prn.process(image) # use dlib to detect face else: if image.shape[1] == image.shape[2]: image = resize(image, (256,256)) pos = prn.net_forward(image/255.) # input image has been cropped to 256x256 else: box = np.array([0, image.shape[1]-1, 0, image.shape[0]-1]) # cropped with bounding box pos = prn.process(image, box) image = image/255. if pos is None: continue if args.is3d or args.isMat or args.isPose or args.isShow: # 3D vertices vertices = prn.get_vertices(pos) if args.isFront: save_vertices = frontalize(vertices) else: save_vertices = vertices if args.isImage: imsave(os.path.join(save_folder, name + '.jpg'), image) if args.is3d: # corresponding colors colors = prn.get_colors(image, vertices) if args.isTexture: texture = cv2.remap(image, pos[:,:,:2].astype(np.float32), None, interpolation=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT,borderValue=(0)) if args.isMask: vertices_vis = get_visibility(vertices, prn.triangles, h, w) uv_mask = get_uv_mask(vertices_vis, prn.triangles, prn.uv_coords, h, w, prn.resolution_op) texture = texture*uv_mask[:,:,np.newaxis] write_obj_with_texture(os.path.join(save_folder, name + '.obj'), save_vertices, colors, prn.triangles, texture, prn.uv_coords/prn.resolution_op)#save 3d face with texture(can open with meshlab) else: write_obj(os.path.join(save_folder, name + '.obj'), save_vertices, colors, prn.triangles) #save 3d face(can open with meshlab) if args.isDepth: depth_image = get_depth_image(vertices, prn.triangles, h, w) imsave(os.path.join(save_folder, name + '_depth.jpg'), depth_image) if args.isMat: sio.savemat(os.path.join(save_folder, name + '_mesh.mat'), {'vertices': save_vertices, 'colors': colors, 'triangles': prn.triangles}) if args.isKpt or args.isShow: # get landmarks kpt = prn.get_landmarks(pos) np.savetxt(os.path.join(save_folder, name + '_kpt.txt'), kpt) if args.isPose or args.isShow: # estimate pose camera_matrix, pose = estimate_pose(vertices) np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose) if args.isShow: # ---------- Plot image_pose = plot_pose_box(image, camera_matrix, kpt) cv2.imshow('sparse alignment', plot_kpt(image, kpt)) cv2.imshow('dense alignment', plot_vertices(image, vertices)) cv2.imshow('pose', plot_pose_box(image, camera_matrix, kpt)) cv2.waitKey(0)
def main(args): if args.isShow or args.isTexture: import cv2 from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # GPU number, -1 for CPU prn = PRN(is_dlib=args.isDlib) # ------------- load data image_folder = args.inputDir save_folder = args.outputDir if not os.path.exists(save_folder): os.mkdir(save_folder) types = ('*.jpg', '*.png') image_path_list = [] if os.path.isfile(image_folder): image_path_list.append(image_folder) for files in types: image_path_list.extend(glob(os.path.join(image_folder, files))) total_num = len(image_path_list) for i, image_path in enumerate(image_path_list): name = image_path.strip().split('/')[-1][:-4] # read image image = imread(image_path) [h, w, _] = image.shape # the core: regress position map if args.isDlib: max_size = max(image.shape[0], image.shape[1]) if max_size > 1000: image = rescale(image, 1000. / max_size) image = (image * 255).astype(np.uint8) pos, crop_image = prn.process(image) # use dlib to detect face else: if image.shape[1] == image.shape[2]: image = resize(image, (256, 256)) pos = prn.net_forward( image / 255.) # input image has been cropped to 256x256 crop_image = None else: box = np.array([0, image.shape[1] - 1, 0, image.shape[0] - 1 ]) # cropped with bounding box pos, crop_image = prn.process(image, box) image = image / 255. if pos is None: continue if args.is3d or args.isMat or args.isPose or args.isShow: # 3D vertices vertices = prn.get_vertices(pos) if args.isFront: save_vertices = frontalize(vertices) else: save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] if args.isImage and crop_image is not None: imsave(os.path.join(save_folder, name + '_crop.jpg'), crop_image) imsave(os.path.join(save_folder, name + '_orig.jpg'), image) if args.is3d: # corresponding colors colors = prn.get_colors(image, vertices) if args.isTexture: texture = cv2.remap(image, pos[:, :, :2].astype(np.float32), None, interpolation=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT, borderValue=(0)) if args.isMask: vertices_vis = get_visibility(vertices, prn.triangles, h, w) uv_mask = get_uv_mask(vertices_vis, prn.triangles, prn.uv_coords, h, w, prn.resolution_op) texture = texture * uv_mask[:, :, np.newaxis] write_obj_with_texture( os.path.join(save_folder, name + '.obj'), save_vertices, colors, prn.triangles, texture, prn.uv_coords / prn.resolution_op ) #save 3d face with texture(can open with meshlab) else: write_obj(os.path.join(save_folder, name + '.obj'), save_vertices, colors, prn.triangles) #save 3d face(can open with meshlab) if args.isDepth: depth_image = get_depth_image(vertices, prn.triangles, h, w, True) depth = get_depth_image(vertices, prn.triangles, h, w) imsave(os.path.join(save_folder, name + '_depth.jpg'), depth_image) sio.savemat(os.path.join(save_folder, name + '_depth.mat'), {'depth': depth}) if args.isMat: sio.savemat(os.path.join(save_folder, name + '_mesh.mat'), { 'vertices': vertices, 'colors': colors, 'triangles': prn.triangles }) if args.isKpt or args.isShow: # get landmarks kpt = prn.get_landmarks(pos) np.savetxt(os.path.join(save_folder, name + '_kpt.txt'), kpt) if args.isPose or args.isShow: # estimate pose camera_matrix, pose = estimate_pose(vertices) np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose) np.savetxt(os.path.join(save_folder, name + '_camera_matrix.txt'), camera_matrix) np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose) if args.isShow: # ---------- Plot image_pose = plot_pose_box(image, camera_matrix, kpt) cv2.imshow('sparse alignment', plot_kpt(image, kpt)) cv2.imshow('dense alignment', plot_vertices(image, vertices)) cv2.imshow('pose', plot_pose_box(image, camera_matrix, kpt)) if crop_image is not None: cv2.imshow('crop', crop_image) cv2.waitKey(0)
def getFacialLandmarks(isDlib, img_, numFaces=1): img = copy.deepcopy(img_) # use dlib or PrNetfor prediction of facial landmarks if isDlib == "True": # load shape predictor model model_path = 'Code/dlib_model/shape_predictor_68_face_landmarks.dat' # load the detector and the predictor. # predictor accepts pre-trained model as input detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(model_path) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) rects = detector(img_gray, 1) # store landmark locations of both faces landmarkCoordAll = [] # iterate through the points in both faces for r, rect in enumerate(rects): landmarks = predictor(img_gray, rect) # reshape landmarks to (68X2) landmarkCoord = np.zeros((68, 2), dtype='int') for i in range(68): landmarkCoord[i] = (landmarks.part(i).x, landmarks.part(i).y) landmarkCoordAll.append(landmarkCoord) # draw bounding box on face cv2.rectangle(img, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (0, 255, 255), 0) # draw facial landmarks img_ = drawFacialLandmarks(img, landmarkCoord) if isDlib == "False": # prn uses dlib for face detection and its own trained model for prediction of facial landmarks prn = PRN(is_dlib = True, prefix='Code/prnet/') [h, w, c] = img.shape if c>3: img = img[:,:,:3] if img.shape[0] == img.shape[1]: img = resize(img, (256,256)) pos = prn.net_forward(img/255.) # input image has been cropped to 256x256 else: posList = [] for i in range(numFaces): pos = prn.process(img, i) posList.append(pos) landmarkCoordAll = [] for i, pos in enumerate(posList): if pos is None: return img_, landmarkCoordAll # get landmark points of face landmarkCoord = prn.get_landmarks(pos) img_ = plot_kpt(img_, landmarkCoord) landmarkCoord = landmarkCoord[:, 0:2] landmarkCoordAll.append(landmarkCoord) return img_, landmarkCoordAll