capture video from usb2.0 camera with opencv3.2+gstreamer on TX2

https://developer.ridgerun.com/wiki/index.php?title=Gstreamer_pipelines_for_Jetson_TX2

https://devtalk.nvidia.com/default/topic/1025078/jetson-tx2/capture-video-from-usb2-0-camera-with-opencv3-2-gstreamer-on-tx2-/post/5214112/#5214112

Hi,everyone
I'm working on a project that needs to capture video from a usb2.0 camera on TX2. And my enviroment is Ubuntu L4T 28.1 on TX2,opencv3.2 compiled with gstreamer.
The output parameters of the USB2.0 Camera are 1920*1080, 30fps, encoding in mjpeg.
Following the instructions on "Jetson_TX2_Accelerated_GStreamer_User_Guide.pdf"(https://developer.nvidia.com/embedded/dlc/l4t-tx2-accelerated-gstreamer-guide-28-1),I can successfully watch the video using the follow gstreamer pipeline in terminal.

 
  1. gst-launch-1.0 v4l2src device="/dev/video0" ! "video/x-raw, width=(int)1920, height=(int)1080, format=(string)I420" ! nveglglessink
 
  1. gst-launch-1.0 v4l2src device="/dev/video0" ! nvjpegdec ! nveglglessink



According the pipeline, I wrote my code as shown below

 
  1. int main(int argc, char *argv[])
  2. {
  3. const char *videosrc = "v4l2src device=/dev/video0 ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)I420 ! videoconvert ! appsink";
  4. cv::VideoCapture cap;
  5. cap.open(videosrc);
  6. if (!m_cap.isOpened())
  7. {
  8. std::cout<<"OPEN CAM ERROR\n";
  9. return 1;
  10. }
  11.  
  12. cv::Mat nowMat;
  13. char ch;
  14. while (true)
  15. {
  16. if(!cap.read(nowMat))
  17. {
  18. break;
  19. }
  20.  
  21. //some code for processing the nowMat
  22.  
  23.  
  24. cv::imshow("Hello", nowMat);
  25. ch = cv::waitKey(1);
  26. if(ch == 'q')break;
  27. }
  28. cap.release();
  29. nowMat.release();
  30. return 0;
  31. }


Because my processing for nowMat is a little complex, the video is caton.And I saw CPUs on TX2 are high load, but GPU isn't high load.
So, is there any way to capture and decode the video stream via GPU in my project,such as modifing the string passed to cap.open().
And because my code for processing is based on opencv3.2, I don't want to rewrite my code on another framework.
Thanks

#1

Posted 10/13/2017 07:41 AM   

WayneWWW

Hi hzpbanana,

I would like suggest you to follow mmapi sample but you only want a openCV framework here.

#2

Posted 10/16/2017 08:41 AM   

hzpbanana

  

Hi WayneWWW,
The subject of my project is Face Detection, the code for processing the nowMatThe is for detection and is written by my partner.So I only want a OpenCV framework.

As you said, if I would like to follow mmapi samples, that means I will do my job in mmapi. Is there any way to transfer the frame data into Mat.data in OpenCV ?

PS.For processing the nowMat, I have already used multi-thread technique.
Thanks

#3

Posted 10/16/2017 09:32 AM   

WayneWWW

Hi hzpbanana,

According to your description and sample, I only see camera capture from v4l2src and does not have a decode process. nowMat is a cpu buffer and I don't know where you would like to improve through GPU.

Our mmapi sample reads the camera source and directly put it on a GPU memory for later processing.

#4

Posted 10/17/2017 02:11 AM   

hzpbanana

  

Hi WayneWWW,
Thank you for your suggestion on mmapi, I would prepare to work on it.

And what else, I have used an IP camera previously, and the string passed to videocapture.open() is shown below

 
  1. const char *videosrc = "rtspsrc location=rtsp://192.168.1.168/main latency=60 ! decodebin ! nvvidconv ! video/x-raw,format=(string)BGRx ! videoconvert ! video/x-raw,format=(string)BGR ! appsink";


Working on it, I have found that in this way, the GPU has a little load. I think it's because "nvvidconv" may do transcoding in GPU. And the video plays more fluent than the video via USB camera. What's your idea about it.

#5

Posted 10/17/2017 03:16 AM   

DaneLLL

 Answer Accepted by Forum Admin

You may also try openCV 3.3 to eliminate 'videoconvert'
https://devtalk.nvidia.com/default/topic/1024245/jetson-tx2/opencv-3-3-and-integrated-camera-problems-/post/5210735/#5210735

猜你喜欢

转载自blog.csdn.net/wlf_go/article/details/97892336