-
-
Save andrewssobral/736f9d599bf5621cc379601a53817626 to your computer and use it in GitHub Desktop.
Bare-bones C++ script for viewing gstreamer video from the CSI port of the Nvidia Jetson TX2.
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
/* | |
Example code for displaying gstreamer video from the CSI port of the Nvidia Jetson in OpenCV. | |
Created by Peter Moran on 7/29/17. | |
https://gist.github.com/peter-moran/742998d893cd013edf6d0c86cc86ff7f | |
*/ | |
#include <opencv2/opencv.hpp> | |
std::string get_tegra_pipeline(int width, int height, int fps) { | |
return "nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)" + std::to_string(width) + ", height=(int)" + | |
std::to_string(height) + ", format=(string)I420, framerate=(fraction)" + std::to_string(fps) + | |
"/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink"; | |
} | |
int main() { | |
// Options | |
WIDTH = 1920; | |
HEIGHT = 1080; | |
FPS = 30; | |
// Define the gstream pipeline | |
std::string pipeline = get_tegra_pipeline(WIDTH, HEIGHT, FPS); | |
std::cout << "Using pipeline: \n\t" << pipeline << "\n"; | |
// Create OpenCV capture object, ensure it works. | |
cv::VideoCapture cap(pipeline, cv::CAP_GSTREAMER); | |
if (!cap.isOpened()) { | |
std::cout << "Connection failed"; | |
return -1; | |
} | |
// View video | |
cv::Mat frame; | |
while (1) { | |
cap >> frame; // Get a new frame from camera | |
// Display frame | |
imshow("Display window", frame); | |
cv::waitKey(1); //needed to show frame | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment