Skip to content

Instantly share code, notes, and snippets.

@peter-moran
Last active June 9, 2022 18:44
Show Gist options
  • Save peter-moran/742998d893cd013edf6d0c86cc86ff7f to your computer and use it in GitHub Desktop.
Save peter-moran/742998d893cd013edf6d0c86cc86ff7f 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.
/*
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
}
}
@juansuerogit
Copy link

after adding declarations and command line args to build i was able to get the script to run but its not doing anything
im a newb

nvidia@tegra-ubuntu:~$ ./gstreamer_view
Using pipeline:
nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink

But nothing happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment