Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Expression_Model/
__pycache__/
Shape_Model/
fpn_new_model/
output_preproc.csv
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ The code provided here bundles all three components for holistic 3D face modelin
* [TensorFlow](https://www.tensorflow.org/)
* [OpenCV Python Wrapper](http://opencv.org/)
* [Numpy](http://www.numpy.org/)
* [Python2.7](https://www.python.org/download/releases/2.7/)
* [Python3.5](https://www.python.org/downloads/release/python-352/)

The code has been tested on Linux with Python 2.7.12. On Linux you can rely on the default version of python, installing all the packages needed from the package manager or on Anaconda Python and install required packages through `conda`.
The code has been tested on Linux with Python 3.5.2. On Linux you can rely on the default version of python, installing all the packages needed from the package manager or on Anaconda Python and install required packages through `conda`.


## Usage
Expand Down
6 changes: 4 additions & 2 deletions ResNet/ThreeDMM_expr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
#sys.path.append('../kaffe')
sys.path.append('/home/usc/Desktop/Research/FG18/ExpNet_Code_Release/kaffe/tensorflow')
from os.path import dirname, join

sys.path.append(join(dirname(dirname(__file__)), 'kaffe', 'tensorflow'))

from network_expr import Network_Expr


Expand Down
6 changes: 4 additions & 2 deletions ResNet/ThreeDMM_shape.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
#sys.path.append('../kaffe')
sys.path.append('/home/usc/Desktop/Research/FG18/ExpNet_Code_Release/kaffe/tensorflow')
from os.path import dirname, join

sys.path.append(join(dirname(dirname(__file__)), 'kaffe', 'tensorflow'))

from network_shape import Network_Shape


Expand Down
6 changes: 3 additions & 3 deletions kaffe/tensorflow/network_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def feed(self, *args):
assert len(args) != 0
self.terminals = []
for fed_layer in args:
if isinstance(fed_layer, basestring):
if isinstance(fed_layer, (str, bytes)):
try:
fed_layer = self.layers[fed_layer]
except KeyError:
Expand Down Expand Up @@ -197,9 +197,9 @@ def conv(self,
with tf.variable_scope(name) as scope:
if name == 'res5c_branch2c' or name == 'res5c_branch2b' or name == 'res5c_branch2a' or \
name == 'res5b_branch2c' or name == 'res5b_branch2b' or name == 'res5b_branch2a':
kernel = self.make_var('weights', shape=[k_h, k_w, c_i / group, c_o])
kernel = self.make_var('weights', shape=[k_h, k_w, int(c_i) / group, c_o])
else:
kernel = self.make_var_fixed('weights', shape=[k_h, k_w, c_i / group, c_o])
kernel = self.make_var_fixed('weights', shape=[k_h, k_w, int(c_i) / group, c_o])

if group == 1:
# This is the common-case. Convolve the input without any further complications.
Expand Down
8 changes: 4 additions & 4 deletions kaffe/tensorflow/network_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def load(self, data_path, prefix_name, session, ignore_missing=False):
ignore_missing: If true, serialized weights for missing layers are ignored.
'''
data_dict = np.load(data_path).item()
print len(data_dict) #data_dict['res2b_branch2a']
print(len(data_dict)) #data_dict['res2b_branch2a']
for op_name in data_dict:
#print op_name
#if op_name == "res2b_branch2a":
Expand Down Expand Up @@ -137,7 +137,7 @@ def feed(self, *args):
assert len(args) != 0
self.terminals = []
for fed_layer in args:
if isinstance(fed_layer, basestring):
if isinstance(fed_layer, (str, bytes)):
try:
fed_layer = self.layers[fed_layer]
except KeyError:
Expand Down Expand Up @@ -197,9 +197,9 @@ def conv(self,
with tf.variable_scope(name) as scope:
if name == 'res5c_branch2c' or name == 'res5c_branch2b' or name == 'res5c_branch2a' or \
name == 'res5b_branch2c' or name == 'res5b_branch2b' or name == 'res5b_branch2a':
kernel = self.make_var('weights', shape=[k_h, k_w, c_i / group, c_o])
kernel = self.make_var('weights', shape=[k_h, k_w, int(c_i) / group, c_o])
else:
kernel = self.make_var_fixed('weights', shape=[k_h, k_w, c_i / group, c_o])
kernel = self.make_var_fixed('weights', shape=[k_h, k_w, int(c_i) / group, c_o])

if group == 1:
# This is the common-case. Convolve the input without any further complications.
Expand Down
2 changes: 1 addition & 1 deletion kaffe/tensorflow/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self, op, *args, **kwargs):

def format(self, arg):
'''Returns a string representation for the given value.'''
return "'%s'" % arg if isinstance(arg, basestring) else str(arg)
return "'%s'" % arg if isinstance(arg, (str, bytes)) else str(arg)

def pair(self, key, value):
'''Returns key=formatted(value).'''
Expand Down
21 changes: 12 additions & 9 deletions main_ExpShapePoseNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

# Global parameters
_tmpdir = './tmp/'#save intermediate images needed to fed into ExpNet, ShapeNet, and PoseNet
print '> make dir'
print('> make dir')
if not os.path.exists( _tmpdir):
os.makedirs( _tmpdir )
output_proc = 'output_preproc.csv' # save intermediate image list
Expand Down Expand Up @@ -71,9 +71,9 @@ def extract_PSE_feats():

# Prepare data
data_dict = myparse.parse_input(inputlist) # please see input.csv for the input format
print len(data_dict)
print(len(data_dict))
## Pre-processing the images
print '> preproc'
print('> preproc')
pu.preProcessImage(_tmpdir, data_dict, './', factor, _alexNetSize, output_proc)


Expand All @@ -86,7 +86,7 @@ def extract_PSE_feats():
###################
# Face Pose-Net
###################
net_data = np.load("./fpn_new_model/PAM_frontal_ALexNet.npy").item()
net_data = np.load("./fpn_new_model/PAM_frontal_ALexNet_py3.npy").item()
pose_labels = np.zeros([FLAGS.batch_size,6])
x1 = tf.image.resize_bilinear(x, tf.constant([227,227], dtype=tf.int32))

Expand Down Expand Up @@ -195,19 +195,19 @@ def extract_PSE_feats():
model = scipy.io.loadmat(BFM_path,squeeze_me=True,struct_as_record=False)
model = model["BFM"]
faces = model.faces-1
print '> Loaded the Basel Face Model to write the 3D output!'
print('> Loaded the Basel Face Model to write the 3D output!')



print '> Start to estimate Expression, Shape, and Pose!'
with open(output_proc, 'rb') as csvfile:
print('> Start to estimate Expression, Shape, and Pose!')
with open(output_proc, 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
for row in csvreader:

image_key = row[0]
image_file_path = row[1]

print '> Process ' + image_file_path
start = time.time()
print('> Process ' + image_file_path)


image = cv2.imread(image_file_path,1) # BGR
Expand Down Expand Up @@ -251,6 +251,9 @@ def extract_PSE_feats():
# Shape + Expression + Pose
SEP,TEP = utils.projectBackBFM_withEP(model, Shape_Texture, Expr, Pose)
utils.write_ply_textureless(outFile + '_Shape_Expr_Pose.ply', SEP, faces)
end = time.time()

print(end - start)



Expand Down
93 changes: 46 additions & 47 deletions pose_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,48 @@
import fileinput
import shutil


def increaseBbox(bbox, factor):
tlx = bbox[0]
tly = bbox[1]
brx = bbox[2]
bry = bbox[3]
tlx = bbox[0]
tly = bbox[1]
brx = bbox[2]
bry = bbox[3]
dx = factor
dy = factor
dw = 1 + factor
dh = 1 + factor
#Getting bbox height and width
w = brx-tlx;
h = bry-tly;
# Getting bbox height and width
w = brx - tlx
h = bry - tly
tlx2 = tlx - w * dx
tly2 = tly - h * dy
brx2 = tlx + w * dw
bry2 = tly + h * dh
nbbox = np.zeros( (4,1), dtype=np.float32 )
nbbox = np.zeros((4, 1), dtype=np.float32)
nbbox[0] = tlx2
nbbox[1] = tly2
nbbox[2] = brx2
nbbox[3] = bry2
nbbox[3] = bry2
return nbbox


def image_bbox_processing_v2(img, bbox):
img_h, img_w, img_c = img.shape
lt_x = bbox[0]
lt_y = bbox[1]
rb_x = bbox[2]
rb_y = bbox[3]

fillings = np.zeros( (4,1), dtype=np.int32)
if lt_x < 0: ## 0 for python
fillings = np.zeros((4, 1), dtype=np.int32)
if lt_x < 0: # 0 for python
fillings[0] = math.ceil(-lt_x)
if lt_y < 0:
fillings[1] = math.ceil(-lt_y)
if rb_x > img_w-1:
if rb_x > img_w - 1:
fillings[2] = math.ceil(rb_x - img_w + 1)
if rb_y > img_h-1:
if rb_y > img_h - 1:
fillings[3] = math.ceil(rb_y - img_h + 1)
new_bbox = np.zeros( (4,1), dtype=np.float32 )
new_bbox = np.zeros((4, 1), dtype=np.float32)
# img = [zeros(size(img,1),fillings(1),img_c), img]
# img = [zeros(fillings(2), size(img,2),img_c); img]
# img = [img, zeros(size(img,1), fillings(3),img_c)]
Expand All @@ -54,83 +56,80 @@ def image_bbox_processing_v2(img, bbox):
imgc = img.copy()
if fillings[0] > 0:
img_h, img_w, img_c = imgc.shape
imgc = np.hstack( [np.zeros( (img_h, fillings[0][0], img_c), dtype=np.uint8 ), imgc] )
imgc = np.hstack([np.zeros((img_h, fillings[0][0], img_c), dtype=np.uint8), imgc])
if fillings[1] > 0:

img_h, img_w, img_c = imgc.shape
imgc = np.vstack( [np.zeros( (fillings[1][0], img_w, img_c), dtype=np.uint8 ), imgc] )
imgc = np.vstack([np.zeros((fillings[1][0], img_w, img_c), dtype=np.uint8), imgc])
if fillings[2] > 0:


img_h, img_w, img_c = imgc.shape
imgc = np.hstack( [ imgc, np.zeros( (img_h, fillings[2][0], img_c), dtype=np.uint8 ) ] )
imgc = np.hstack([imgc, np.zeros((img_h, fillings[2][0], img_c), dtype=np.uint8)])
if fillings[3] > 0:
img_h, img_w, img_c = imgc.shape
imgc = np.vstack( [ imgc, np.zeros( (fillings[3][0], img_w, img_c), dtype=np.uint8) ] )

imgc = np.vstack([imgc, np.zeros((fillings[3][0], img_w, img_c), dtype=np.uint8)])

new_bbox[0] = lt_x + fillings[0]
new_bbox[1] = lt_y + fillings[1]
new_bbox[2] = rb_x + fillings[0]
new_bbox[3] = rb_y + fillings[1]
return imgc, new_bbox


def preProcessImage(_savingDir, data_dict, data_root, factor, _alexNetSize, _listFile):
#### Formatting the images as needed
# Formatting the images as needed
file_output = _listFile
count = 1
fileIn = open(file_output , 'w' )
for key in data_dict.keys():
fileIn = open(file_output, 'w')
for key in data_dict.keys():
filename = data_dict[key]['file']
im = cv2.imread(data_root + filename)
im = cv2.imread(data_root + filename)
if im is not None:
print 'Processing ' + filename + ' '+ str(count)
print('Processing ' + filename + ' ' + str(count))
sys.stdout.flush()
lt_x = data_dict[key]['x']
lt_y = data_dict[key]['y']
rb_x = lt_x + data_dict[key]['width']
rb_y = lt_y + data_dict[key]['height']
w = data_dict[key]['width']
h = data_dict[key]['height']
center = ( (lt_x+rb_x)/2, (lt_y+rb_y)/2 )
side_length = max(w,h);
bbox = np.zeros( (4,1), dtype=np.float32 )
bbox[0] = center[0] - side_length/2
bbox[1] = center[1] - side_length/2
bbox[2] = center[0] + side_length/2
bbox[3] = center[1] + side_length/2
center = ((lt_x + rb_x) / 2, (lt_y + rb_y) / 2)
side_length = max(w, h)
bbox = np.zeros((4, 1), dtype=np.float32)
bbox[0] = center[0] - side_length / 2
bbox[1] = center[1] - side_length / 2
bbox[2] = center[0] + side_length / 2
bbox[3] = center[1] + side_length / 2
#img_2, bbox_green = image_bbox_processing_v2(im, bbox)
#%% Get the expanded square bbox
bbox_red = increaseBbox(bbox, factor)
#[img, bbox_red] = image_bbox_processing_v2(img, bbox_red);
img_3, bbox_new = image_bbox_processing_v2(im, bbox_red)
#%% Crop and resized
#bbox_red = ceil(bbox_red);
bbox_new = np.ceil( bbox_new )
bbox_new = np.ceil(bbox_new)
#side_length = max(bbox_new(3) - bbox_new(1), bbox_new(4) - bbox_new(2));
side_length = max( bbox_new[2] - bbox_new[0], bbox_new[3] - bbox_new[1] )
side_length = max(bbox_new[2] - bbox_new[0], bbox_new[3] - bbox_new[1])
bbox_new[2:4] = bbox_new[0:2] + side_length
#crop_img = img(bbox_red(2):bbox_red(4), bbox_red(1):bbox_red(3), :);
#resized_crop_img = imresize(crop_img, [227, 227]);# % re-scaling to 227 x 227
# crop_img = img(bbox_red(2):bbox_red(4), bbox_red(1):bbox_red(3), :);
# resized_crop_img = imresize(crop_img, [227, 227]);# % re-scaling to 227 x 227
bbox_new = bbox_new.astype(int)
crop_img = img_3[bbox_new[1][0]:bbox_new[3][0], bbox_new[0][0]:bbox_new[2][0], :];
resized_crop_img = cv2.resize(crop_img, ( _alexNetSize, _alexNetSize ), interpolation = cv2.INTER_CUBIC)
cv2.imwrite(_savingDir + key + '.jpg', resized_crop_img )


resized_crop_img = cv2.resize(crop_img, (_alexNetSize, _alexNetSize), interpolation=cv2.INTER_CUBIC)
cv2.imwrite(_savingDir + key + '.jpg', resized_crop_img)

## Tracking pose image
# Tracking pose image
fileIn.write(key + ',')
fileIn.write(_savingDir + key + '.jpg\n')

else:
print ' '.join(['Skipping image:', filename, 'Image is None', str(count)])
count+=1
#if count == 101:
print(' '.join(['Skipping image:', filename, 'Image is None', str(count)]))
count += 1
# if count == 101:
# break
fileIn.close()


def replaceInFile(filep, before, after):
for line in fileinput.input(filep, inplace=True):
print line.replace(before,after),

print(line.replace(before, after),)
Loading