Skip to content

Commit

Permalink
#48 respect rows alignment in CUDA memory
Browse files Browse the repository at this point in the history
  • Loading branch information
tomskikh committed Feb 3, 2023
1 parent 6bd1011 commit 8c99751
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions libs/savantboost/pysavantboost/include/nvsurfaceptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ namespace pysavantboost {
guint width();
guint height();
guint size();
guint pitch();
};
}
7 changes: 6 additions & 1 deletion libs/savantboost/pysavantboost/src/nvsurfaceptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ namespace pysavantboost {
return ds_cuda_memory->size();
}

guint PyDSCudaMemory::pitch() {
return ds_cuda_memory->pitch();
}

void bindnvsurfaceptr(py::module &m) {
py::class_<PyDSCudaMemory>(m, "PyDSCudaMemory")
.def(py::init<uint64_t, guint>())
.def("GetMapCudaPtr", &PyDSCudaMemory::GetMapCudaPtr)
.def("UnMapCudaPtr", &PyDSCudaMemory::UnMapCudaPtr)
.def_property_readonly("width", &PyDSCudaMemory::width)
.def_property_readonly("height", &PyDSCudaMemory::height)
.def_property_readonly("size", &PyDSCudaMemory::size);
.def_property_readonly("size", &PyDSCudaMemory::size)
.def_property_readonly("pitch", &PyDSCudaMemory::pitch);
}
}
4 changes: 4 additions & 0 deletions libs/savantboost/savantboost/deepstream/nvsurfaceptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ guint DSCudaMemory::height() {
guint DSCudaMemory::size() {
return _surface->surfaceList[_batch_id].dataSize;
}

guint DSCudaMemory::pitch() {
return _surface->surfaceList[_batch_id].pitch;
}
1 change: 1 addition & 0 deletions libs/savantboost/savantboost/deepstream/nvsurfaceptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DSCudaMemory {
guint width();
guint height();
guint size();
guint pitch();
};


Expand Down
22 changes: 20 additions & 2 deletions savant/deepstream/drawfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,27 @@ def __init__(self, **kwargs):
labels[label] = COLOR[color]

def __call__(self, nvds_frame_meta: pyds.NvDsFrameMeta, frame: np.ndarray):
frame_height, frame_width, _ = frame.shape
frame_height, frame_width, frame_channels = frame.shape
if not frame.flags['C_CONTIGUOUS']:
# Pycairo requires numpy array to be C-contiguous.
# Pyds can return non-contiguous array since rows in the array are aligned.
new_shape = (
frame_height,
frame.strides[0] // frame_channels,
frame_channels,
)
self.logger.debug(
'Converting numpy array of the shape %s to C-contiguous. New shape: %s.',
frame.shape,
new_shape,
)
frame = np.lib.stride_tricks.as_strided(frame, new_shape, frame.strides)
surface = ImageSurface.create_for_data(
frame, Format.ARGB32, frame_width, frame_height
frame,
Format.ARGB32,
frame_width,
frame_height,
frame.strides[0],
)
artist = Artist(Context(surface))
frame_meta = NvDsFrameMeta(frame_meta=nvds_frame_meta)
Expand Down
1 change: 1 addition & 0 deletions savant/deepstream/opencv_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def nvds_to_gpu_mat(
py_ds_cuda_memory.width,
cv2.CV_8UC4,
cuda_ptr,
py_ds_cuda_memory.pitch,
)
finally:
py_ds_cuda_memory.UnMapCudaPtr()
Expand Down

0 comments on commit 8c99751

Please sign in to comment.