Skip to content

Instantly share code, notes, and snippets.

@patricksnape
Created June 19, 2017 21:14
Show Gist options
  • Save patricksnape/8e6e7aad6c1d7317e7b2eb463ec29888 to your computer and use it in GitHub Desktop.
Save patricksnape/8e6e7aad6c1d7317e7b2eb463ec29888 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true,
"collapsed": false,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "%matplotlib inline\nimport numpy as np\nfrom pathlib import Path\n\nimport menpo.io as mio\nfrom menpo.feature import fast_dsift\nfrom menpo.transform import AlignmentSimilarity, scale_about_centre\n\nfrom menpodetect import load_dlib_frontal_face_detector\nfrom menpowidgets import visualize_images, visualize_fitting_results\n\nfrom menpofit.aam import LucasKanadeAAMFitter, PatchAAM\n\n# Load the DLIB detector which is fairly strong\n# and has a very low number of false positives\ndetector = load_dlib_frontal_face_detector()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "face_indices = [4, 5, 6, 7, 8, 9]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "# Loading our images\nimages_folder = Path('/tmp/dataset+landmarks+template');\nimages = mio.import_images(images_folder)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "visualize_images(images)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "# Just a short helper method to crop the images\n# and turn to greyscale - again to save memory\ndef load_image(i):\n i, tr = i.crop_to_landmarks_proportion(0.3, return_transform=True)\n if i.n_channels == 3:\n i = i.as_greyscale()\n i = i.as_unmasked()\n i.landmarks['LJSON'] = i.landmarks['LJSON'].add_label('face', face_indices)\n return i",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "training_images = images.map(load_image)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "visualize_images(training_images)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"scrolled": true,
"collapsed": false,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "aam = PatchAAM(\n training_images,\n group='LJSON',\n verbose=True,\n holistic_features=fast_dsift, \n diagonal=200, \n patch_shape=[(19, 19), (17, 17), (15, 15)],\n scales=(0.25, 0.5, 1.0)\n)\n\nreference_face_points = aam.reference_shape.get_label('face')\nreference_bb = reference_face_points.bounding_box()\n\naam_fitter = LucasKanadeAAMFitter(aam, n_shape=[3, 5, 10], \n n_appearance=0.8)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "pretend_test_image = images[10]",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "face_bounding_box = detector(pretend_test_image)[0]\ninitial_bb = AlignmentSimilarity(reference_bb,\n face_bounding_box).apply(aam.reference_shape)\ninitial_bb = scale_about_centre(initial_bb, 0.7).apply(initial_bb)\nresult = aam_fitter.fit_from_shape(pretend_test_image, \n initial_bb, \n max_iters=20)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "result.view_widget()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "mio.export_landmark_file(result.final_shape, '/tmp/result.ljson')",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"file_extension": ".py",
"version": "3.5.3",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment