Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save grigorisg9gr/de65a67d71412499f7366e4531c3442b to your computer and use it in GitHub Desktop.
Save grigorisg9gr/de65a67d71412499f7366e4531c3442b 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": true
},
"outputs": [],
"source": [
"# Load a video, apply a (face) detector and then track.\n",
"\n",
"# I do recommend either using the dlib detector if time \n",
"# performance is crucial for you or the ffld2 (DPM model) \n",
"# if you want the 'optimal' detection. A nice review of \n",
"# why and how these options work for detection can be \n",
"# found here: \n",
"# http://ibug.doc.ic.ac.uk/media/uploads/documents/1603.06015v1.pdf "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from os.path import isdir, join\n",
"import numpy as np\n",
"\n",
"import menpo.io as mio\n",
"from menpodetect.ffld2 import load_ffld2_frontal_face_detector\n",
"from menpodetect.dlib import load_dlib_frontal_face_detector\n",
"from menpodetect.dlib.conversion import pointgraph_to_rect\n",
"from dlib import shape_predictor\n",
"from menpo.shape import PointCloud\n",
"\n",
"try:\n",
" %matplotlib inline\n",
" from menpowidgets import visualize_images\n",
"except NameError:\n",
" # e.g. if you are in a terminal\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Auxiliary functions for detector/landmark localisation method"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# decide which detector to use: \n",
"# detector = load_ffld2_frontal_face_detector()\n",
"detector = load_dlib_frontal_face_detector()\n",
"\n",
"# landmark localisation function\n",
"def detection_to_pointgraph(detection):\n",
" return PointCloud(np.array([(p.y, p.x) for p in detection.parts()]))\n",
"\n",
"\n",
"def landm_loc(im, model):\n",
" # accepts an image, a model (dlibERT) and performs \n",
" # the landmark localisation. The bounding box should exist.\n",
" im_pili = np.array(im.as_PILImage())\n",
" # the group is defined as the first label attached to the image.\n",
" group = im.landmarks.group_labels[0]\n",
" ln = im.landmarks[group]\n",
" det_frame = model(im_pili, pointgraph_to_rect(ln.lms))\n",
" init_pc = detection_to_pointgraph(det_frame)\n",
" im.landmarks['g'] = init_pc\n",
" return init_pc\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Base paths"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# base path of your clips\n",
"pb = ('/vol/atlas/homes/grigoris/videos_external/'\n",
" 'kollias_dimitris/2016_12_semaine/mp4/')\n",
"assert isdir(pb), 'The base path does not exist.'\n",
"# path to export the landmarks to\n",
"p_out = 'path_to_export_the_landmarks'\n",
"\n",
"# base path of the landmark localisation model:\n",
"# e.g. if you use the dlib detector, you can\n",
"# download the following landmark localisation \n",
"# model: \n",
"# https://github.com/davisking/dlib-models\n",
"p_sp = 'path_to_model'\n",
"assert(isfile(p_sp))\n",
"model = shape_predictor(p_sp)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Main processing"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# import the video\n",
"ims = mio.import_video(join(pb, 'Session3.2PrudenceOperator.mov'))\n",
"# In case you have the frames, this should work by \n",
"# replacing the 'import_video' with 'import_images' \n",
"# in the appropriate paths."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# process all frames iteratively.\n",
"for cnt, im in enumerate(ims):\n",
" if im.n_channels == 3:\n",
" im = im.as_greyscale()\n",
" lns = detector(im)\n",
"\n",
" if im.landmarks.n_groups == 0:\n",
" # there are no detections\n",
" continue\n",
" name = '{0:06d}'.format(cnt)\n",
" im.constrain_landmarks_to_bounds()\n",
" # call the landmark localisation method.\n",
" landm_loc(im, model)\n",
" # export the landmarks\n",
" mio.export_landmark_file(im.landmarks['g'], \n",
" p_out + name + '.pts', overwrite=True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.5"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment