Commit 3764204d authored by Richard Torenvliet's avatar Richard Torenvliet

Initial commit

parents
## Prerequisites
Run the following command
~~~~
$ make
~~~~
Will get the data needed for this tool.
## References
1. [imm_dataset](http://www.imm.dtu.dk/~aam/datasets/datasets.html, "Imm dataset")
## Download the imm database
wget http://www.imm.dtu.dk/~aam/datasets/imm_face_db.tar.gz && tar -xvzf imm_face_db.tar.gz
all: data
data: data/imm_face_db
data/imm_face_db: data/imm_face_db.tar.gz
(cd data; \
mkdir -p imm_face_db; \
tar -xvzf imm_face_db.tar.gz -C imm_face_db \
)
data/imm_face_db.tar.gz:
(cd data; wget http://www.imm.dtu.dk/~aam/datasets/imm_face_db.tar.gz)
from matplotlib.tri import Triangulation
import cv2
import numpy as np
import argparse
class IMMPoints():
"""Accepts IMM datapoint file which can be shown or used"""
def __init__(self, filename=None, points=None):
"""
Args:
filename: optional .asf file with the imm format
points: optional list of x,y points
"""
self.points = points if points is not None else []
self.filename = filename
if filename:
self.import_file(filename)
def get_points(self):
return self.points
def import_file(self, filename):
with open(filename, 'r') as f:
lines = f.readlines()
# store the filename we've got
self.filename = lines[-1].strip()
data = lines[16:74]
for d in data:
self.points.append(d.split()[2:4])
self.points = np.asarray(self.points, dtype='f')
def draw_triangles(self, img, points):
assert(len(self.points) > 0)
h, w, c = img.shape
points[:, 0] = points[:, 0] * w
points[:, 1] = points[:, 1] * h
point_indices = list(range(0, 58))
triangles = Triangulation(points[:, 0], points[:, 1])
for t, tri in enumerate(triangles.triangles):
p1, p2, p3 = points[tri]
cv2.line(img, tuple(p1), tuple(p2), (255, 0, 100), 1)
cv2.line(img, tuple(p2), tuple(p3), (255, 0, 100), 1)
cv2.line(img, tuple(p3), tuple(p1), (255, 0, 100), 1)
for i, p in enumerate(points):
point_index = int(point_indices[i])
cv2.putText(img, str(point_index), tuple((p[0], p[1])),
cv2.FONT_HERSHEY_SIMPLEX, .5, (100, 0, 255))
cv2.circle(img, tuple(p), 3, color=(0, 255, 100))
def show_on_img(self, img):
self.draw_triangles(img, self.points)
cv2.imshow('image', img)
cv2.waitKey(0)
def show(self):
"""show the image and datapoints on the image"""
assert(len(self.points) > 0)
assert(len(self.filename) > 0)
img = cv2.imread('data/imm_face_db/' + self.filename)
self.draw_triangles(img, self.points)
cv2.imshow('image', img)
cv2.waitKey(0)
def add_parser_options():
parser = argparse.ArgumentParser(description='IMMPoints tool')
# asf files
parser.add_argument(
'asf', type=str, nargs='+', help='asf files to process'
)
return parser
if __name__ == '__main__':
parser = add_parser_options()
args = parser.parse_args()
for f in args.asf:
imm = IMMPoints(f)
imm.show()
from pca import pca
import numpy as np
import argparse
from imm_points import IMMPoints
def add_parser_options():
parser = argparse.ArgumentParser(description='IMMPoints tool')
# asf files
parser.add_argument(
'--asf', nargs='+', help='asf files to process'
)
return parser
def build_mean_aam(imm_points):
""" construct a mean from a matrix of x,y values
Args:
imm_points(numpy array)that follows the follwing structure:
observations:
0. [[x_0_0, y_0_0], ... , [x_0_m, y_0_m]],
1. [[x_1_0, y_1_0], ... , [x_1_m, y_1_m]],
2. [[x_2_0, y_2_0], ... , [x_2_m, y_2_m]],
3. [[x_3_0, y_3_0], ... , [x_3_m, y_3_m]]
.... .... .m...
n. [[x_4_0, y_4_0], ... , [x_n_m, y_n_m]]
Returns mean_values (numpy array)
This vector containts the mean values of the corresponding column, like so:
0. [[x_0_0, y_0_0], ... , [x_0_k, y_0_k]],
1. [[x_1_0, y_1_0], ... , [x_1_k, y_1_k]],
2. [[x_2_0, y_2_0], ... , [x_2_k, y_2_k]],
3. [[x_3_0, y_3_0], ... , [x_3_k, y_3_k]]
.... .... .....
n. [[x_4_0, y_4_0], ... , [x_n_k, y_n_k]]
mean. [[x_mean_0, y_mean_0], ... [x_mean_n, y_mean_n]]
"""
mean_values = []
for i in range(imm_points.shape[1]):
mean_values.append(np.mean(imm_points[:, i], axis=0))
return np.array(mean_values)
if __name__ == '__main__':
parser = add_parser_options()
args = parser.parse_args()
if args.asf:
imm_points = []
for f in args.asf:
imm = IMMPoints(filename=f)
imm_points.append(imm.get_points())
# imm.show()
mean_values = build_mean_aam(np.array(imm_points))
pca(imm_points, mean_values)
# show immpoints
imm = IMMPoints(points=mean_values)
img = np.full((480, 640, 3), 255, np.uint8)
imm.show_on_img(img)
def pca(data, mean_values):
print mean_values
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