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

Add Python binding to another overload of segNet::Mask #476

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
63 changes: 62 additions & 1 deletion python/bindings/PySegNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,66 @@ static PyObject* PySegNet_Mask( PySegNet_Object* self, PyObject* args, PyObject
}


#define DOC_MASKCLASS "Produce a grayscale binary segmentation mask, where the pixel values correspond to the class ID of the corresponding class type.\n\n" \
"Parameters:\n" \
" image (capsule) -- output CUDA memory capsule\n" \
" width (int) -- width of the image (in pixels)\n" \
" height (int) -- height of the image (in pixels)\n" \
"Returns: (none)"

// Class Id Mask
static PyObject* PySegNet_MaskClass( PySegNet_Object* self, PyObject* args, PyObject *kwds )
{
if( !self || !self->net )
{
PyErr_SetString(PyExc_Exception, LOG_PY_INFERENCE "segNet invalid object instance");
return NULL;
}

// parse arguments
PyObject* capsule = NULL;

int width = 0;
int height = 0;

static char* kwlist[] = {"image", "width", "height", NULL};

if( !PyArg_ParseTupleAndKeywords(args, kwds, "Oii", kwlist, &capsule, &width, &height))
{
PyErr_SetString(PyExc_Exception, LOG_PY_INFERENCE "segNet.Mask() failed to parse args tuple");
printf(LOG_PY_INFERENCE "segNet.Mask() failed to parse args tuple\n");
return NULL;
}

// verify dimensions
if( width <= 0 || height <= 0 )
{
PyErr_SetString(PyExc_Exception, LOG_PY_INFERENCE "segNet.Mask() image dimensions are invalid");
return NULL;
}

// get pointer to image data
void* img = PyCUDA_GetPointer(capsule);

if( !img )
{
PyErr_SetString(PyExc_Exception, LOG_PY_INFERENCE "segNet.Mask() failed to get image pointer from PyCapsule container");
return NULL;
}

// visualize the image
const bool result = self->net->Mask((uint8_t*)img, width, height);

if( !result )
{
PyErr_SetString(PyExc_Exception, LOG_PY_INFERENCE "segNet.Mask() encountered an error segmenting the image");
return NULL;
}

Py_RETURN_NONE;
}


#define DOC_GET_NETWORK_NAME "Return the name of the built-in network used by the model.\n\n" \
"Parameters: (none)\n\n" \
"Returns:\n" \
Expand Down Expand Up @@ -498,7 +558,8 @@ static PyMethodDef PySegNet_Methods[] =
{
{ "Process", (PyCFunction)PySegNet_Process, METH_VARARGS|METH_KEYWORDS, DOC_PROCESS},
{ "Overlay", (PyCFunction)PySegNet_Overlay, METH_VARARGS|METH_KEYWORDS, DOC_OVERLAY},
{ "Mask", (PyCFunction)PySegNet_Mask, METH_VARARGS|METH_KEYWORDS, DOC_MASK},
{ "Mask", (PyCFunction)PySegNet_Mask, METH_VARARGS|METH_KEYWORDS, DOC_MASK},
{ "MaskClass", (PyCFunction)PySegNet_MaskClass, METH_VARARGS|METH_KEYWORDS, DOC_MASKCLASS},
{ "GetNetworkName", (PyCFunction)PySegNet_GetNetworkName, METH_NOARGS, DOC_GET_NETWORK_NAME},
{ "GetNumClasses", (PyCFunction)PySegNet_GetNumClasses, METH_NOARGS, DOC_GET_NUM_CLASSES},
{ "GetClassDesc", (PyCFunction)PySegNet_GetClassDesc, METH_VARARGS, DOC_GET_CLASS_DESC},
Expand Down
6 changes: 3 additions & 3 deletions python/examples/segnet-camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

# allocate the output images for the overlay & mask
img_overlay = jetson.utils.cudaAllocMapped(opt.width * opt.height * 4 * ctypes.sizeof(ctypes.c_float))
img_mask = jetson.utils.cudaAllocMapped(opt.width/2 * opt.height/2 * 4 * ctypes.sizeof(ctypes.c_float))
img_mask = jetson.utils.cudaAllocMapped(opt.width * opt.height * ctypes.sizeof(ctypes.c_float))

# create the camera and display
camera = jetson.utils.gstCamera(opt.width, opt.height, opt.camera)
Expand All @@ -71,12 +71,12 @@

# generate the overlay and mask
net.Overlay(img_overlay, width, height, opt.filter_mode)
net.Mask(img_mask, width/2, height/2, opt.filter_mode)
net.Mask(img_mask, width // 2, height// 2, opt.filter_mode)

# render the images
display.BeginRender()
display.Render(img_overlay, width, height)
display.Render(img_mask, width/2, height/2, width)
display.Render(img_mask, width // 2, height // 2, width)
display.EndRender()

# update the title bar
Expand Down