openface/tests/openface_api_tests.py

68 lines
2.2 KiB
Python
Raw Normal View History

2016-01-12 01:41:23 +08:00
# OpenFace API tests.
2015-12-27 21:17:38 +08:00
#
# Copyright 2015-2016 Carnegie Mellon University
2015-12-23 11:41:04 +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 cv2
import os
import numpy as np
np.set_printoptions(precision=2)
import scipy
import scipy.spatial
import openface
2016-01-12 01:13:14 +08:00
openfaceDir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
modelDir = os.path.join(openfaceDir, 'models')
2015-12-23 11:41:04 +08:00
dlibModelDir = os.path.join(modelDir, 'dlib')
openfaceModelDir = os.path.join(modelDir, 'openface')
2016-01-12 01:13:14 +08:00
exampleImages = os.path.join(openfaceDir, 'images', 'examples')
lfwSubset = os.path.join(openfaceDir, 'data', 'lfw-subset')
2016-01-12 01:00:14 +08:00
2015-12-23 11:41:04 +08:00
dlibFacePredictor = os.path.join(dlibModelDir,
"shape_predictor_68_face_landmarks.dat")
2016-01-13 06:10:49 +08:00
model = os.path.join(openfaceModelDir, 'nn4.small2.v1.t7')
2015-12-23 11:41:04 +08:00
imgDim = 96
2015-12-30 08:57:29 +08:00
align = openface.AlignDlib(dlibFacePredictor)
2016-01-13 06:10:49 +08:00
net = openface.TorchNeuralNet(model, imgDim=imgDim)
2015-12-23 11:41:04 +08:00
2015-12-23 12:11:15 +08:00
2016-01-13 06:10:49 +08:00
def test_pipeline():
2016-01-12 01:00:14 +08:00
imgPath = os.path.join(exampleImages, 'lennon-1.jpg')
2016-01-08 07:28:05 +08:00
bgrImg = cv2.imread(imgPath)
if bgrImg is None:
raise Exception("Unable to load image: {}".format(imgPath))
rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB)
2016-01-13 00:54:36 +08:00
# assert np.isclose(norm(rgbImg), 11.1355)
2016-01-08 07:28:05 +08:00
bb = align.getLargestFaceBoundingBox(rgbImg)
assert bb.left() == 341
assert bb.right() == 1006
assert bb.top() == 193
assert bb.bottom() == 859
alignedFace = align.align(imgDim, rgbImg, bb,
landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)
2016-01-13 02:26:24 +08:00
# assert np.isclose(norm(alignedFace), 7.61577)
2016-01-08 07:28:05 +08:00
2016-01-13 06:10:49 +08:00
rep = net.forward(alignedFace)
2015-12-25 00:07:19 +08:00
cosDist = scipy.spatial.distance.cosine(rep, np.ones(128))
2016-01-13 06:10:49 +08:00
print(cosDist)
assert np.isclose(cosDist, 0.938840385931)