OpenCV Video Capture with GStreamer doesn't work on ROS-melodic

Hello. I am a beginner of ROS. I tried to stream video using OpenCV Video Capture and GStreamer on Ubuntu 18.04 LTS(NVIDIA Jetson TX2) and ROS-melodic. I wanted a node to publish image which is from cv2.VideoCapture with GStreamer pipeline, to a subscribing node and it show the image using cv2.imshow().

However, when I roslaunch the package, it doesn’t show the image on a window, just keep run with warning message:

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

I added ‘print()’ to check what the problem is and commented publishing part to check the captured image, but the string didn’t be printed not at all.

I attached the code of main node.

#!/usr/bin/env python
import rospy
from sensor_msgs.msg import Image
import sys
import cv2
import gi
import numpy as np
from cv_bridge import CvBridge
gi.require_version('Gst', '1.0')
from gi.repository import Gst
class Camera:
    def __init__(self):
        self.pub = rospy.Publisher("/camera_test/images", Image, queue_size=1)
        self.cvb = CvBridge()
    def read_cam(self):
        # cap = cv2.VideoCapture(0)
        cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
        if cap.isOpened():
            print("cap is opened but") # NOT PRINTED
            cv2.namedWindow("demo", cv2.WINDOW_AUTOSIZE)
            while not rospy.core.is_shutdown():
                ret_val, img = cap.read()
                print("img type is : ", type(img)) # NOT PRINTED
                cv2.imshow('demo', img) # NOT SHOW

                # send image to pub node using cvbridge
                # self.pub.publish(self.cvb.cv2_to_imgmsg(img, 'bgr8'))
                # cv2.imshow('demo',img)
                # if cv2.waitKey(1) == ord('q'):
                #     break
        else:
            print("Cap failed")
if __name__=="__main__":
    cam = Camera()
    rospy.init_node('camera')
    try:
        cam.read_cam()
        rospy.spin()
        outcome = 'Test Completed'
    except rospy.ROSInterruptException:
        print("Exception")
        pass
    rospy.core.signal_shutdown(outcome)

Help me, thank you:D

Hi,
Please run gst-launch-1.0 command to confirm the source is valid.

$ gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1' ! nvoverlaysink

Hi,
Once you confirm the source is valid, you may also try this simple code:

import sys
import cv2

def read_cam():
    cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert !  appsink")
    if cap.isOpened():
        cv2.namedWindow("demo", cv2.WINDOW_AUTOSIZE)
        while True:
            ret_val, img = cap.read();
            cv2.imshow('demo',img)
            cv2.waitKey(10)
    else:
     print "camera open failed"

    cv2.destroyAllWindows()


if __name__ == '__main__':
    read_cam()
1 Like

I have the same issue.

I have the raspberry pi camera v2.1 and jetson nano dev kit

if I run

gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1' ! nvoverlaysink

I get the video stream on the screen, but when I try to run the jetcam I get the error:

#!/usr/bin/python
#

import sys
import jetson.inference
import jetson.utils
import cv2
sys.path.append('/usr/local/lib/python3.6/dist-packages/jetcam-0.0.0-py3.6.egg')
from jetcam.csi_camera import CSICamera


# create the camera and display
camera = CSICamera(width=224, height=224, capture_width=1080, capture_height=720, capture_fps=30)
display = jetson.utils.glDisplay()

while display.IsOpen():
	img = camera.read()
	display.RenderOnce(img, 1080, 720)

it outputs:

jetson_2@jetson:~$ ./camera.py
jetson.inference.__init__.py
jetson.inference -- initializing Python 2.7 bindings...
jetson.inference -- registering module types...
jetson.inference -- done registering module types
jetson.inference -- done Python 2.7 binding initialization
jetson.utils.__init__.py
jetson.utils -- initializing Python 2.7 bindings...
jetson.utils -- registering module functions...
jetson.utils -- done registering module functions
jetson.utils -- registering module types...
jetson.utils -- done registering module types
jetson.utils -- done Python 2.7 binding initialization
GST_ARGUS: Creating output stream
CONSUMER: Waiting until producer is connected...
GST_ARGUS: Available Sensor modes :
GST_ARGUS: 3264 x 2464 FR = 21.000000 fps Duration = 47619048 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 3264 x 1848 FR = 28.000001 fps Duration = 35714284 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1920 x 1080 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1280 x 720 FR = 59.999999 fps Duration = 16666667 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1280 x 720 FR = 120.000005 fps Duration = 8333333 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: Running with following settings:
   Camera index = 0 
   Camera mode  = 4 
   Output Stream W = 1280 H = 720 
   seconds to Run    = 0 
   Frame Rate = 120.000005 
GST_ARGUS: PowerService: requested_clock_Hz=5103000
GST_ARGUS: Setup Complete, Starting captures for 0 seconds
GST_ARGUS: Starting repeat capture requests.
CONSUMER: Producer has connected; continuing.
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
jetson.utils -- PyDisplay_New()
jetson.utils -- PyDisplay_Init()
[OpenGL] glDisplay -- X screen 0 resolution:  1920x1080
[OpenGL] glDisplay -- display device initialized
Traceback (most recent call last):
  File "./camera.py", line 38, in <module>
    display.RenderOnce(img, 1080, 720)
Exception: jetson.utils -- glDisplay.RenderOnce() failed to get image pointer from PyCapsule container
GST_ARGUS: Cleaning up
CONSUMER: Done Success
GST_ARGUS: Done Success
jetson.utils -- PyDisplay_Dealloc()
GST_ARGUS: 
PowerServiceHwVic::cleanupResources

One more thing, I’m also trying to run this camera, but nvarguscamerasrc return “no camera available”:

https://www.amazon.com/Camera-Module-Raspberry-Supports-Compatible/dp/B073183KYK/ref=sr_1_5?keywords=raspberry+pi+infrared+camera+ov5647&qid=1584050083&sr=8-5

is it incopatible with jetson nano?

Hi ricardobittencourt,

Please open a new topic in Jetson Nano forum. Thanks