Skip to content

Commit 1c9a3c7

Browse files
committed
first commit
0 parents  commit 1c9a3c7

18 files changed

+1204
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
### base on
2+
```
3+
https://github.com/jetsonhacks/buildOpenCVXavier.git
4+
5+
6+
commit ca885938823640505cfdca6d6a82f7949c161717 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD)
7+
Author: jetsonhacks <jim@jetsonhacks.com>
8+
Date: Wed Nov 7 14:21:18 2018 -0800
9+
10+
remove libjasper-dev install
11+
```
12+
13+
14+
### 编译so
15+
```
16+
# cd buildOpenCVXavier/
17+
# ./buildAndPackageOpenCV.sh
18+
```
19+
20+
### 打包 whl
21+
```
22+
# cd opencv-python
23+
# cp /root/opencv/build/lib/python3/cv2.cpython-36m-aarch64-linux-gnu.so cv2/cv2.cpython-36m-aarch64-linux-gnu.so
24+
25+
# python3 setup.py bdist_wheel
26+
27+
# ls dist/ -lh
28+
total 1.6M
29+
-rw-r--r-- 1 root root 1.6M Jun 13 01:20 opencv_python-3.4.3-cp36-cp36m-linux_aarch64.whl
30+
```

buildOpenCVXavier/Examples/README.md

Lines changed: 65 additions & 0 deletions
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+
Lines changed: 143 additions & 0 deletions
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()
Lines changed: 41 additions & 0 deletions
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+
}

buildOpenCVXavier/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017-2018 Jetsonhacks
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)