Skip to content

Commit

Permalink
updated Python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dusty-nv committed Jun 18, 2020
1 parent bfb7234 commit 29f5896
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 10 deletions.
2 changes: 1 addition & 1 deletion python/examples/camera-viewer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
#
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
Expand Down
99 changes: 99 additions & 0 deletions python/examples/cuda-examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/python3
#
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#

import jetson.utils
import argparse


# parse the command line
parser = argparse.ArgumentParser(description='Perform some example CUDA processing on an image')

parser.add_argument("file_in", type=str, default="images/granny_smith_1.jpg", nargs='?', help="filename of the input image to process")
parser.add_argument("file_out", type=str, default="cuda-example.jpg", nargs='?', help="filename of the output image to save")

opt = parser.parse_args()


# convert colorspace
def convert_color(img, output_format):
converted_img = jetson.utils.cudaAllocMapped(width=img.width, height=img.height, format=output_format)
jetson.utils.cudaConvertColor(img, converted_img)
return converted_img


# center crop an image
def crop(img, crop_factor):
crop_border = ((1.0 - crop_factor[0]) * 0.5 * img.width,
(1.0 - crop_factor[1]) * 0.5 * img.height)

crop_roi = (crop_border[0], crop_border[1], img.width - crop_border[0], img.height - crop_border[1])

crop_img = jetson.utils.cudaAllocMapped(width=img.width * crop_factor[0],
height=img.height * crop_factor[1],
format=img.format)

jetson.utils.cudaCrop(img, crop_img, crop_roi)
return crop_img


# resize an image
def resize(img, resize_factor):
resized_img = jetson.utils.cudaAllocMapped(width=img.width * resize_factor[0],
height=img.height * resize_factor[1],
format=img.format)

jetson.utils.cudaResize(img, resized_img)
return resized_img


# load the image
input_img = jetson.utils.loadImage(opt.file_in)

print('input image:')
print(input_img)

# convert to grayscale - other formats are:
# rgb8, rgba8, rgb32f, rgba32f, gray32f
gray_img = convert_color(input_img, "gray8")

print('grayscale image:')
print(gray_img)

# crop the image
crop_img = crop(gray_img, (0.75, 0.75))

print('cropped image:')
print(crop_img)

# resize the image
resized_img = resize(crop_img, (0.5, 0.5))

print('resized image:')
print(resized_img)

# save the image
if opt.file_out is not None:
jetson.utils.cudaDeviceSynchronize()
jetson.utils.saveImage(opt.file_out, resized_img)
print("saved {:d}x{:d} test image to '{:s}'".format(crop_img.width, crop_img.height, opt.file_out))

65 changes: 65 additions & 0 deletions python/examples/cuda-from-cv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/python3
#
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#

import cv2
import jetson.utils
import argparse


# parse the command line
parser = argparse.ArgumentParser(description='Convert an image from OpenCV to CUDA')

parser.add_argument("file_in", type=str, default="images/granny_smith_1.jpg", nargs='?', help="filename of the input image to process")
parser.add_argument("file_out", type=str, default="cuda-from-cv.jpg", nargs='?', help="filename of the output image to save")

opt = parser.parse_args()


# load the image
cv_img = cv2.imread(opt.file_in)

print('OpenCV image size: ' + str(cv_img.shape))
print('OpenCV image type: ' + str(cv_img.dtype))

# convert to CUDA (cv2 images are numpy arrays, in BGR format)
bgr_img = jetson.utils.cudaFromNumpy(cv_img, isBGR=True)

print('BGR image: ')
print(bgr_img)

# convert from BGR -> RGB
rgb_img = jetson.utils.cudaAllocMapped(width=bgr_img.width,
height=bgr_img.height,
format='rgb8')

jetson.utils.cudaConvertColor(bgr_img, rgb_img)

print('RGB image: ')
print(rgb_img)

# save the image
if opt.file_out is not None:
jetson.utils.cudaDeviceSynchronize()
jetson.utils.saveImage(opt.file_out, rgb_img)
print("saved {:d}x{:d} test image to '{:s}'".format(rgb_img.width, rgb_img.height, opt.file_out))

2 changes: 1 addition & 1 deletion python/examples/cuda-from-numpy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
#
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
Expand Down
67 changes: 67 additions & 0 deletions python/examples/cuda-to-cv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/python3
#
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#

import cv2
import jetson.utils
import argparse


# parse the command line
parser = argparse.ArgumentParser(description='Convert an image from CUDA to OpenCV')

parser.add_argument("file_in", type=str, default="images/jellyfish.jpg", nargs='?', help="filename of the input image to process")
parser.add_argument("file_out", type=str, default="cuda-to-cv.jpg", nargs='?', help="filename of the output image to save")

opt = parser.parse_args()


# load the image into CUDA memory
rgb_img = jetson.utils.loadImage(opt.file_in)

print('RGB image: ')
print(rgb_img)

# convert to BGR, since that's what OpenCV expects
bgr_img = jetson.utils.cudaAllocMapped(width=rgb_img.width,
height=rgb_img.height,
format='bgr8')

jetson.utils.cudaConvertColor(rgb_img, bgr_img)

print('BGR image: ')
print(bgr_img)

# make sure the GPU is done work before we convert to cv2
jetson.utils.cudaDeviceSynchronize()

# convert to cv2 image (cv2 images are numpy arrays)
cv_img = jetson.utils.cudaToNumpy(bgr_img)

print('OpenCV image size: ' + str(cv_img.shape))
print('OpenCV image type: ' + str(cv_img.dtype))

# save the image
if opt.file_out is not None:
cv2.imwrite(opt.file_out, cv_img)
print("saved {:d}x{:d} test image to '{:s}'".format(bgr_img.width, bgr_img.height, opt.file_out))

12 changes: 5 additions & 7 deletions python/examples/cuda-to-numpy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python
#!/usr/bin/python3
#
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
Expand All @@ -23,25 +23,23 @@

import jetson.utils
import argparse
import ctypes
import numpy

# parse the command line
parser = argparse.ArgumentParser('Map CUDA to memory to numpy ndarray')

parser.add_argument("--width", type=int, default=4, help="width of the array (in float elements)")
parser.add_argument("--height", type=int, default=2, help="height of the array (in float elements)")
parser.add_argument("--depth", type=int, default=3, help="depth of the array (in float elements)")

opt = parser.parse_args()

# allocate cuda memory
cuda_mem = jetson.utils.cudaAllocMapped(opt.height * opt.width * opt.depth * ctypes.sizeof(ctypes.c_float))
print(cuda_mem)
cuda_img = jetson.utils.cudaAllocMapped(width=opt.width, height=opt.height, format='rgb32f')
print(cuda_img)

# create a numpy ndarray that references the CUDA memory
# it won't be copied, but uses the same memory underneath
array = jetson.utils.cudaToNumpy(cuda_mem, opt.width, opt.height, opt.depth)
array = jetson.utils.cudaToNumpy(cuda_img)

print("\ncudaToNumpy() array:")
print(type(array))
Expand Down
2 changes: 1 addition & 1 deletion python/examples/gl-display-test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
#
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
Expand Down

0 comments on commit 29f5896

Please sign in to comment.