Advertisement
Guest User

Jetson CUDA Code

a guest
Jun 29th, 2020
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cmath>
  4.  
  5.  
  6. #include <opencv2/core.hpp>
  7. #include <opencv2/highgui.hpp>
  8. #include <opencv2/imgproc.hpp>
  9. #include <opencv2/imgcodecs.hpp>
  10.  
  11. #include <opencv2/core/cuda.hpp>
  12. #include <opencv2/cudaimgproc.hpp>
  13. #include <opencv2/cudawarping.hpp>
  14. #include <opencv2/cudaarithm.hpp>
  15.  
  16.  
  17. std::string gstreamer_pipeline (int capture_width, int capture_height, int display_width, int display_height, int framerate, int flip_method) {
  18. return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" + std::to_string(capture_width) + ", height=(int)" +
  19.         std::to_string(capture_height) + ", format=(string)NV12, framerate=(fraction)" + std::to_string(framerate) +
  20.         "/1 ! nvvidconv flip-method=" + std::to_string(flip_method) + " ! video/x-raw, width=(int)" + std::to_string(display_width) + ", height=(int)" +
  21.         std::to_string(display_height) + ", format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
  22. }
  23.  
  24. int main()
  25. {
  26.     cv::cuda::printCudaDeviceInfo(cv::cuda::getDevice());
  27.  
  28.     int capture_width = 1280 ;
  29.     int capture_height = 720 ;
  30.     int display_width = 1280 ;
  31.     int display_height = 720 ;
  32.     int framerate = 60 ;
  33.     int flip_method = 2 ;
  34.  
  35.     std::string pipeline = gstreamer_pipeline(capture_width,
  36.     capture_height,
  37.     display_width,
  38.     display_height,
  39.     framerate,
  40.     flip_method);
  41.     std::cout << "Using pipeline: \n\t" << pipeline << "\n";
  42.  
  43.     cv::VideoCapture cap(pipeline, cv::CAP_GSTREAMER);
  44.     if(!cap.isOpened()) {
  45.     std::cout<<"Failed to open camera."<<std::endl;
  46.     return (-1);
  47.     }
  48.  
  49.     cv::namedWindow("CSI Camera", cv::WINDOW_AUTOSIZE);
  50.     cv::Mat img;
  51.  
  52.     std::cout << "Hit ESC to exit" << "\n" ;
  53.     while(true)
  54.     {
  55.         if (!cap.read(img)) {
  56.         std::cout<<"Capture read error"<<std::endl;
  57.         break;
  58.     }
  59.     cv::cuda::GpuMat display, smaller;
  60.     display.upload(img);
  61.  
  62.     cv::cuda::resize(display, smaller, cv::Size(1920,1080));
  63.    
  64.     display.download(img);
  65.    
  66.     cv::imshow("CSI Camera",img);
  67.     int keycode = cv::waitKey(5) & 0xff ;
  68.         if (keycode == 27) break ;
  69.     }
  70.  
  71.     cap.release();
  72.     cv::destroyAllWindows() ;
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement