From 9698245751b4f74d07dd8821a6979e5be3720709 Mon Sep 17 00:00:00 2001 From: Deconimus Date: Thu, 10 Sep 2020 18:57:17 +0200 Subject: [PATCH] Disabled mesh_renderer as dependency when running Windows. --- demo.py | 15 ++++++++----- face_decoder.py | 59 ++++++++++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/demo.py b/demo.py index 68d3439..65964cb 100644 --- a/demo.py +++ b/demo.py @@ -1,15 +1,15 @@ import tensorflow as tf import numpy as np -import cv2 from PIL import Image -import os -import glob +import os, glob, cv2, platform from scipy.io import loadmat,savemat from preprocess_img import Preprocess from load_data import * from face_decoder import Face3D +is_windows = platform.system() == "Windows" + def load_graph(graph_filename): with tf.gfile.GFile(graph_filename,'rb') as f: graph_def = tf.GraphDef() @@ -77,11 +77,14 @@ def demo(): face_texture_ = np.squeeze(face_texture_, (0)) face_color_ = np.squeeze(face_color_, (0)) landmarks_2d_ = np.squeeze(landmarks_2d_, (0)) - recon_img_ = np.squeeze(recon_img_, (0)) + recon_img_ = None + if not is_windows: + recon_img_ = np.squeeze(recon_img_, (0)) # save output files - savemat(os.path.join(save_path,file.split(os.path.sep)[-1].replace('.png','.mat').replace('jpg','mat')),{'cropped_img':input_img[:,:,::-1],'recon_img':recon_img_,'coeff':coeff_,\ - 'face_shape':face_shape_,'face_texture':face_texture_,'face_color':face_color_,'lm_68p':landmarks_2d_,'lm_5p':lm_new}) + if recon_img_: + savemat(os.path.join(save_path,file.split(os.path.sep)[-1].replace('.png','.mat').replace('jpg','mat')),{'cropped_img':input_img[:,:,::-1],'recon_img':recon_img_,'coeff':coeff_,\ + 'face_shape':face_shape_,'face_texture':face_texture_,'face_color':face_color_,'lm_68p':landmarks_2d_,'lm_5p':lm_new}) save_obj(os.path.join(save_path,file.split(os.path.sep)[-1].replace('.png','_mesh.obj').replace('jpg','_mesh.obj')),face_shape_,tri_,np.clip(face_color_,0,255)/255) # 3D reconstruction face (in canonical view) if __name__ == '__main__': diff --git a/face_decoder.py b/face_decoder.py index 0f07e43..8c76536 100644 --- a/face_decoder.py +++ b/face_decoder.py @@ -1,8 +1,14 @@ import tensorflow as tf import math as m import numpy as np -import mesh_renderer from scipy.io import loadmat +import platform, PIL + +is_windows = platform.system() == "Windows" + +if not is_windows: + import mesh_renderer + ############################################################################################### # Reconstruct 3D face based on output coefficients and facemodel ############################################################################################### @@ -56,10 +62,13 @@ class Face3D(): self.face_color = face_color # reconstruction images - render_imgs = self.Render_block(face_shape_t,norm_r,face_color,self.facemodel,batchsize) - render_imgs = tf.clip_by_value(render_imgs,0,255) - render_imgs = tf.cast(render_imgs,tf.float32) - self.render_imgs = render_imgs + if not is_windows: + render_imgs = self.Render_block(face_shape_t,norm_r,face_color,self.facemodel,batchsize) + render_imgs = tf.clip_by_value(render_imgs,0,255) + render_imgs = tf.cast(render_imgs,tf.float32) + self.render_imgs = render_imgs + else: + self.render_imgs = [] ###################################################################################################### def Split_coeff(self,coeff): @@ -272,23 +281,27 @@ class Face3D(): far_clip = 50*tf.ones([batchsize]) #using tf_mesh_renderer for rasterization (https://github.com/google/tf_mesh_renderer) # img: [batchsize,224,224,4] images in RGBA order (0-255) - with tf.device('/cpu:0'): - img = mesh_renderer.mesh_renderer(face_shape, - tf.cast(facemodel.face_buf-1,tf.int32), - face_norm, - face_color, - camera_position = camera_position, - camera_lookat = camera_lookat, - camera_up = camera_up, - light_positions = light_positions, - light_intensities = light_intensities, - image_width = 224, - image_height = 224, - fov_y = fov_y, #12.5936 - ambient_color = ambient_color, - near_clip = near_clip, - far_clip = far_clip) - - return img + + if not is_windows: + with tf.device('/cpu:0'): + img = mesh_renderer.mesh_renderer(face_shape, + tf.cast(facemodel.face_buf-1,tf.int32), + face_norm, + face_color, + camera_position = camera_position, + camera_lookat = camera_lookat, + camera_up = camera_up, + light_positions = light_positions, + light_intensities = light_intensities, + image_width = 224, + image_height = 224, + fov_y = fov_y, #12.5936 + ambient_color = ambient_color, + near_clip = near_clip, + far_clip = far_clip) + return img + else: + #return PIL.Image.new('RGB', (224, 224)) + return np.zeros([224, 224], dtype=np.int32)