Commit 8e5a7e83 authored by Richard Torenvliet's avatar Richard Torenvliet

refactored a little and add a script to generate and store the model

parent bafb7dbd
...@@ -10,6 +10,12 @@ data/imm_face_db: data/imm_face_db.tar.gz ...@@ -10,6 +10,12 @@ data/imm_face_db: data/imm_face_db.tar.gz
data/imm_face_db.tar.gz: data/imm_face_db.tar.gz:
(cd data; wget http://www.imm.dtu.dk/~aam/datasets/imm_face_db.tar.gz) (cd data; wget http://www.imm.dtu.dk/~aam/datasets/imm_face_db.tar.gz)
generate_test_model:
python src/main.py \
--store_pca \
--asf data/imm_face_db/*.asf \
--file data/pca_test_model
test: test:
py.test -f src/*_test.py py.test -f src/*_test.py
files=`ls data/imm_face_db/ | grep -E "^[0-2][0-9].*.asf"`
for f in $files; do
echo "data/imm_face_db/$f"
done
files=`ls data/imm_face_db/ | grep -E "^[3-4][0-9].*.asf"`
for f in $files; do
echo "data/imm_face_db/$f"
done
import copy import copy
import argparse import argparse
import logging
import cv2 import cv2
import numpy as np import numpy as np
from pca import pca, reconstruct import pca
from aam import get_mean from aam import get_mean
from imm_points import IMMPoints, build_feature_vectors, \ from imm_points import IMMPoints, build_feature_vectors, \
flatten_feature_vectors flatten_feature_vectors
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(name)s: %(message)s')
logger = logging.getLogger(__name__)
def nothing(_):
pass
def add_parser_options(): def add_parser_options():
...@@ -21,12 +23,12 @@ def add_parser_options(): ...@@ -21,12 +23,12 @@ def add_parser_options():
pca_group.add_argument( pca_group.add_argument(
'--store_pca', action='store_true', '--store_pca', action='store_true',
help='number of principle components to keep and are able to manipulate' help='Store the pca model'
) )
pca_group.add_argument( pca_group.add_argument(
'--show_pca', action='store_true', '--show_pca', action='store_true',
help='number of principle components to keep and are able to manipulate' help='Show and manipulate the stored PCA model'
) )
# asf files # asf files
...@@ -47,6 +49,10 @@ def add_parser_options(): ...@@ -47,6 +49,10 @@ def add_parser_options():
return parser return parser
def nothing(_):
pass
def init_eigenvalue_trackbars(n_components, s): def init_eigenvalue_trackbars(n_components, s):
cv2.namedWindow('eigenvalues') cv2.namedWindow('eigenvalues')
...@@ -54,10 +60,24 @@ def init_eigenvalue_trackbars(n_components, s): ...@@ -54,10 +60,24 @@ def init_eigenvalue_trackbars(n_components, s):
cv2.createTrackbar('{}'.format(i), 'eigenvalues', 500, 1000, nothing) cv2.createTrackbar('{}'.format(i), 'eigenvalues', 500, 1000, nothing)
def store_model(args): def store_pca_model(args):
""" """
Store the U, s, Vt and mean of all the asf datafiles given by the asf Store the U, s, Vt and mean of all the asf datafiles given by the asf
files. files.
It is stored in the following way:
np.load(filename, np.assary([U, s, Vt, mean_values])
And accessed by:
UsVtm = np.load(args.file)
U = UsVtm[0]
s = UsVtm[1]
Vt = UsVtm[2]
mean_values = UsVtm[3]
""" """
assert args.asf, '--asf files should be given' assert args.asf, '--asf files should be given'
assert args.file, '--file needs to be provided to store the pca model' assert args.file, '--file needs to be provided to store the pca model'
...@@ -65,12 +85,13 @@ def store_model(args): ...@@ -65,12 +85,13 @@ def store_model(args):
imm_points = build_feature_vectors(args.asf, flattened=True) imm_points = build_feature_vectors(args.asf, flattened=True)
mean_values = get_mean(imm_points) mean_values = get_mean(imm_points)
U, s, Vt = pca(imm_points, mean_values) U, s, Vt = pca.pca(imm_points, mean_values)
np.save(args.file, np.asarray([U, s, Vt, mean_values])) np.save(args.file, np.asarray([U, s, Vt, mean_values]))
logger.info('Stored pca model in %s', args.file)
def show_pca(args): def show_pca_model(args):
assert args.asf, '--asf files should be given to allow the image to be shown' assert args.asf, '--asf files should be given to allow the image to be shown'
assert args.file, '--file needs to be provided to get the pca model' assert args.file, '--file needs to be provided to get the pca model'
...@@ -80,7 +101,6 @@ def show_pca(args): ...@@ -80,7 +101,6 @@ def show_pca(args):
U = UsVtm[0] U = UsVtm[0]
s = UsVtm[1] s = UsVtm[1]
Vt = UsVtm[2] Vt = UsVtm[2]
mean_values = UsVtm[3] mean_values = UsVtm[3]
# init trackbars # init trackbars
...@@ -95,7 +115,7 @@ def show_pca(args): ...@@ -95,7 +115,7 @@ def show_pca(args):
s_copy = copy.copy(s) s_copy = copy.copy(s)
while True: while True:
projection = reconstruct(U, s_copy, Vt, n_components) projection = pca.reconstruct(U, s_copy, Vt, n_components)
X_reconstructed = (projection[index] + mean_values).reshape((58, 2)) X_reconstructed = (projection[index] + mean_values).reshape((58, 2))
imm = IMMPoints(points=X_reconstructed) imm = IMMPoints(points=X_reconstructed)
...@@ -122,9 +142,11 @@ def main(): ...@@ -122,9 +142,11 @@ def main():
args = parser.parse_args() args = parser.parse_args()
if args.show_pca: if args.show_pca:
show_pca(args) show_pca_model(args)
elif args.store_pca: elif args.store_pca:
store_model(args) store_pca_model(args)
elif args.reconstruct:
reconstruct_with_model(args)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment