From 1b9e501692c2170b757bfa581c24f2688f3733b1 Mon Sep 17 00:00:00 2001 From: hanzhi713 Date: Sat, 11 Jan 2020 14:51:56 -0500 Subject: [PATCH 1/2] integral parameters --- python/examples/segnet-camera.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/examples/segnet-camera.py b/python/examples/segnet-camera.py index e22f18360..bebb501b3 100755 --- a/python/examples/segnet-camera.py +++ b/python/examples/segnet-camera.py @@ -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) @@ -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 From cd16f626c1c8a7fddc4c73660076fd573e62e07b Mon Sep 17 00:00:00 2001 From: hanzhi713 Date: Sat, 11 Jan 2020 14:52:41 -0500 Subject: [PATCH 2/2] binding for another overload of segNet::Mask --- python/bindings/PySegNet.cpp | 63 +++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/python/bindings/PySegNet.cpp b/python/bindings/PySegNet.cpp index b4fdfea40..2352bb2ab 100644 --- a/python/bindings/PySegNet.cpp +++ b/python/bindings/PySegNet.cpp @@ -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" \ @@ -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},