import time
start = time.time()
import argparse
import cv2
import os
import pickle
import sys
from operator import itemgetter
import numpy as np
np.set_printoptions(precision=2)
import pandas as pd
import openface
from sklearn.pipeline import Pipeline
from sklearn.lda import LDA
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
from sklearn.mixture import GMM
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
def getRep(imgPath, multiple=False,verbose=0):
start = time.time()
# read image
bgrImg = cv2.imread(imgPath)
# If image not found
if bgrImg is None:
raise Exception("Unable to load image: {}".format(imgPath))
rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB) #Convert BGR to RGB opencv internal
if verbose:
print(" + Original size: {}".format(rgbImg.shape))
print("Loading the image took {} seconds.".format(time.time() - start))
start = time.time()
if multiple: #align = openface.AlignDlib(args.dlibFacePredictor) A Face predictor model instance Landmarks file
bbs = align.getAllFaceBoundingBoxes(rgbImg)# Get multiple face BBS
else:
bb1 = align.getLargestFaceBoundingBox(rgbImg)# Get the larges BB using Dlib
bbs = [bb1]
#bbs is a list of bounding boxes
#Face not found Error exception
if len(bbs) == 0 or (not multiple and bb1 is None):
raise Exception("Unable to find a face: {}".format(imgPath))
if verbose:
print("Face detection took {} seconds.".format(time.time() - start))
reps = []#Empty list inilitization
for bb in bbs:#For each face in the list of faces
start = time.time()
alignedFace = align.align(
imgDim,
rgbImg,
bb,
landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE) # Allign the face with the paticular parameters
#Allign error
if alignedFace is None:
raise Exception("Unable to align image: {}".format(imgPath))
if verbose:
print("Alignment took {} seconds.".format(time.time() - start))
print("This bbox is centered at {}, {}".format(bb.center().x, bb.center().y))
start = time.time()
#Pass the alligned face throught the network to get 128 feature vector
rep = net.forward(alignedFace)
if verbose:
print("Neural network forward pass took {} seconds.".format(
time.time() - start))
reps.append((bb.center().x, rep))
sreps = sorted(reps, key=lambda x: x[0]) # Unkonwn command TODO
return sreps
def predit_face(imgs_files):
# Load the Classifier weights from pkl file
with open(neural_net_weights, 'rb') as f:
(le, clf) = pickle.load(f, encoding='latin1')# LE contains labels for classes and CLF contains the classifer file
# For each image find who that is
#for img_name in imgs_files:
print ("\n === {} loading".format(imgs_files))
reps = getRep(imgs_files)
if len(reps) > 1:
print("List of faces in image from left to right")
for r in reps:
rep = r[1].reshape(1, -1)#Convert the 128 dim array to a 1x128 vector
bbx = r[0]#bounding box X cordinate
start = time.time()
predictions = clf.predict_proba(rep).ravel() # send through the classifier and return single flattned vector
maxI = np.argmax(predictions)# Get Max probabile class (TODO can be modified to get multiple scores)
person = le.inverse_transform(maxI)#Get label of the max probable class
confidence = predictions[maxI]#Save probbability value
if verbose:
print("Prediction took {} seconds.".format(time.time() - start))
if multiple:
print("Predict {} @ x={} with {:.2f} confidence.".format(person.decode('utf-8'), bbx,
confidence))
else:
print("Predict {} with {:.2f} confidence.".format(person.decode('utf-8'), confidence))
if isinstance(clf, GMM):# IF classifier is a GMM model
dist = np.linalg.norm(rep - clf.means_[maxI])
print(" + Distance from the mean: {}".format(dist))
# Load pkl files with encoding='latin1'
dlib_landmark_model="/home/aanilil/ml/openface/models/dlib/shape_predictor_68_face_landmarks.dat"
neural_net_model='/home/aanilil/ml/openface/models/openface/nn4.small2.v1.t7'
neural_net_weights='/home/aanilil/ml/openface/models/openface/celeb-classifier.nn4.small2.v1.pkl'
imgDim=96# Resize all face to square of this size before input to CNN
align = openface.AlignDlib(dlib_landmark_model)
net = openface.TorchNeuralNet(neural_net_weights, imgDim=imgDim)
test_im='eva.jpg'
predit_face(test_im)
But I get this error
File "/home/aanilil/PycharmProjects/Face_reco/testr.py", line 131, in <module>
predit_face(test_im)
File "/home/aanilil/PycharmProjects/Face_reco/testr.py", line 95, in predit_face
reps = getRep(imgs_files)
File "/home/aanilil/PycharmProjects/Face_reco/testr.py", line 78, in getRep
rep = net.forward(alignedFace)
File "/home/aanilil/anaconda3/envs/tensorflow/lib/python3.5/site-packages/openface/torch_neural_net.py", line 205, in forward
rep = self.forwardPath(t)
File "/home/aanilil/anaconda3/envs/tensorflow/lib/python3.5/site-packages/openface/torch_neural_net.py", line 162, in forwardPath
""".format(self.cmd, self.p.stdout.read()))
Exception:
OpenFace: `openface_server.lua` subprocess has died.
+ Is the Torch command `th` on your PATH? Check with `which th`.
+ If `th` is on your PATH, try running `./util/profile-network.lua`
to see if Torch can correctly load and run the network.
+ If this gives illegal instruction errors, see the section on
this in our FAQ at http://cmusatyalab.github.io/openface/faq/
+ In Docker, use a Bash login shell or source
/root/torch/install/bin/torch-activate for the Torch environment.
+ See this GitHub issue if you are running on a non-64-bit machine:
https://github.com/cmusatyalab/openface/issues/42
+ Advanced Users: If you think this problem is caused by
running Lua as a subprocess, Vitalius Parubochyi has created
a version of this that uses https://github.com/imodpasteur/lutorpy.
This file is available at <openface>/openface/torch_neural_net.lutorpy.py
and our mailing list discussion on this can be found at:
https://groups.google.com/forum/#!topic/cmu-openface/Jj68LJBdN-Y
+ Please post further issues to our mailing list at
https://groups.google.com/forum/#!forum/cmu-openface
Diagnostic information:
cmd: ['/usr/bin/env', 'th', '/home/aanilil/anaconda3/envs/tensorflow/lib/python3.5/site-packages/openface/openface_server.lua', '-model', '/home/aanilil/ml/openface/models/openface/celeb-classifier.nn4.small2.v1.pkl', '-imgDim', '96']
============
stdout:
Process finished with exit code 1
Actual behavior.
I think the main problem is how I invoke the neural net.
As I'm not experienced in torch I have no idea if the params I sent are correct.
Steps to reproduce.
Copy paste my code and change the file names and paths. Run this file from anywhere outside the openface dir.
OS and hardware information.
Operating system: Ubuntu 16.04
Torch version: 7
CPU architecture: Intel(R) Xeon(R) CPU E5-2687W 0 @ 3.10GHz
Context of the issue.
My Code
But I get this error
Actual behavior.
I think the main problem is how I invoke the neural net. As I'm not experienced in torch I have no idea if the params I sent are correct.
Steps to reproduce.
Copy paste my code and change the file names and paths. Run this file from anywhere outside the openface dir.
OS and hardware information.