-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
98 lines (90 loc) · 3.94 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
@AmineHorseman
Sep, 12th, 2016
"""
import tensorflow as tf
from tflearn import DNN
import time
import numpy as np
import argparse
import dlib
import cv2
import os
from skimage.feature import hog
from parameters import DATASET, TRAINING, NETWORK, VIDEO_PREDICTOR
from model import build_model
window_size = 24
window_step = 6
def load_model():
model = None
with tf.Graph().as_default():
print( "loading pretrained model...")
network = build_model()
model = DNN(network)
if os.path.isfile(TRAINING.save_model_path):
model.load(TRAINING.save_model_path)
else:
print( "Error: file '{}' not found".format(TRAINING.save_model_path))
return model
def get_landmarks(image, rects, predictor):
# this function have been copied from http://bit.ly/2cj7Fpq
if len(rects) > 1:
raise TooManyFaces
if len(rects) == 0:
raise NoFaces
return np.matrix([[p.x, p.y] for p in predictor(image, rects[0]).parts()])
def sliding_hog_windows(image):
hog_windows = []
for y in range(0, NETWORK.input_size, window_step):
for x in range(0, NETWORK.input_size, window_step):
window = image[y:y+window_size, x:x+window_size]
hog_windows.extend(hog(window, orientations=8, pixels_per_cell=(8, 8),
cells_per_block=(1, 1), visualise=False))
return hog_windows
def predict(image, model, shape_predictor=None):
# get landmarks
if NETWORK.use_landmarks or NETWORK.use_hog_and_landmarks or NETWORK.use_hog_sliding_window_and_landmarks:
face_rects = [dlib.rectangle(left=0, top=0, right=NETWORK.input_size, bottom=NETWORK.input_size)]
face_landmarks = np.array([get_landmarks(image, face_rects, shape_predictor)])
features = face_landmarks
if NETWORK.use_hog_sliding_window_and_landmarks:
hog_features = sliding_hog_windows(image)
hog_features = np.asarray(hog_features)
face_landmarks = face_landmarks.flatten()
features = np.concatenate((face_landmarks, hog_features))
else:
hog_features, _ = hog(image, orientations=8, pixels_per_cell=(16, 16),
cells_per_block=(1, 1), visualise=True)
hog_features = np.asarray(hog_features)
face_landmarks = face_landmarks.flatten()
features = np.concatenate((face_landmarks, hog_features))
tensor_image = image.reshape([-1, NETWORK.input_size, NETWORK.input_size, 1])
predicted_label = model.predict([tensor_image, features.reshape((1, -1))])
return get_emotion(predicted_label[0])
else:
tensor_image = image.reshape([-1, NETWORK.input_size, NETWORK.input_size, 1])
predicted_label = model.predict(tensor_image)
return get_emotion(predicted_label[0])
return None
def get_emotion(label):
if VIDEO_PREDICTOR.print_emotions:
print( "- Angry: {0:.1f}%\n- Happy: {1:.1f}%\n- Sad: {2:.1f}%\n- Surprise: {3:.1f}%\n- Neutral: {4:.1f}%".format(
label[0]*100, label[1]*100, label[2]*100, label[3]*100, label[4]*100))
label = label.tolist()
return VIDEO_PREDICTOR.emotions[label.index(max(label))], max(label)
# parse arg to see if we need to launch training now or not yet
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--image", help="Image file to predict")
args = parser.parse_args()
if args.image:
if os.path.isfile(args.image):
model = load_model()
image = cv2.imread(args.image, 0)
shape_predictor = dlib.shape_predictor(DATASET.shape_predictor_path)
start_time = time.time()
emotion, confidence = predict(image, model, shape_predictor)
total_time = time.time() - start_time
print( "Prediction: {0} (confidence: {1:.1f}%)".format(emotion, confidence*100))
print( "time: {0:.1f} sec".format(total_time))
else:
print( "Error: file '{}' not found".format(args.image))