Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NUMPY=`python -c 'import numpy; print numpy.get_include()'`
PYROOT=`python -c 'import sys; print sys.prefix'`
VER=`python -c "import sys; print('%s.%s'%(sys.version_info[0],sys.version_info[1]))"`
NUMPY=`python -c 'import numpy; print(numpy.get_include())'`
PYROOT=`python -c 'import sys; print(sys.prefix)'`
PYTHON_LIB=`python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())"`
CC=g++
LIBS=
#FLAGS= -Wall -DNUMPYCHECK -fPIC
Expand All @@ -15,7 +15,7 @@ features_pedro_py.so: features_pedro_py.o
g++ $^ -shared -o $@ $(LIBS)

features_pedro_py.o: features_pedro_py.cc numpymacros.h
g++ -c $< $(FLAGS) -I$(NUMPY) -I$(PYROOT)/include/python$(VER) -I../src/ -o $@
g++ -c $< $(FLAGS) -I$(NUMPY) -I$(PYTHON_LIB) -I../src/ -o $@

.PHONY: clean
clean:
Expand Down
60 changes: 59 additions & 1 deletion features_pedro_py.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@

#include "numpymacros.h"


struct module_state {
PyObject *error;
};

#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif

// small value, used to avoid division by zero
#define eps 0.0001

Expand Down Expand Up @@ -256,6 +268,7 @@ static PyObject *process(PyObject *self, PyObject *args) {
return PyArray_Return(mxfeat);//Py_BuildValue("N", mxfeat);
}


static PyMethodDef features_pedro_py_methods[] = {
{"process",
process,
Expand All @@ -264,8 +277,53 @@ static PyMethodDef features_pedro_py_methods[] = {
{NULL, NULL, 0, NULL} /* sentinel*/
};

PyMODINIT_FUNC initfeatures_pedro_py() {

#if PY_MAJOR_VERSION >= 3

static int myextension_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(GETSTATE(m)->error);
return 0;
}

static int myextension_clear(PyObject *m) {
Py_CLEAR(GETSTATE(m)->error);
return 0;
}


static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"features_pedro_py",
NULL,
sizeof(struct module_state),
features_pedro_py_methods,
NULL,
myextension_traverse,
myextension_clear,
NULL
};

#define INITERROR return NULL

PyMODINIT_FUNC
PyInit_features_pedro_py(void)

#else
#define INITERROR return

void
initfeatures_pedro_py(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
Py_InitModule("features_pedro_py", features_pedro_py_methods);
#endif
import_array();

#if PY_MAJOR_VERSION >= 3
return module;
#endif
}

8 changes: 4 additions & 4 deletions pyhog.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ def hog_picture(w, bs=20):
bim1 = np.zeros((bs, bs))
bim1[:,round(bs/2)-1:round(bs/2)] = 1
bim = np.zeros((9,)+bim1.shape)
for i in xrange(9):
for i in range(9):
bim[i] = imrotate(bim1, -i*20)/255.0
s = w.shape
w = w.copy()
w[w < 0] = 0
im = np.zeros((bs*s[0], bs*s[1]))
for i in xrange(s[0]):
for i in range(s[0]):
iis = slice( i*bs, (i+1)*bs )
for j in xrange(s[1]):
for j in range(s[1]):
jjs = slice( j*bs, (j+1)*bs )
for k in xrange(9):
for k in range(9):
im[iis,jjs] += bim[k] * w[i,j,k+18]
return im/np.max(w)