-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
239 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
# | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
# | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
# | ||
|