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
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++.