From 274197c1e176bf44fa169e13b50abce9fd33278b Mon Sep 17 00:00:00 2001 From: Peter Rennert Date: Mon, 6 Feb 2017 20:35:27 +0000 Subject: [PATCH] ported to python3 --- Makefile | 8 +++--- features_pedro_py.cc | 60 +++++++++++++++++++++++++++++++++++++++++++- pyhog.py | 8 +++--- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index db7e6a6..1f9b021 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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: diff --git a/features_pedro_py.cc b/features_pedro_py.cc index 11f8bd9..71d712d 100644 --- a/features_pedro_py.cc +++ b/features_pedro_py.cc @@ -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 @@ -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, @@ -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 } diff --git a/pyhog.py b/pyhog.py index 0da15d0..5aca90d 100644 --- a/pyhog.py +++ b/pyhog.py @@ -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)