Skip to content

Commit 52713b1

Browse files
committed
Initial Commit
1 parent ec60410 commit 52713b1

11 files changed

+1103
-1
lines changed

Examples/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
There are two example programs here. Both programs require OpenCV to be installed with GStreamer support enabled.
2+
Both of these examples were last tested with L4T 31.0.2, OpenCV 3.4.3
3+
4+
The first is a simple C++ program to view the onboard camera feed from the Jetson Dev Kit.
5+
6+
To compile gstreamer_view.cpp:
7+
8+
$ g++ -o gstreamer_view -Wall -std=c++11 gstreamer_view.cpp $(pkg-config --libs opencv)
9+
10+
11+
to run the program:
12+
13+
$ ./gstreamer_view
14+
15+
The second is a Python program that reads the onboard camera feed from the Jetson Dev Kit and does Canny Edge Detection.
16+
17+
To run the Canny detection demo (Python 2.7):
18+
19+
$ python cannyDetection.py
20+
21+
With Python 3.3:
22+
23+
$ python3 cannyDetection.py
24+
25+
With the Canny detection demo, use the less than (<) and greater than (>) to adjust the edge detection parameters.
26+
27+
## Notes
28+
29+
1. The gstreamer_view example is from Peter Moran:
30+
https://gist.github.com/peter-moran/742998d893cd013edf6d0c86cc86ff7f
31+
Note that the nvvidconv flip-method was changed to 0. Earlier versions of L4T used a flip method of 2.
32+
33+
2. For the Python examples, you will need to have the appropriate librariers installed. From the buildOpenCV scripts:
34+
35+
#### Python 2.7
36+
$ sudo apt-get install -y python-dev python-numpy python-py python-pytest
37+
#### Python 3.5
38+
$ sudo apt-get install -y python3-dev python3-numpy python3-py python3-pytest
39+
40+
41+
## License
42+
MIT License
43+
44+
Copyright (c) 2017-2018 Jetsonhacks
45+
46+
Permission is hereby granted, free of charge, to any person obtaining a copy
47+
of this software and associated documentation files (the "Software"), to deal
48+
in the Software without restriction, including without limitation the rights
49+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
50+
copies of the Software, and to permit persons to whom the Software is
51+
furnished to do so, subject to the following conditions:
52+
53+
The above copyright notice and this permission notice shall be included in all
54+
copies or substantial portions of the Software.
55+
56+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
62+
SOFTWARE.
63+
64+
gstreamer_view example Copyright (c) 2017 Peter Moran
65+

Examples/cannyDetection.py

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env python
2+
# MIT License
3+
4+
# Copyright (c) 2017-18 Jetsonhacks
5+
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
24+
import sys
25+
import argparse
26+
import cv2
27+
import numpy as np
28+
29+
def parse_cli_args():
30+
parser = argparse.ArgumentParser()
31+
parser.add_argument("--video_device", dest="video_device",
32+
help="Video device # of USB webcam (/dev/video?) [0]",
33+
default=0, type=int)
34+
arguments = parser.parse_args()
35+
return arguments
36+
37+
# Use a Jetson TX1 or TX2 camera in the Jetson AGX Xavier Camera Slot
38+
def open_onboard_camera():
39+
return cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720, format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw,format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
40+
41+
# Open an external usb camera /dev/videoX
42+
def open_camera_device(device_number):
43+
return cv2.VideoCapture(device_number)
44+
45+
def read_cam(video_capture):
46+
if video_capture.isOpened():
47+
windowName = "CannyDemo"
48+
cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
49+
cv2.resizeWindow(windowName,1280,720)
50+
cv2.moveWindow(windowName,0,0)
51+
cv2.setWindowTitle(windowName,"Canny Edge Detection")
52+
showWindow=3 # Show all stages
53+
showHelp = True
54+
font = cv2.FONT_HERSHEY_PLAIN
55+
helpText="'Esc' to Quit, '1' for Camera Feed, '2' for Canny Detection, '3' for All Stages. '4' to hide help"
56+
edgeThreshold=40
57+
showFullScreen = False
58+
while True:
59+
if cv2.getWindowProperty(windowName, 0) < 0: # Check to see if the user closed the window
60+
# This will fail if the user closed the window; Nasties get printed to the console
61+
break;
62+
ret_val, frame = video_capture.read();
63+
if frame is None:
64+
continue ;
65+
66+
hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
67+
blur=cv2.GaussianBlur(hsv,(7,7),1.5)
68+
edges=cv2.Canny(blur,0,edgeThreshold)
69+
if showWindow == 3: # Need to show the 4 stages
70+
# Composite the 2x2 window
71+
# Feed from the camera is RGB, the others gray
72+
# To composite, convert gray images to color.
73+
# All images must be of the same type to display in a window
74+
frameRs=cv2.resize(frame, (640,360))
75+
hsvRs=cv2.resize(hsv,(640,360))
76+
vidBuf = np.concatenate((frameRs, cv2.cvtColor(hsvRs,cv2.COLOR_GRAY2BGR)), axis=1)
77+
blurRs=cv2.resize(blur,(640,360))
78+
edgesRs=cv2.resize(edges,(640,360))
79+
vidBuf1 = np.concatenate( (cv2.cvtColor(blurRs,cv2.COLOR_GRAY2BGR),cv2.cvtColor(edgesRs,cv2.COLOR_GRAY2BGR)), axis=1)
80+
vidBuf = np.concatenate( (vidBuf, vidBuf1), axis=0)
81+
82+
if showWindow==1: # Show Camera Frame
83+
displayBuf = frame
84+
elif showWindow == 2: # Show Canny Edge Detection
85+
displayBuf = edges
86+
elif showWindow == 3: # Show All Stages
87+
displayBuf = vidBuf
88+
89+
if showHelp == True:
90+
cv2.putText(displayBuf, helpText, (11,20), font, 1.0, (32,32,32), 4, cv2.LINE_AA)
91+
cv2.putText(displayBuf, helpText, (10,20), font, 1.0, (240,240,240), 1, cv2.LINE_AA)
92+
cv2.imshow(windowName,displayBuf)
93+
key=cv2.waitKey(10)
94+
if key == 27: # Check for ESC key
95+
cv2.destroyAllWindows()
96+
break ;
97+
elif key==49: # 1 key, show frame
98+
cv2.setWindowTitle(windowName,"Camera Feed")
99+
showWindow=1
100+
elif key==50: # 2 key, show Canny
101+
cv2.setWindowTitle(windowName,"Canny Edge Detection")
102+
showWindow=2
103+
elif key==51: # 3 key, show Stages
104+
cv2.setWindowTitle(windowName,"Camera, Gray scale, Gaussian Blur, Canny Edge Detection")
105+
showWindow=3
106+
elif key==52: # 4 key, toggle help
107+
showHelp = not showHelp
108+
elif key==44: # , lower canny edge threshold
109+
edgeThreshold=max(0,edgeThreshold-1)
110+
print ('Canny Edge Threshold Maximum: ',edgeThreshold)
111+
elif key==46: # , raise canny edge threshold
112+
edgeThreshold=edgeThreshold+1
113+
print ('Canny Edge Threshold Maximum: ', edgeThreshold)
114+
elif key==74: # Toggle fullscreen; This is the F3 key on this particular keyboard
115+
# Toggle full screen mode
116+
if showFullScreen == False :
117+
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
118+
else:
119+
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_NORMAL)
120+
showFullScreen = not showFullScreen
121+
122+
else:
123+
print ("camera open failed")
124+
125+
126+
127+
if __name__ == '__main__':
128+
arguments = parse_cli_args()
129+
print("Called with args:")
130+
print(arguments)
131+
print("OpenCV version: {}".format(cv2.__version__))
132+
print("Device Number:",arguments.video_device)
133+
print cv2.getBuildInformation()
134+
if arguments.video_device==0:
135+
print("Using on-board camera")
136+
video_capture=open_onboard_camera()
137+
else:
138+
video_capture=open_camera_device(arguments.video_device)
139+
# Only do this on external cameras; onboard will cause camera not to read
140+
video_capture.set(cv2.CAP_PROP_FPS,30)
141+
read_cam(video_capture)
142+
video_capture.release()
143+
cv2.destroyAllWindows()

Examples/gstreamer_view.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Example code for displaying gstreamer video from the CSI port of the Nvidia Jetson in OpenCV.
3+
Created by Peter Moran on 7/29/17.
4+
https://gist.github.com/peter-moran/742998d893cd013edf6d0c86cc86ff7f
5+
*/
6+
7+
#include <opencv2/opencv.hpp>
8+
9+
std::string get_tegra_pipeline(int width, int height, int fps) {
10+
return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" + std::to_string(width) + ", height=(int)" +
11+
std::to_string(height) + ", format=(string)NV12, framerate=(fraction)" + std::to_string(fps) +
12+
"/1 ! nvvidconv flip-method=0 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
13+
}
14+
15+
int main() {
16+
// Options
17+
int WIDTH = 1920;
18+
int HEIGHT = 1080;
19+
int FPS = 30;
20+
21+
// Define the gstream pipeline
22+
std::string pipeline = get_tegra_pipeline(WIDTH, HEIGHT, FPS);
23+
std::cout << "Using pipeline: \n\t" << pipeline << "\n";
24+
25+
// Create OpenCV capture object, ensure it works.
26+
cv::VideoCapture cap(pipeline, cv::CAP_GSTREAMER);
27+
if (!cap.isOpened()) {
28+
std::cout << "Connection failed";
29+
return -1;
30+
}
31+
32+
// View video
33+
cv::Mat frame;
34+
while (1) {
35+
cap >> frame; // Get a new frame from camera
36+
37+
// Display frame
38+
imshow("Display window", frame);
39+
cv::waitKey(1); //needed to show frame
40+
}
41+
}

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Jetsonhacks
3+
Copyright (c) 2017-2018 Jetsonhacks
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+129
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,131 @@
11
# buildOpenCVXavier
22
Build and install OpenCV for the NVIDIA Jetson AGX Xavier
3+
4+
These scripts build OpenCV version 3.4 for the NVIDIA Jetson AGX Xavier Developer Kit.
5+
6+
OpenCV is a rich environment which can be configured in many different ways. You should configure OpenCV for your needs, by modifying the build file "buildOpenCV.sh". Note that selecting different options in OpenCV may also have additional library requirements which are not included in these scripts. Please read the notes below for other important points before installing.
7+
8+
The buildOpenCV script has two optional command line parameters:
9+
10+
<ul>
11+
<li>-s | --sourcedir Directory in which to place the opencv sources (default $HOME)</li>
12+
<li>-i | --installdir Directory in which to install opencv libraries (default /usr/local)</li>
13+
</ul>
14+
15+
To run the the build file:
16+
17+
$ ./buildOpenCV.sh -s &lt;file directory&gt;
18+
19+
This example will build OpenCV in the given file directory and install OpenCV in the /usr/local directory.
20+
21+
The folder ~/opencv and (optional) ~/opencv_extras contain the source, build and extra data files. If you wish to remove them after installation, a convenience script is provided:
22+
23+
$ ./removeOpenCVSources.sh -d &lt;file directory&gt;
24+
25+
where the &lt;file directory&gt; contains the OpenCV source.
26+
27+
The folder ~/opencv and ~/opencv_extras contain the source, build and extra data files. If you wish to remove them after installation, a convenience script is provided:
28+
29+
$ ./removeOpenCVSources.sh
30+
31+
<h3>Packaging</h3>
32+
An alternative build script, buildAndPackageOpenCV.sh , will build the OpenCV package as described above and the build .deb files using the standard OpenCV mechanism defined using the CPACK_BINARY_DEB=ON in the OpenCV Make file. See the script.
33+
34+
The buildAndPackageOpenCV script has two optional command line parameters:
35+
36+
<ul>
37+
<li>-s | --sourcedir Directory in which to place the opencv sources (default $HOME)</li>
38+
<li>-i | --installdir Directory in which to install opencv libraries (default /usr/local)</li>
39+
</ul>
40+
41+
To run the the build file:
42+
43+
$ ./buildAndPackageOpenCV.sh -s &lt;file directory&gt;
44+
45+
This example will build OpenCV in the given file directory and install OpenCV in the /usr/local directory.
46+
47+
The corresponding .deb files will be in the &lt;file directory&gt;/opencv/build directory in .deb file and compressed forms.
48+
49+
<h4>Installing .deb files</h4>
50+
51+
To install .deb files:
52+
53+
Switch to the directory where the .deb files are located. Then:
54+
55+
<blockquote>
56+
$ sudo dpkg -i OpenCV-&lt;OpenCV Version info&gt;-aarch64-libs.deb
57+
58+
<em>For example: $ sudo dpkg -i OpenCV-3.4.1-1-g75a2577-aarch64-libs.deb</em>
59+
60+
$ sudo apt-get install -f
61+
62+
$ sudo dpkg -i OpenCV-&lt;OpenCV Version info&gt;-aarch64-dev.deb
63+
64+
$ sudo dpkg -i OpenCV-&lt;OpenCV Version info&gt;-aarch64-python.deb </blockquote>
65+
66+
The libraries will be installed in /usr/lib
67+
68+
Binaries are in /usr/bin
69+
70+
opencv.pc is in /usr/lib/pkgconfig
71+
72+
<strong>Package Notes: </strong>
73+
<ul><li>The build process default installation is in /usr/local
74+
Note that the .deb file install into /usr</li>
75+
<li>After installation, the dpkg/apt name does not include version information, e.g. the name is opencv-libs</li>
76+
</ul>
77+
78+
## Notes
79+
There may be issues if different version of OpenCV are installed. JetPack normally installs OpenCV in the /usr folder. You will need to consider if this is appropriate for your application. It is important to realize that many packages may rely on OpenCV. The standard installation by JetPack places the OpenCV libraries in the /usr directory.
80+
81+
You may consider removing OpenCV installed by JetPack before performing this script installation:
82+
83+
$ sudo apt-get purge libopencv*
84+
85+
With this script release, the script now installs OpenCV in /usr/local. Earlier versions of this script installed in /usr. You may have to set your include and libraries and/or PYTHONPATH to point to the new version. See the Examples folder. Alternatively, you may want to change the script to install into the /usr directory.
86+
87+
The Jetson is an aarch64 machine, which means that the OpenCV configuration variable ENABLE_NEON is ignored. The compiler includes NEON support for all machines with aarch64 architecture.
88+
89+
These scripts rely on OpenCV finding the correct CUDA version, instead of setting it manually.
90+
91+
Special thanks to Daniel (Github user @dkoguciuk) for script cleanup.
92+
93+
94+
## References
95+
96+
Most of this information is derived from:
97+
98+
http://docs.opencv.org/3.2.0/d6/d15/tutorial_building_tegra_cuda.html
99+
100+
https://devtalk.nvidia.com/default/topic/965134/opencv-3-1-compilation-on-tx1-lets-collect-the-quot-definitive-quot-cmake-settings-/?offset=3
101+
102+
## Release Notes
103+
November 2018
104+
* Initial Release
105+
* L4T 31.0.2
106+
* CUDA 10
107+
* OpenCV 3.4.3
108+
109+
## License
110+
MIT License
111+
112+
Copyright (c) 2017-2018 Jetsonhacks
113+
114+
Permission is hereby granted, free of charge, to any person obtaining a copy
115+
of this software and associated documentation files (the "Software"), to deal
116+
in the Software without restriction, including without limitation the rights
117+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
118+
copies of the Software, and to permit persons to whom the Software is
119+
furnished to do so, subject to the following conditions:
120+
121+
The above copyright notice and this permission notice shall be included in all
122+
copies or substantial portions of the Software.
123+
124+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
125+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
126+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
127+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
128+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
129+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
130+
SOFTWARE.
131+

0 commit comments

Comments
 (0)