ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

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

asked 2020-03-05 02:21:11 -0500

bamma gravatar image

updated 2022-01-22 16:10:24 -0500

Evgeny gravatar image

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

edit retag flag offensive close merge delete

Comments

I never used this functionality in opencv and i used not much rospy, but just in case check which versions of python is compatible this opencv functionality (i mean because if I am not wrong, rospy in ROS 1 uses python 2.7). If this is the problem, you should search a compatibility turnaround or use c++.

Solrac3589 gravatar image Solrac3589  ( 2020-03-05 06:08:17 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-04-03 05:06:13 -0500

goe gravatar image

You are using the built-in version of OpenCV. In the case of nvidia systems, the OpenCV version installed by default has not support for GStreamer or FFMpeg, so, you have to compile and install your own version of OpenCV with those capabilities (and dependencies) if you need to open camera streams.

You can use the OpenCV function getBuildInformation() to check if this is your case.

I suggest you to use this blog post for the compilation task: https://www.jetsonhacks.com/2018/11/0...

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-03-05 02:21:11 -0500

Seen: 8,150 times

Last updated: Mar 05 '20