Skip to content
Snippets Groups Projects
Commit e186ee33 authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

Initial commit (prototype)

parents
No related branches found
No related tags found
No related merge requests found
up
run
run
run
run
break ext.cpp:77
run
n
print ipl_image
print ipl_image->px
print ipl_image->px->imageData
run
break ext.cpp:77
run
n
break 70
d 1
run
c
c
run
c
break ext.cpp:70
d 2
run
c
run
n
n
run
run
run
run
run
run
run
run
run
break ext.cpp:70
run
c
up
down
break ext.cpp:70
run
c
c
c
*~
*.swp
*.pyc
bin
eggs
parts
.installed.cfg
.mr.developer.cfg
*.egg-info
develop-eggs
sphinx
dist
.project
.pydevproject
##############################################################################
#
# Copyright (c) 2006 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Bootstrap a buildout-based project
Simply run this script in a directory containing a buildout.cfg.
The script accepts buildout command-line options, so you can
use the -c option to specify an alternate configuration file.
"""
import os, shutil, sys, tempfile, urllib, urllib2, subprocess
from optparse import OptionParser
if sys.platform == 'win32':
def quote(c):
if ' ' in c:
return '"%s"' % c # work around spawn lamosity on windows
else:
return c
else:
quote = str
# See zc.buildout.easy_install._has_broken_dash_S for motivation and comments.
stdout, stderr = subprocess.Popen(
[sys.executable, '-Sc',
'try:\n'
' import ConfigParser\n'
'except ImportError:\n'
' print 1\n'
'else:\n'
' print 0\n'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
has_broken_dash_S = bool(int(stdout.strip()))
# In order to be more robust in the face of system Pythons, we want to
# run without site-packages loaded. This is somewhat tricky, in
# particular because Python 2.6's distutils imports site, so starting
# with the -S flag is not sufficient. However, we'll start with that:
if not has_broken_dash_S and 'site' in sys.modules:
# We will restart with python -S.
args = sys.argv[:]
args[0:0] = [sys.executable, '-S']
args = map(quote, args)
os.execv(sys.executable, args)
# Now we are running with -S. We'll get the clean sys.path, import site
# because distutils will do it later, and then reset the path and clean
# out any namespace packages from site-packages that might have been
# loaded by .pth files.
clean_path = sys.path[:]
import site # imported because of its side effects
sys.path[:] = clean_path
for k, v in sys.modules.items():
if k in ('setuptools', 'pkg_resources') or (
hasattr(v, '__path__') and
len(v.__path__) == 1 and
not os.path.exists(os.path.join(v.__path__[0], '__init__.py'))):
# This is a namespace package. Remove it.
sys.modules.pop(k)
is_jython = sys.platform.startswith('java')
setuptools_source = 'http://peak.telecommunity.com/dist/ez_setup.py'
distribute_source = 'http://python-distribute.org/distribute_setup.py'
# parsing arguments
def normalize_to_url(option, opt_str, value, parser):
if value:
if '://' not in value: # It doesn't smell like a URL.
value = 'file://%s' % (
urllib.pathname2url(
os.path.abspath(os.path.expanduser(value))),)
if opt_str == '--download-base' and not value.endswith('/'):
# Download base needs a trailing slash to make the world happy.
value += '/'
else:
value = None
name = opt_str[2:].replace('-', '_')
setattr(parser.values, name, value)
usage = '''\
[DESIRED PYTHON FOR BUILDOUT] bootstrap.py [options]
Bootstraps a buildout-based project.
Simply run this script in a directory containing a buildout.cfg, using the
Python that you want bin/buildout to use.
Note that by using --setup-source and --download-base to point to
local resources, you can keep this script from going over the network.
'''
parser = OptionParser(usage=usage)
parser.add_option("-v", "--version", dest="version",
help="use a specific zc.buildout version")
parser.add_option("-d", "--distribute",
action="store_true", dest="use_distribute", default=False,
help="Use Distribute rather than Setuptools.")
parser.add_option("--setup-source", action="callback", dest="setup_source",
callback=normalize_to_url, nargs=1, type="string",
help=("Specify a URL or file location for the setup file. "
"If you use Setuptools, this will default to " +
setuptools_source + "; if you use Distribute, this "
"will default to " + distribute_source + "."))
parser.add_option("--download-base", action="callback", dest="download_base",
callback=normalize_to_url, nargs=1, type="string",
help=("Specify a URL or directory for downloading "
"zc.buildout and either Setuptools or Distribute. "
"Defaults to PyPI."))
parser.add_option("--eggs",
help=("Specify a directory for storing eggs. Defaults to "
"a temporary directory that is deleted when the "
"bootstrap script completes."))
parser.add_option("-t", "--accept-buildout-test-releases",
dest='accept_buildout_test_releases',
action="store_true", default=False,
help=("Normally, if you do not specify a --version, the "
"bootstrap script and buildout gets the newest "
"*final* versions of zc.buildout and its recipes and "
"extensions for you. If you use this flag, "
"bootstrap and buildout will get the newest releases "
"even if they are alphas or betas."))
parser.add_option("-c", None, action="store", dest="config_file",
help=("Specify the path to the buildout configuration "
"file to be used."))
options, args = parser.parse_args()
# if -c was provided, we push it back into args for buildout's main function
if options.config_file is not None:
args += ['-c', options.config_file]
if options.eggs:
eggs_dir = os.path.abspath(os.path.expanduser(options.eggs))
else:
eggs_dir = tempfile.mkdtemp()
if options.setup_source is None:
if options.use_distribute:
options.setup_source = distribute_source
else:
options.setup_source = setuptools_source
if options.accept_buildout_test_releases:
args.append('buildout:accept-buildout-test-releases=true')
args.append('bootstrap')
try:
import pkg_resources
import setuptools # A flag. Sometimes pkg_resources is installed alone.
if not hasattr(pkg_resources, '_distribute'):
raise ImportError
except ImportError:
ez_code = urllib2.urlopen(
options.setup_source).read().replace('\r\n', '\n')
ez = {}
exec ez_code in ez
setup_args = dict(to_dir=eggs_dir, download_delay=0)
if options.download_base:
setup_args['download_base'] = options.download_base
if options.use_distribute:
setup_args['no_fake'] = True
ez['use_setuptools'](**setup_args)
if 'pkg_resources' in sys.modules:
reload(sys.modules['pkg_resources'])
import pkg_resources
# This does not (always?) update the default working set. We will
# do it.
for path in sys.path:
if path not in pkg_resources.working_set.entries:
pkg_resources.working_set.add_entry(path)
cmd = [quote(sys.executable),
'-c',
quote('from setuptools.command.easy_install import main; main()'),
'-mqNxd',
quote(eggs_dir)]
if not has_broken_dash_S:
cmd.insert(1, '-S')
find_links = options.download_base
if not find_links:
find_links = os.environ.get('bootstrap-testing-find-links')
if find_links:
cmd.extend(['-f', quote(find_links)])
if options.use_distribute:
setup_requirement = 'distribute'
else:
setup_requirement = 'setuptools'
ws = pkg_resources.working_set
setup_requirement_path = ws.find(
pkg_resources.Requirement.parse(setup_requirement)).location
env = dict(
os.environ,
PYTHONPATH=setup_requirement_path)
requirement = 'zc.buildout'
version = options.version
if version is None and not options.accept_buildout_test_releases:
# Figure out the most recent final version of zc.buildout.
import setuptools.package_index
_final_parts = '*final-', '*final'
def _final_version(parsed_version):
for part in parsed_version:
if (part[:1] == '*') and (part not in _final_parts):
return False
return True
index = setuptools.package_index.PackageIndex(
search_path=[setup_requirement_path])
if find_links:
index.add_find_links((find_links,))
req = pkg_resources.Requirement.parse(requirement)
if index.obtain(req) is not None:
best = []
bestv = None
for dist in index[req.project_name]:
distv = dist.parsed_version
if _final_version(distv):
if bestv is None or distv > bestv:
best = [dist]
bestv = distv
elif distv == bestv:
best.append(dist)
if best:
best.sort()
version = best[-1].version
if version:
requirement = '=='.join((requirement, version))
cmd.append(requirement)
if is_jython:
import subprocess
exitcode = subprocess.Popen(cmd, env=env).wait()
else: # Windows prefers this, apparently; otherwise we would prefer subprocess
exitcode = os.spawnle(*([os.P_WAIT, sys.executable] + cmd + [env]))
if exitcode != 0:
sys.stdout.flush()
sys.stderr.flush()
print ("An error occurred when trying to install zc.buildout. "
"Look above this message for any errors that "
"were output by easy_install.")
sys.exit(exitcode)
ws.add_entry(eggs_dir)
ws.require(requirement)
import zc.buildout.buildout
zc.buildout.buildout.main(args)
if not options.eggs: # clean up temporary egg directory
shutil.rmtree(eggs_dir)
; vim: set fileencoding=utf-8 :
; Andre Anjos <andre.anjos@idiap.ch>
; Mon 16 Apr 08:29:18 2012 CEST
[buildout]
parts = external python
develop = .
eggs = bob
flandmark
[external]
recipe = xbob.buildout:external
egg-directories = /idiap/group/torch5spro/nightlies/last/install/linux-x86_64-release/lib
[python]
recipe = zc.recipe.egg
interpreter = python
eggs = ${buildout:eggs}
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Thu 20 Sep 2012 14:46:35 CEST
*
* @brief Boost.Python extension to flandmark
*/
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
#include <boost/python.hpp>
#include <bob/core/python/ndarray.h>
#include <cv.h>
#include "flandmark_detector.h"
using namespace boost::python;
static void delete_cascade(CvHaarClassifierCascade* o) {
cvReleaseHaarClassifierCascade(&o);
}
static void delete_flandmark(FLANDMARK_Model* o) {
flandmark_free(o);
}
static void delete_image(IplImage* i) {
cvReleaseImage(&i);
}
static void delete_storage(CvMemStorage* s) {
cvReleaseMemStorage(&s);
}
/**
* A simple wrapper to operate the flandmark library quickly in iterative
* environments like Python.
*/
class Localizer {
public: //api
/**
* Constructor, has to load a boosted cascaded from OpenCV and the
* flandmark model.
*/
Localizer(const std::string& opencv_cascade,
const std::string& flandmark_model) :
m_cascade((CvHaarClassifierCascade*)cvLoad(opencv_cascade.c_str(), 0, 0, 0), std::ptr_fun(delete_cascade)),
m_flandmark(flandmark_init(flandmark_model.c_str()), std::ptr_fun(delete_flandmark))//,
//m_storage(cvCreateMemStorage(0), std::ptr_fun(delete_storage))
{
if( !m_cascade ) {
PYTHON_ERROR(RuntimeError, "Couldnt load Face detector '%s'", opencv_cascade.c_str());
}
if ( !m_flandmark ) {
PYTHON_ERROR(RuntimeError, "Structure model wasn't created. Corrupted file '%s'", flandmark_model.c_str());
}
m_landmarks.reset(new float[2*m_flandmark->data.options.M]);
}
/**
* Locates the landmarks from an input image
*/
tuple operator() (bob::python::const_ndarray input) {
//bob::python::no_gil unlock;
//checks type
const bob::core::array::typeinfo& type = input.type();
if ((type.dtype != bob::core::array::t_uint8) || (type.nd != 2)) {
PYTHON_ERROR(TypeError, "Input data must be a 2D numpy.array with dtype=uint8 (i.e. a gray-scaled image), but you passed %s", type.str().c_str());
}
//converts to IplImage
const blitz::Array<uint8_t, 2> bz = input.bz<uint8_t,2>();
boost::shared_ptr<IplImage> ipl_image(cvCreateImage(cvSize(type.shape[1],
type.shape[0]), IPL_DEPTH_8U, 1), std::ptr_fun(delete_image));
char* saved_ipl_data = ipl_image->imageData;
ipl_image->imageData = (char*)bz.data(); //replace
// Flags for OpenCV face detection
CvSize minFeatureSize = cvSize(40, 40);
int flags = CV_HAAR_DO_CANNY_PRUNING;
float search_scale_factor = 1.1f;
// Detect all the faces in the greyscale image.
boost::shared_ptr<CvMemStorage> m_storage(cvCreateMemStorage(0),
std::ptr_fun(delete_storage));
cvClearMemStorage(m_storage.get());
CvSeq* rects = cvHaarDetectObjects(ipl_image.get(), m_cascade.get(),
m_storage.get(), search_scale_factor, 2, flags, minFeatureSize);
int nFaces = rects->total;
list retval;
for (int iface = 0; iface < (rects ? nFaces : 0); ++iface) {
CvRect* r = (CvRect*)cvGetSeqElem(rects, iface);
dict det;
det["bbox"] = make_tuple(r->x, r->y, r->width, r->height);
int bbox[4] = {r->x, r->y, r->x + r->width, r->y + r->height};
flandmark_detect(ipl_image.get(), bbox, m_flandmark.get(),
m_landmarks.get());
list lmlist; ///< landmark list
// The first point represents the center of the bounding box used by
// the flandmark library.
for (int i = 2; i < (2*m_flandmark->data.options.M); i += 2) {
lmlist.append(make_tuple(m_landmarks[i], m_landmarks[i+1]));
}
det["landmark"] = lmlist;
retval.append(det);
}
ipl_image->imageData = saved_ipl_data;
return tuple(retval);
}
private: //representation
boost::shared_ptr<CvHaarClassifierCascade> m_cascade;
boost::shared_ptr<FLANDMARK_Model> m_flandmark;
//boost::shared_ptr<CvMemStorage> m_storage;
boost::shared_array<float> m_landmarks;
};
BOOST_PYTHON_MODULE(_flandmark) {
bob::python::setup_python("bindings to flandmark - a library for the localization of facial landmarks");
class_<Localizer>("Localizer", "A key-point localization for faces using flandmark", init<const std::string&, const std::string&>((arg("detector"), arg("localizer")), "Initializes with both an OpenCV face detector model and an flandmark model"))
.def("__call__", &Localizer::operator(), (arg("self"), arg("image")), "Locates (possibly multiple) key-points on the given input image. Returns a list of located faces (by OpenCV's model), each attached to a list of key-points.")
;
}
This diff is collapsed.
#ifndef __FLANDMARK_DETECTOR_H_
#define __FLANDMARK_DETECTOR_H_
#include <stdint.h>
#include <cv.h>
#include <cvaux.h>
// index row-order matrices
#define INDEX(ROW, COL, NUM_ROWS) ((COL)*(NUM_ROWS)+(ROW))
#define ROW(IDX, ROWS) (((IDX)-1) % (ROWS))
#define COL(IDX, ROWS) (((IDX)-1) / (ROWS))
typedef struct psig_struct {
int * disp;
int ROWS, COLS;
} FLANDMARK_PSIG;
typedef struct options_struct {
uint8_t M;
int * S;
int bw[2], bw_margin[2];
FLANDMARK_PSIG *PsiGS0, *PsiGS1, *PsiGS2;
int PSIG_ROWS[3], PSIG_COLS[3];
} FLANDMARK_Options;
typedef struct lbp_struct {
int winSize[2];
uint8_t hop;
uint32_t *wins;
int WINS_ROWS, WINS_COLS;
} FLANDMARK_LBP;
typedef struct data_struct {
FLANDMARK_LBP * lbp;
int Images_ROWS, Images_COLS;
int imSize[2];
int * mapTable;
FLANDMARK_Options options;
} FLANDMARK_Data;
typedef struct model_struct {
double * W;
int W_ROWS, W_COLS;
FLANDMARK_Data data;
uint8_t *normalizedImageFrame;
IplImage *croppedImage;
IplImage *resizedImage;
double *bb;
float *sf;
int p_width, p_height;
} FLANDMARK_Model;
typedef struct psi_struct {
char * data;
uint32_t PSI_ROWS, PSI_COLS;
} FLANDMARK_PSI;
typedef struct psi_sparse {
uint32_t * idxs;
uint32_t PSI_ROWS, PSI_COLS;
} FLANDMARK_PSI_SPARSE;
// -------------------------------------------------------------------------
enum EError_T {
NO_ERR=0,
ERROR_M=1,
ERROR_BW=2,
ERROR_BW_MARGIN=3,
ERROR_W=4,
ERROR_DATA_IMAGES=5,
ERROR_DATA_MAPTABLE=6,
ERROR_DATA_LBP=7,
ERROR_DATA_OPTIONS_S=8,
ERROR_DATA_OPTIONS_PSIG=9,
UNKNOWN_ERROR=100,
};
// read / write structure Model from / to file procedures
/**
* Function flandmark_init
*
* Given the path to the file containing the model in binary form, this function will return a pointer to this model. It returns null pointer in the case of failure
*
* \param[in] filename
* \return Pointer to the FLANDMARK_Model data structure
*/
FLANDMARK_Model * flandmark_init(const char* filename);
/**
* Function flandmark_write model
*
* This function writes given FLANDMARK_model data structure to a file specified by its path.
*
* \param[in] filename
* \param[in] model
*/
void flandmark_writeModel(const char* filename, FLANDMARK_Model* model);
/**
* Function flandmark_checkModel
*
* This function checks if both given FLANDMARK_Model data structres are equal
*
* \param[in] model
* \param[in] tst
* \return
*/
EError_T flandmark_checkModel(FLANDMARK_Model* model, FLANDMARK_Model* tst);
/**
* Function flandmark_free
*
* This function dealocates the FLANDMARK_Model data structure
*
* \param[in] model
*/
void flandmark_free(FLANDMARK_Model* model);
// getPsiMat (calls LBP features computation - liblbpfeatures from LIBOCAS)
/**
*
* \param[out] Psi
* \param[in] model
* \param[in] lbpidx
*/
void flandmark_getPsiMat(FLANDMARK_PSI* Psi, FLANDMARK_Model* model, int lbpidx);
/**
* Computes LBP features representing it as sparse matrix (i.e. only inices with ones are stored in connected list)
*
* \param[out] Psi
* \param[in] model
* \param[in] lbpidx
*/
void flandmark_getPsiMatSparse(FLANDMARK_PSI_SPARSE* Psi, FLANDMARK_Model* model, int lbpidx);
// dot product maximization with max-index return
/**
* Function maximizedotprod
*
* \param[in]
* \param[in]
* \param[out]
*/
void maximize_gdotprod(double *maximum, double *idx, double *first, double *second, int *third, int cols, int tsize);
/**
* Function getNormalizedImageFrame
*
*
*/
int getNormalizedImageFrame(IplImage *input, const int bbox[], double *bb, uint8_t *face_img, FLANDMARK_Model *model);
int imCrop(IplImage *input, IplImage *output, const CvRect region, FLANDMARK_Model *model);
int imResize(IplImage *input, IplImage *output, int width, int height, FLANDMARK_Model *model);
/**
* Function flandmark_detect_base
*
* Estimates positions of facial landmarks in the normalized image frame.
*
* \param[in] face_image pointer to 1D uint8 array with normalized image frame of face
* \param[in] model Data structure holding info about model
* \param[in, out] int array representing 2D array of size [2 x options.M] with estimated positions of landmarks
* \return int indicator of success or fail of the detection
*/
int flandmark_detect_base(uint8_t *face_image, FLANDMARK_Model *model, float *landmarks);
/**
* Function flandmark_detect
*
* Estimates positions of facial landmarks given the image and the bounding box of the detected face
*
*/
int flandmark_detect(IplImage *img, int * bbox, FLANDMARK_Model *model, float * landmarks, int * bw_margin = 0);
#endif // __LIBFLD_DETECTOR_H_
#include "liblbp.h"
/*-----------------------------------------------------------------------
-----------------------------------------------------------------------*/
void liblbp_pyr_features_sparse(t_index* vec, uint32_t vec_nDim, uint32_t* img, uint16_t img_nRows, uint16_t img_nCols)
{
uint32_t offset, ww, hh, x, y, center, j, idx;
uint8_t pattern;
idx = 0;
offset = 0;
ww = img_nCols;
hh = img_nRows;
while(1)
{
for(x = 1; x < ww-1; x++)
{
for(y = 1; y< hh-1; y++)
{
pattern = 0;
center = img[LIBLBP_INDEX(y,x,img_nRows)];
if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern = pattern | 0x01;
if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern = pattern | 0x02;
if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern = pattern | 0x04;
if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern = pattern | 0x08;
if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern = pattern | 0x10;
if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern = pattern | 0x20;
if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern = pattern | 0x40;
if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern = pattern | 0x80;
//vec[offset+pattern]++;
vec[idx++] = offset+pattern;
offset += 256;
}
}
if(vec_nDim <= idx)
return;
if(ww % 2 == 1) ww--;
if(hh % 2 == 1) hh--;
ww = ww/2;
for(x=0; x < ww; x++)
for(j=0; j < hh; j++)
img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] +
img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
hh = hh/2;
for(y=0; y < hh; y++)
for(j=0; j < ww; j++)
img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
}
return;
}
/*-----------------------------------------------------------------------
-----------------------------------------------------------------------*/
void liblbp_pyr_features(char *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols )
{
uint32_t offset, ww, hh, x, y,center,j ;
uint8_t pattern;
offset=0;
/* ww=win_W;*/
/* hh=win_H;*/
ww=img_nCols;
hh=img_nRows;
while(1)
{
for(x=1; x < ww-1; x++)
{
for(y=1; y< hh-1; y++)
{
pattern = 0;
center = img[LIBLBP_INDEX(y,x,img_nRows)];
if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern = pattern | 0x01;
if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern = pattern | 0x02;
if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern = pattern | 0x04;
if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern = pattern | 0x08;
if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern = pattern | 0x10;
if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern = pattern | 0x20;
if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern = pattern | 0x40;
if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern = pattern | 0x80;
vec[offset+pattern]++;
offset += 256;
}
}
if(vec_nDim <= offset)
return;
if(ww % 2 == 1) ww--;
if(hh % 2 == 1) hh--;
ww = ww/2;
for(x=0; x < ww; x++)
for(j=0; j < hh; j++)
img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] +
img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
hh = hh/2;
for(y=0; y < hh; y++)
for(j=0; j < ww; j++)
img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
}
return;
}
/*-----------------------------------------------------------------------
-----------------------------------------------------------------------*/
double liblbp_pyr_dotprod(double *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols)
{
double dot_prod = 0;
uint32_t offset=0;
uint32_t ww, hh, center, x, y, j;
uint8_t pattern;
/* ww=win_W;*/
/* hh=win_H;*/
ww=img_nCols;
hh=img_nRows;
while(1)
{
for(x=1; x < ww-1; x++)
{
for(y=1; y< hh-1; y++)
{
pattern = 0;
center = img[LIBLBP_INDEX(y,x,img_nRows)];
if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern = pattern | 0x01;
if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern = pattern | 0x02;
if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern = pattern | 0x04;
if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern = pattern | 0x08;
if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern = pattern | 0x10;
if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern = pattern | 0x20;
if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern = pattern | 0x40;
if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern = pattern | 0x80;
dot_prod += vec[offset+pattern];
offset += 256;
}
}
if(vec_nDim <= offset)
return(dot_prod);
if(ww % 2 == 1) ww--;
if(hh % 2 == 1) hh--;
ww = ww/2;
for(x=0; x < ww; x++)
for(j=0; j < hh; j++)
img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] +
img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
hh = hh/2;
for(y=0; y < hh; y++)
for(j=0; j < ww; j++)
img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
}
}
/*-----------------------------------------------------------------------
-----------------------------------------------------------------------*/
void liblbp_pyr_addvec(int64_t *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols)
{
uint32_t offset, ww, hh, x, y, center,j ;
uint8_t pattern;
offset=0;
/* ww=win_W;*/
/* hh=win_H;*/
ww=img_nCols;
hh=img_nRows;
while(1)
{
for(x=1; x < ww-1; x++)
{
for(y=1; y< hh-1; y++)
{
pattern = 0;
center = img[LIBLBP_INDEX(y,x,img_nRows)];
if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern = pattern | 0x01;
if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern = pattern | 0x02;
if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern = pattern | 0x04;
if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern = pattern | 0x08;
if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern = pattern | 0x10;
if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern = pattern | 0x20;
if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern = pattern | 0x40;
if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern = pattern | 0x80;
vec[offset+pattern]++;
offset += 256;
}
}
if(vec_nDim <= offset)
return;
if(ww % 2 == 1) ww--;
if(hh % 2 == 1) hh--;
ww = ww/2;
for(x=0; x < ww; x++)
for(j=0; j < hh; j++)
img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] +
img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
hh = hh/2;
for(y=0; y < hh; y++)
for(j=0; j < ww; j++)
img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
}
return;
}
/*-----------------------------------------------------------------------
-----------------------------------------------------------------------*/
void liblbp_pyr_subvec(int64_t *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols)
{
uint32_t offset, ww, hh, x, y,center,j ;
uint8_t pattern;
offset=0;
/* ww=win_W;*/
/* hh=win_H;*/
ww=img_nCols;
hh=img_nRows;
while(1)
{
for(x=1; x < ww-1; x++)
{
for(y=1; y< hh-1; y++)
{
pattern = 0;
center = img[LIBLBP_INDEX(y,x,img_nRows)];
if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern = pattern | 0x01;
if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern = pattern | 0x02;
if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern = pattern | 0x04;
if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern = pattern | 0x08;
if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern = pattern | 0x10;
if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern = pattern | 0x20;
if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern = pattern | 0x40;
if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern = pattern | 0x80;
vec[offset+pattern]--;
offset += 256;
}
}
if(vec_nDim <= offset)
return;
if(ww % 2 == 1) ww--;
if(hh % 2 == 1) hh--;
ww = ww/2;
for(x=0; x < ww; x++)
for(j=0; j < hh; j++)
img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] +
img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
hh = hh/2;
for(y=0; y < hh; y++)
for(j=0; j < ww; j++)
img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
}
return;
}
/*-----------------------------------------------------------------------
-----------------------------------------------------------------------*/
uint32_t liblbp_pyr_get_dim(uint16_t img_nRows, uint16_t img_nCols, uint16_t nPyramids)
{
uint32_t w, h, N, i;
for(w=img_nCols, h=img_nRows, N=0, i=0; i < nPyramids && LIBLBP_MIN(w,h) >= 3; i++)
{
N += (w-2)*(h-2);
if(w % 2) w--;
if(h % 2) h--;
w = w/2;
h = h/2;
}
return(256*N);
}
#ifndef _liblbp_h
#define _liblbp_h
#include <stdint.h>
#define LIBLBP_INDEX(ROW,COL,NUM_ROWS) ((COL)*(NUM_ROWS)+(ROW))
#define LIBLBP_MIN(A,B) ((A) > (B) ? (B) : (A))
//typedef long unsigned int t_index;
typedef uint32_t t_index;
extern void liblbp_pyr_features_sparse(t_index *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols );
extern void liblbp_pyr_features(char *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols );
extern double liblbp_pyr_dotprod(double *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols);
extern void liblbp_pyr_addvec(int64_t *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols);
extern void liblbp_pyr_subvec(int64_t *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols);
extern uint32_t liblbp_pyr_get_dim(uint16_t img_nRows, uint16_t img_nCols, uint16_t nPyramids);
#endif
File added
This diff is collapsed.
setup.py 0 → 100644
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Andre Anjos <andre.anjos@idiap.ch>
# Thu 20 Sep 2012 14:43:19 CEST
"""Bindings for flandmark
"""
import sys
import subprocess
from distutils.core import setup
from distutils.extension import Extension
def pkgconfig(package):
def uniq(seq, idfun=None):
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
flag_map = {
'-I': 'include_dirs',
'-L': 'library_dirs',
'-l': 'libraries',
}
cmd = [
'pkg-config',
'--libs',
'--cflags',
package,
]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
output = proc.communicate()[0]
if proc.returncode != 0:
raise RuntimeError, "PkgConfig did not find package %s. Output:\n%s" % \
(package, output.strip())
kw = {}
for token in output.split():
if flag_map.has_key(token[:2]):
kw.setdefault(flag_map.get(token[:2]), []).append(token[2:])
else: # throw others to extra_link_args
kw.setdefault('extra_compile_args', []).append(token)
for k, v in kw.iteritems(): # remove duplicated
kw[k] = uniq(v)
return kw
def setup_bob_extension(ext_name, sources):
"""Sets up a given C++ extension that depends on Bob"""
bob = pkgconfig('bob-python')
ocv = pkgconfig('opencv')
return Extension(
ext_name,
sources=sources,
language="c++",
include_dirs=bob['include_dirs'] + ocv['include_dirs'],
library_dirs=bob['library_dirs'] + ocv.get('library_dirs', []),
runtime_library_dirs=bob['library_dirs'] + ocv.get('library_dirs', []),
libraries=bob['libraries'] + ocv['libraries'],
)
setup(
name="flandmark",
version="1.0.6",
ext_modules=[
setup_bob_extension("_flandmark",
[
"flandmark/ext/flandmark_detector.cpp",
"flandmark/ext/liblbp.cpp",
"flandmark/ext/ext.cpp",
])
])
test.py 0 → 100644
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Andre Anjos <andre.anjos@idiap.ch>
# Thu 20 Sep 2012 17:21:36 CEST
"""
"""
import sys
import bob
from _flandmark import Localizer
op = Localizer("flandmark/haarcascade_frontalface_alt.xml",
"flandmark/flandmark_model.dat")
for f in bob.io.VideoReader('/scratch/aanjos/bob/python/bob/io/test/data/test.mov'):
print "Hello!"
print op(bob.ip.rgb_to_gray(f))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment