2015-09-30 06:07:12 +08:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
#
|
|
|
|
# Example to compare the faces in two images.
|
|
|
|
# Brandon Amos
|
|
|
|
# 2015/09/29
|
|
|
|
#
|
|
|
|
# Copyright 2015 Carnegie Mellon University
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
start = time.time()
|
2015-12-23 09:58:06 +08:00
|
|
|
|
2015-09-30 06:07:12 +08:00
|
|
|
import argparse
|
|
|
|
import cv2
|
2015-10-04 07:23:13 +08:00
|
|
|
import itertools
|
2015-09-30 06:07:12 +08:00
|
|
|
import os
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
np.set_printoptions(precision=2)
|
|
|
|
|
2015-10-09 22:48:06 +08:00
|
|
|
import openface
|
|
|
|
import openface.helper
|
2015-12-23 09:58:06 +08:00
|
|
|
from openface.alignment import NaiveDlib # Depends on dlib.
|
2015-09-30 06:07:12 +08:00
|
|
|
|
2015-12-25 00:45:00 +08:00
|
|
|
fileDir = os.path.dirname(os.path.realpath(__file__))
|
2015-09-30 06:07:12 +08:00
|
|
|
modelDir = os.path.join(fileDir, '..', 'models')
|
|
|
|
dlibModelDir = os.path.join(modelDir, 'dlib')
|
2015-10-09 22:48:06 +08:00
|
|
|
openfaceModelDir = os.path.join(modelDir, 'openface')
|
2015-09-30 06:07:12 +08:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
2015-10-04 07:23:13 +08:00
|
|
|
parser.add_argument('imgs', type=str, nargs='+', help="Input images.")
|
2015-09-30 06:07:12 +08:00
|
|
|
parser.add_argument('--dlibFacePredictor', type=str, help="Path to dlib's face predictor.",
|
|
|
|
default=os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat"))
|
|
|
|
parser.add_argument('--networkModel', type=str, help="Path to Torch network model.",
|
2015-10-09 22:48:06 +08:00
|
|
|
default=os.path.join(openfaceModelDir, 'nn4.v1.t7'))
|
2015-10-12 23:30:29 +08:00
|
|
|
parser.add_argument('--imgDim', type=int,
|
|
|
|
help="Default image dimension.", default=96)
|
2015-10-09 21:54:15 +08:00
|
|
|
parser.add_argument('--cuda', action='store_true')
|
|
|
|
parser.add_argument('--verbose', action='store_true')
|
2015-09-30 06:07:12 +08:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2015-10-04 07:23:13 +08:00
|
|
|
if args.verbose:
|
2015-10-12 23:30:29 +08:00
|
|
|
print("Argument parsing and loading libraries took {} seconds.".format(
|
|
|
|
time.time() - start))
|
2015-09-30 06:07:12 +08:00
|
|
|
|
|
|
|
start = time.time()
|
2015-12-09 06:04:57 +08:00
|
|
|
align = NaiveDlib(args.dlibFacePredictor)
|
2015-10-09 22:48:06 +08:00
|
|
|
net = openface.TorchWrap(args.networkModel, imgDim=args.imgDim, cuda=args.cuda)
|
2015-10-04 07:23:13 +08:00
|
|
|
if args.verbose:
|
2015-10-12 23:30:29 +08:00
|
|
|
print("Loading the dlib and OpenFace models took {} seconds.".format(
|
|
|
|
time.time() - start))
|
|
|
|
|
2015-09-30 06:07:12 +08:00
|
|
|
|
|
|
|
def getRep(imgPath):
|
2015-10-04 07:23:13 +08:00
|
|
|
if args.verbose:
|
|
|
|
print("Processing {}.".format(imgPath))
|
2015-12-09 06:04:57 +08:00
|
|
|
bgrImg = cv2.imread(imgPath)
|
|
|
|
if bgrImg is None:
|
2015-09-30 06:07:12 +08:00
|
|
|
raise Exception("Unable to load image: {}".format(imgPath))
|
2015-12-09 06:04:57 +08:00
|
|
|
rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB)
|
|
|
|
|
2015-10-04 07:23:13 +08:00
|
|
|
if args.verbose:
|
2015-12-09 06:04:57 +08:00
|
|
|
print(" + Original size: {}".format(rgbImg.shape))
|
2015-09-30 06:07:12 +08:00
|
|
|
|
|
|
|
start = time.time()
|
2015-12-09 06:04:57 +08:00
|
|
|
bb = align.getLargestFaceBoundingBox(rgbImg)
|
2015-09-30 06:07:12 +08:00
|
|
|
if bb is None:
|
|
|
|
raise Exception("Unable to find a face: {}".format(imgPath))
|
2015-10-04 07:23:13 +08:00
|
|
|
if args.verbose:
|
2015-10-12 23:30:29 +08:00
|
|
|
print(" + Face detection took {} seconds.".format(time.time() - start))
|
2015-09-30 06:07:12 +08:00
|
|
|
|
|
|
|
start = time.time()
|
2015-12-09 06:04:57 +08:00
|
|
|
alignedFace = align.alignImg("affine", args.imgDim, rgbImg, bb)
|
2015-09-30 06:07:12 +08:00
|
|
|
if alignedFace is None:
|
|
|
|
raise Exception("Unable to align image: {}".format(imgPath))
|
2015-10-04 07:23:13 +08:00
|
|
|
if args.verbose:
|
2015-10-12 23:30:29 +08:00
|
|
|
print(" + Face alignment took {} seconds.".format(time.time() - start))
|
2015-09-30 06:07:12 +08:00
|
|
|
|
|
|
|
start = time.time()
|
2015-10-04 07:23:13 +08:00
|
|
|
rep = net.forwardImage(alignedFace)
|
|
|
|
if args.verbose:
|
2015-10-12 23:30:29 +08:00
|
|
|
print(" + OpenFace forward pass took {} seconds.".format(time.time() - start))
|
2015-10-04 07:23:13 +08:00
|
|
|
print("Representation:")
|
|
|
|
print(rep)
|
|
|
|
print("-----\n")
|
2015-09-30 06:07:12 +08:00
|
|
|
return rep
|
|
|
|
|
2015-10-04 07:23:13 +08:00
|
|
|
for (img1, img2) in itertools.combinations(args.imgs, 2):
|
|
|
|
d = getRep(img1) - getRep(img2)
|
|
|
|
print("Comparing {} with {}.".format(img1, img2))
|
2015-12-11 10:05:52 +08:00
|
|
|
print(" + Squared l2 distance between representations: {:0.3f}".format(np.dot(d, d)))
|