2016-01-12 01:41:03 +08:00
|
|
|
# OpenFace batch-represent tests.
|
|
|
|
#
|
2016-01-14 03:52:52 +08:00
|
|
|
# Copyright 2015-2016 Carnegie Mellon University
|
2016-01-12 01:41:03 +08:00
|
|
|
#
|
|
|
|
# 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 os
|
|
|
|
import shutil
|
2016-01-12 06:36:54 +08:00
|
|
|
import tempfile
|
2016-01-12 01:41:03 +08:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
np.set_printoptions(precision=2)
|
|
|
|
import pandas as pd
|
|
|
|
import scipy
|
|
|
|
import scipy.spatial
|
|
|
|
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
|
|
|
openfaceDir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
|
|
|
modelDir = os.path.join(openfaceDir, 'models')
|
|
|
|
|
|
|
|
exampleImages = os.path.join(openfaceDir, 'images', 'examples')
|
|
|
|
lfwSubset = os.path.join(openfaceDir, 'data', 'lfw-subset')
|
|
|
|
|
|
|
|
|
|
|
|
def test_batch_represent():
|
2016-06-07 02:27:33 +08:00
|
|
|
assert os.path.isdir(
|
|
|
|
lfwSubset), "Get lfw-subset by running ./data/download-lfw-subset.sh"
|
2016-01-12 01:41:03 +08:00
|
|
|
|
2016-01-12 06:36:54 +08:00
|
|
|
workDir = tempfile.mkdtemp(prefix='OpenFaceBatchRep-')
|
|
|
|
|
2016-01-12 01:41:03 +08:00
|
|
|
cmd = ['python2', os.path.join(openfaceDir, 'util', 'align-dlib.py'),
|
|
|
|
os.path.join(lfwSubset, 'raw'), 'align', 'outerEyesAndNose',
|
2016-01-12 06:36:54 +08:00
|
|
|
os.path.join(workDir, 'aligned')]
|
2016-01-12 01:41:03 +08:00
|
|
|
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
|
|
|
|
(out, err) = p.communicate()
|
2016-01-13 02:31:00 +08:00
|
|
|
print(out)
|
|
|
|
print(err)
|
2016-01-12 01:41:03 +08:00
|
|
|
assert p.returncode == 0
|
|
|
|
|
2016-06-07 02:27:33 +08:00
|
|
|
cmd = ['python2', os.path.join(openfaceDir, 'util', 'align-dlib.py'),
|
|
|
|
os.path.join(lfwSubset, 'raw'), 'align', 'outerEyesAndNose',
|
|
|
|
os.path.join(workDir, 'aligned'), '--version', '2']
|
|
|
|
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
|
|
|
|
(out, err) = p.communicate()
|
|
|
|
print(out)
|
|
|
|
print(err)
|
|
|
|
assert p.returncode == 0
|
|
|
|
|
2016-01-12 01:41:03 +08:00
|
|
|
cmd = ['th', './batch-represent/main.lua',
|
2016-01-12 06:36:54 +08:00
|
|
|
'-data', os.path.join(workDir, 'aligned'),
|
|
|
|
'-outDir', os.path.join(workDir, 'reps')]
|
2016-01-12 01:41:03 +08:00
|
|
|
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
|
|
|
|
(out, err) = p.communicate()
|
2016-01-13 02:31:00 +08:00
|
|
|
print(out)
|
|
|
|
print(err)
|
2016-01-12 01:41:03 +08:00
|
|
|
assert p.returncode == 0
|
|
|
|
|
2016-01-12 06:36:54 +08:00
|
|
|
fname = os.path.join(workDir, 'reps', 'labels.csv')
|
2016-01-12 01:41:03 +08:00
|
|
|
labels = pd.read_csv(fname, header=None).as_matrix()
|
2016-01-12 06:36:54 +08:00
|
|
|
fname = os.path.join(workDir, 'reps', 'reps.csv')
|
2016-01-12 01:41:03 +08:00
|
|
|
embeddings = pd.read_csv(fname, header=None).as_matrix()
|
|
|
|
|
|
|
|
brody1 = brody2 = None
|
|
|
|
for i, (cls, label) in enumerate(labels):
|
|
|
|
if "Brody_0001" in label:
|
|
|
|
brody1 = embeddings[i]
|
|
|
|
elif "Brody_0002" in label:
|
|
|
|
brody2 = embeddings[i]
|
|
|
|
|
|
|
|
assert brody1 is not None
|
|
|
|
assert brody2 is not None
|
2016-01-13 02:31:00 +08:00
|
|
|
print("brody1:", brody1)
|
|
|
|
print("brody2:", brody2)
|
2016-01-12 01:41:03 +08:00
|
|
|
|
|
|
|
cosDist = scipy.spatial.distance.cosine(brody1, brody2)
|
2016-01-13 04:46:49 +08:00
|
|
|
print('cosDist:', cosDist)
|
2016-01-13 06:35:10 +08:00
|
|
|
assert np.isclose(cosDist, 0.1568, atol=1e-4)
|
2016-01-12 01:41:03 +08:00
|
|
|
|
2016-01-12 06:36:54 +08:00
|
|
|
shutil.rmtree(workDir)
|