OpenCV and ROS melodic using python3
Hi,
I would like to subscribe to the camera topic of my robot and use this images in openCV. Unfortunatly, I need to use Python3 in melodic because of the robot, so using cv_bridge seems not to work. I found the following sourcecode as solution at another question, adapted it to my problem, but won't get it working.
Is someone keen to help me or is there another solution for this problem?
#!/usr/bin/env python3
import rospy
from sensor_msgs.msg import Image
import cv2 as cv
import numpy as np
from geometry_msgs.msg import Twist
import sys
def callback():
pass
def imgmsg_to_cv2(img_msg):
img_msg = Image()
if img_msg.encoding != "bgr8":
rospy.logerr("This Coral detect node has been hardcoded to the 'bgr8' encoding. Come change the code if you're actually trying to implement a new camera")
dtype = np.dtype("uint8") # Hardcode to 8 bits...
dtype = dtype.newbyteorder('>' if img_msg.is_bigendian else '<')
image_opencv = np.ndarray(shape=(img_msg.height, img_msg.width, 3), # and three channels of data. Since OpenCV works with bgr natively, we don't need to reorder the channels.
dtype=dtype, buffer=img_msg.data)
# If the byt order is different between the message and the system.
if img_msg.is_bigendian == (sys.byteorder == 'little'):
image_opencv = image_opencv.byteswap().newbyteorder()
cv.imshow("Video", image_opencv)
return image_opencv
cv_image = rospy.Subscriber("/robot/front_rgbd_camera/rgb/image_raw", Image, callback )
if __name__ =="__main__":
imgmsg_to_cv2(cv_image)