Skip to content

Commit

Permalink
Add optional model path to convert.py (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ganonmaster committed Dec 22, 2017
1 parent 7f7f110 commit 3ba44f7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
13 changes: 11 additions & 2 deletions convert.py
@@ -1,5 +1,5 @@
import cv2
from lib.cli import DirectoryProcessor
from lib.cli import DirectoryProcessor, FullPaths
from pathlib import Path
from lib.faces_process import convert_one_image
from lib.faces_detect import crop_faces
Expand All @@ -8,6 +8,15 @@
class ConvertImage(DirectoryProcessor):
filename = ''

def add_optional_arguments(self, parser):
parser.add_argument('-m', '--model-dir',
action=FullPaths,
dest="model_dir",
default="models",
help="Model directory. A directory containing the trained model \
you wish to process. Defaults to 'models'")
return parser

def process_image(self, filename):
try:
image = cv2.imread(filename)
Expand All @@ -16,7 +25,7 @@ def process_image(self, filename):
print('- Found more than one face!')
self.verify_output = True

new_face = convert_one_image(cv2.resize(face.image, (256, 256)))
new_face = convert_one_image(cv2.resize(face.image, (256, 256)), self.arguments.model_dir)
image[slice(face.y, face.y + face.h), slice(face.x, face.x + face.w)] = cv2.resize(new_face, (face.w, face.h))
self.faces_detected = self.faces_detected + 1
output_file = self.output_dir / Path(filename).name
Expand Down
2 changes: 1 addition & 1 deletion lib/cli.py
Expand Up @@ -12,7 +12,7 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, os.path.abspath(os.path.expanduser(values)))


class DirectoryProcessor():
class DirectoryProcessor(object):
'''
Abstract class that processes a directory of images
and writes output to the specified folder
Expand Down
17 changes: 9 additions & 8 deletions lib/faces_process.py
Expand Up @@ -6,18 +6,19 @@
from .model import autoencoder_B
from .model import encoder, decoder_A, decoder_B

encoder.load_weights("models/encoder.h5")
decoder_A.load_weights("models/decoder_A.h5")
decoder_B.load_weights("models/decoder_B.h5")

autoencoder = autoencoder_B
def convert_one_image(image, model_dir="models"):

# landmark file can be found in http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
# unzip it in the same folder as the main scripts
aligner = Aligner("shape_predictor_68_face_landmarks.dat", "mmod_human_face_detector.dat")
encoder.load_weights(model_dir + "/encoder.h5")
decoder_A.load_weights(model_dir + "/decoder_A.h5")
decoder_B.load_weights(model_dir + "/decoder_B.h5")

autoencoder = autoencoder_B

# landmark file can be found in http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
# unzip it in the same folder as the main scripts
aligner = Aligner("shape_predictor_68_face_landmarks.dat", "mmod_human_face_detector.dat")

def convert_one_image(image):
assert image.shape == (256, 256, 3)
crop = slice(48, 208)
face = image[crop, crop]
Expand Down

0 comments on commit 3ba44f7

Please sign in to comment.