Skip to content

Instantly share code, notes, and snippets.

@grigorisg9gr
Created April 11, 2016 11:40
Show Gist options
  • Save grigorisg9gr/a35f3032815c9e9d4ed19d8d2bd1ccf6 to your computer and use it in GitHub Desktop.
Save grigorisg9gr/a35f3032815c9e9d4ed19d8d2bd1ccf6 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import random\n",
"from os.path import isdir, join\n",
"import menpo.io as mio"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the datasets to be loaded "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"p0 = '/vol/atlas/databases/'\n",
"assert(isdir(p0))\n",
"folds = ['ibug', 'afw', '300w', 'helen/trainset', \n",
" 'helen/testset', 'lfpw/trainset', 'lfpw/testset']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load the actual landmark files "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"shapes = []\n",
"for f in folds: \n",
" sh1 = [ln for ln in mio.import_landmark_files(join(p0, f, ''))]\n",
" shapes = shapes + sh1\n",
" \n",
"# below 'save' only the ones that have 68 points\n",
"shapes68 = []\n",
"for ln in shapes:\n",
" if ln.lms.points.shape[0] == 68:\n",
" # save only the ones with 68 points\n",
" shapes68.append(ln.lms)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# convert to 49 points from 68 mark-up.\n",
"from menpo.landmark import face_ibug_68_to_face_ibug_49 as i68to49\n",
"shapes68 = [i68to49(sh) for sh in shapes68]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build ATM "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# shuffle the shapes\n",
"random.Random(9).shuffle(shapes68)\n",
"# build the ATM\n",
"from menpofit.atm import HolisticATM\n",
"# dummy image for template model\n",
"im = mio.import_builtin_asset.einstein_jpg()\n",
"if shapes68[0].n_points == 49:\n",
" # if we have transformed the landmarks to 49, the same should \n",
" # happen to the template landmarks.\n",
" im.landmarks['PTS'] = i68to49(im.landmarks['PTS'])\n",
"atm = HolisticATM(im, shapes68, scales=[1.])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualise the ATM "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"try:\n",
" from menpowidgets.menpofit import visualize_atm\n",
" visualize_atm(atm)\n",
"except:\n",
" # in case we call this from the terminal.\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export the ATM"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# create a dictionary and export it.\n",
"p_exp = '/vol/atlas/homes/grigoris/videos_external/christos_georgakis/2016_04/'\n",
"assert(isdir(p_exp))\n",
"from time import gmtime, strftime\n",
"tm = strftime(\"%Y.%m.%d %H:%M:%S\", gmtime())\n",
"clas1 = {'folds' : folds, 'date' : tm, 'path_base' : p0, 'atm': atm}\n",
"mio.export_pickle(clas1, p_exp + 'shape_model_tt_49.pkl')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Export in mat format (only PCA) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"c = atm.shape_models[0].model\n",
"components = c.components\n",
"eigenvalues = c.eigenvalues\n",
"eig_cr = c.eigenvalues_cumulative_ratio()\n",
"\n",
"from scipy.io import savemat\n",
"savemat(p_exp + 'shape_model_tt_49.mat', {'components': components, 'eigenvalues' : eigenvalues, \n",
" 'eigenvalues_cumulative_ratio' : eig_cr})"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment