Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 compatible #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 15 additions & 7 deletions pyfreenect2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "pyfreenect2.hpp"
#include <iostream>
#include <Python.h>

// Define methods
static PyMethodDef pyfreenect2Methods[] = {
// Freenect2
{ "numberOfDevices", py_numberOfDevices, METH_VARARGS, NULL },
@@ -36,12 +38,18 @@ static PyMethodDef pyfreenect2Methods[] = {
{ NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC init_pyfreenect2() {

/// enables debug of libfreenect2
///libfreenect2::setGlobalLogger(libfreenect2::createConsoleLogger(libfreenect2::Logger::Debug));
// Define module
static struct PyModuleDef pyfreenect2module = {
PyModuleDef_HEAD_INIT,
"_pyfreenect2",
NULL,
-1,
pyfreenect2Methods
};

import_array();
Py_InitModule("_pyfreenect2", pyfreenect2Methods);
import_array();
// Initialize module
PyMODINIT_FUNC PyInit__pyfreenect2(void) {
import_array(); // Initialize numpy C-API
return PyModule_Create(&pyfreenect2module);
}

9 changes: 7 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -16,7 +16,12 @@
include_dirs=[numpy.get_include()],
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")])

setup(name="pyfreenect2",
setup(
name="pyfreenect2",
version="0.0.1",
description="Python wrapper for libfreenect2",
ext_modules=[libfreenect2_module],
py_modules=["pyfreenect2"],
install_requires=["numpy"])
install_requires=["numpy"]
)

13 changes: 7 additions & 6 deletions src/Frame.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../pyfreenect2.hpp"
#include <iostream>
#include "SmartFrame.h"
#include <numpy/arrayobject.h>

using libfreenect2::Frame;

@@ -14,9 +15,9 @@ PyObject *py_Frame_getHeight(PyObject *self, PyObject *args) {
return NULL;
SPFrame *spFrame = (SPFrame*) PyCapsule_GetPointer(frameCapsule, "Frame");
Frame *frame = spFrame->acquire();
int height = frame->height;
size_t height = frame->height;
spFrame->release();
return PyInt_FromSize_t(height);
return PyLong_FromSize_t(height); // Updated here
}

PyObject *py_Frame_getWidth(PyObject *self, PyObject *args) {
@@ -25,9 +26,9 @@ PyObject *py_Frame_getWidth(PyObject *self, PyObject *args) {
return NULL;
SPFrame *spFrame = (SPFrame*) PyCapsule_GetPointer(frameCapsule, "Frame");
Frame *frame = spFrame->acquire();
int width = frame->width;
size_t width = frame->width;
spFrame->release();
return PyInt_FromSize_t(width);
return PyLong_FromSize_t(width); // Updated here
}

PyObject *py_Frame_getData(PyObject *self, PyObject *args) {
@@ -38,7 +39,7 @@ PyObject *py_Frame_getData(PyObject *self, PyObject *args) {
Frame *frame = spFrame->acquire();

// frames are apparently 4 channel (4 bytes per pixel)
npy_intp dims[] = {frame->height, frame->width, 4 };
npy_intp dims[] = {static_cast<npy_intp>(frame->height), static_cast<npy_intp>(frame->width), 4}; // Updated here

// this should be elsewhere, however, fails without it.
import_array();
@@ -59,7 +60,7 @@ PyObject *py_Frame_getDepthData(PyObject *self, PyObject *args){
SPFrame *spFrame = (SPFrame*) PyCapsule_GetPointer(frameCapsule, "Frame");
Frame *frame = spFrame->acquire();

npy_intp dims[] = {frame->height, frame->width, 4};
npy_intp dims[] = {static_cast<npy_intp>(frame->height), static_cast<npy_intp>(frame->width), 4}; // Updated here

import_array();

4 changes: 2 additions & 2 deletions src/Freenect2.cpp
Original file line number Diff line number Diff line change
@@ -8,12 +8,12 @@ Freenect2& getGlobalFreenect2() { return freenect2; }
PyObject *py_numberOfDevices(PyObject *self, PyObject *args) {
if(!PyArg_ParseTuple(args, ""))
return NULL;
return PyInt_FromLong(freenect2.enumerateDevices());
return PyLong_FromLong(freenect2.enumerateDevices()); // Updated here
}

PyObject *py_getDefaultDeviceSerialNumber(PyObject *self, PyObject *args) {
if(!PyArg_ParseTuple(args, ""))
return NULL;
std::string serialNumber = freenect2.getDefaultDeviceSerialNumber();
return PyString_FromString(serialNumber.c_str());
return PyUnicode_FromString(serialNumber.c_str()); // Updated here
}
4 changes: 2 additions & 2 deletions src/Freenect2Device.cpp
Original file line number Diff line number Diff line change
@@ -104,15 +104,15 @@ PyObject *py_Freenect2Device_getSerialNumber(PyObject *self, PyObject *args) {
return NULL;
Freenect2Device *device = (Freenect2Device*) PyCapsule_GetPointer(deviceCapsule, "Freenect2Device");
std::string serialNumber = device->getSerialNumber();
return PyString_FromString(serialNumber.c_str());
return PyUnicode_FromString(serialNumber.c_str()); // Updated here
}
PyObject *py_Freenect2Device_getFirmwareVersion(PyObject *self, PyObject *args) {
PyObject *deviceCapsule = NULL;
if(!PyArg_ParseTuple(args, "O", &deviceCapsule))
return NULL;
Freenect2Device *device = (Freenect2Device*) PyCapsule_GetPointer(deviceCapsule, "Freenect2Device");
std::string firmwareVersion = device->getFirmwareVersion();
return PyString_FromString(firmwareVersion.c_str());
return PyUnicode_FromString(firmwareVersion.c_str()); // Updated here
}

PyObject *py_Freenect2Device_getColorCameraParams(PyObject *self, PyObject *args) {