Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save patricksnape/afe444bd3fc390cf5e9a to your computer and use it in GitHub Desktop.
Save patricksnape/afe444bd3fc390cf5e9a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "%matplotlib inline\nimport numpy as np\nfrom functools import partial\nfrom collections import namedtuple\nimport menpodetect\nimport menpo.io as mio\nfrom menpo.base import LazyList\nfrom menpo.image import Image\nfrom menpo.shape import bounding_box, PointCloud\nfrom menpo.visualize import print_progress\nfrom menpowidgets import visualize_images\nfrom pathlib import Path\nimport skimage.draw as sk_draw\nimport csv\nimport matplotlib.pyplot as plt\nfrom scipy.io import loadmat\nfrom lxml import etree",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# Options"
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "IMPORT_IMAGES = False",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# AFLW"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "with open('/vol/hci2/Databases/video/AFLW/aflw/data/csv_from_sql_data/faces.csv', 'r') as f:\n reader = csv.reader(f)\n aflw_faces = list(reader)[1:]\n aflw_path_to_id = {x[1]: x[0] for x in faces}\n\nwith open('/vol/hci2/Databases/video/AFLW/aflw/data/csv_from_sql_data/facerect.csv', 'r') as f:\n reader = csv.reader(f)\n rects = list(reader)[1:]\n aflw_rects = {x[0]: map(np.flipud, np.split(np.array(map(int, x[1:-1])), 2)) for x in rects}\n \nwith open('/vol/hci2/Databases/video/AFLW/aflw/data/csv_from_sql_data/faceellipse.csv', 'r') as f:\n reader = csv.reader(f)\n ellipses = list(reader)[1:]\n aflw_ellipses = {x[0]: np.array(map(float, x[1:-2])) for x in ellipses}",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "%%time\nafw_paths = (list(mio.image_paths('/vol/atlas/databases/aflw/0/*')) +\n list(mio.image_paths('/vol/atlas/databases/aflw/2/*')) +\n list(mio.image_paths('/vol/atlas/databases/aflw/3/*')))",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "def aflw_attach_landmarks(path):\n if IMPORT_IMAGES:\n image = mio.import_image(path)\n else:\n image = Image.init_blank((1, 1))\n image.path = path\n \n im_id = aflw_path_to_id[path.name]\n ellipse = aflw_ellipses[im_id]\n rect = aflw_rects[im_id]\n \n e = map(int, map(np.round, ellipse))\n theta = ellipse[-1]\n e_perim = sk_draw.ellipse_perimeter(e[1], e[0], e[3], e[2], orientation=theta)\n e_perim = PointCloud(np.stack(e_perim, axis=1))\n \n image.landmarks['bbox_0'] = bounding_box(rect[0], rect[0] + rect[1])\n image.landmarks['ellipse_0']= e_perim\n image.landmarks['ellipse_bb_0']= e_perim.bounding_box()\n return image\n\naflw_images = LazyList.init_from_index_callable(lambda k: afw_paths[k], len(afw_paths)).map(aflw_attach_landmarks)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": true
},
"cell_type": "markdown",
"source": "# AFW"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "afw_headhunter_annotations = loadmat('/mnt/data/new_annotations_AFW.mat')['Annotations']",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "AFW_BASE_PATH = Path('/mnt/data/afw')\n\ndef afw_attach_landmarks(anno):\n path = AFW_BASE_PATH / anno[0][0][0]\n annotations = anno[0][-2]\n \n if IMPORT_IMAGES:\n image = mio.import_image(path)\n else:\n image = Image.init_blank((1, 1))\n image.path = path\n\n for k, a in enumerate(annotations):\n key = 'afw_{}{}'.format(k, '_ignore' if a[-1] else '')\n image.landmarks[key] = bounding_box(a[:2][::-1], a[2:4][::-1])\n return image\n\nafw_images = LazyList.init_from_index_callable(lambda k: afw_headhunter_annotations[k], \n len(afw_headhunter_annotations)).map(afw_attach_landmarks)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": true
},
"cell_type": "markdown",
"source": "# FDDB"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "FDDB_BASE_PATH = Path('/vol/atlas/databases/fddb')\nfddb_ellipses_list = sorted(list((FDDB_BASE_PATH / 'FDDB-folds').glob('*-ellipseList.txt')))\n\nFDDB_Ellipse = namedtuple('FDDB_Ellipse', ['major_axis_radius', 'minor_axis_radius', 'angle', \n 'center_x', 'center_y'])\n\nfddb_images_ellipses = []\nfor l in fddb_ellipses_list:\n with open(str(l), 'r') as f:\n for line in f:\n image_name = line.strip()\n n_faces = int(f.next().strip())\n ellipses = [FDDB_Ellipse(*[float(x) for x in f.next().strip().split(' ')[:-1] if x]) \n for k in range(n_faces)]\n fddb_images_ellipses.append((image_name, ellipses))",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "def fddb_attach_landmarks(anno):\n image_name, ellipses = anno\n path = (FDDB_BASE_PATH / image_name).with_suffix('.jpg')\n \n if IMPORT_IMAGES:\n image = mio.import_image(path)\n else:\n image = Image.init_blank((1, 1))\n image.path = path\n\n for k, e in enumerate(ellipses):\n theta = e.angle\n e_perim = sk_draw.ellipse_perimeter(int(e.center_y), \n int(e.center_x), \n int(e.minor_axis_radius), \n int(e.major_axis_radius), \n orientation=theta)\n e_perim = PointCloud(np.stack(e_perim, axis=1))\n\n image.landmarks['fddb_ellipse_{}'.format(k)] = e_perim\n image.landmarks['fddb_ellipse_bb_{}'.format(k)] = e_perim.bounding_box()\n return image\n\n\nfddb_images = LazyList.init_from_index_callable(lambda k: fddb_images_ellipses[k], \n len(fddb_images_ellipses)).map(fddb_attach_landmarks)",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# PASCAL"
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "PASCAL_BASE_PATH = Path('/vol/atlas/databases/VOC2012')\nPASCAL_JPEG_BASE_PATH = PASCAL_BASE_PATH / 'JPEGImages'\n\npascal_headhunter_annotations = loadmat('/mnt/data/Annotations_Face_PASCALLayout_large_fixed.mat')['Annotations']",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "def pascal_attach_landmarks(anno):\n path = PASCAL_JPEG_BASE_PATH / anno[0][0][0]\n annotations = anno[0][-2]\n \n if IMPORT_IMAGES:\n image = mio.import_image(path)\n else:\n image = Image.init_blank((1, 1))\n image.path = path\n\n for k, a in enumerate(annotations):\n key = 'pascal_{}{}'.format(k, '_ignore' if a[-1] else '')\n image.landmarks[key] = bounding_box(a[:2][::-1], a[2:4][::-1])\n return image\n\npascal_images = LazyList.init_from_index_callable(lambda k: pascal_headhunter_annotations[k], \n len(pascal_headhunter_annotations)).map(pascal_attach_landmarks)",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# Make DLIB XML"
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "def create_bbox_element(key, bbox):\n ignore = 'ignore' in key\n box = etree.Element('box')\n (top, left), _ = bbox.lms.bounds()\n height, width = bbox.lms.range()\n box.set('top', str(int(top)))\n box.set('left', str(int(left)))\n box.set('width', str(int(width)))\n box.set('height', str(int(height)))\n if ignore:\n box.set('ignore', '1')\n return box\n\n\ndef create_image_element(image):\n path = image.path\n image_e = etree.Element('image')\n image_e.set('file', str(path))\n \n for k in image.landmarks:\n image_e.append(\n create_bbox_element(k, image.landmarks[k]))\n return image_e\n\ndef create_dlib_xml(images):\n root = etree.Element('dataset')\n root.append(etree.Element('name'))\n root.append(etree.Element('comment'))\n\n images_e = etree.Element('images')\n root.append(images_e)\n\n for i in print_progress(images):\n images_e.append(create_image_element(i))\n\n et = etree.ElementTree(root)\n return et",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "et = create_dlib_xml(pascal_images + aflw_images + afw_images + fddb_images)\nwith open('/home/pts08/gits/dlib/tools/imglab/build/all.xml', 'w') as f:\n et.write(f, pretty_print=True)",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python2",
"display_name": "Python 2",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"name": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11",
"file_extension": ".py",
"codemirror_mode": {
"version": 2,
"name": "ipython"
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment