How to subscribe image topic and using opencv with webots

asked 2022-06-14 09:54:55 -0500

wangry gravatar image

I'm using webots to simulate in windows 10 and want to subscribe the camera in webots. I tried to use a call function to show the camera message in opencv, but found nothing to show. Main coding is like this:

    self.create_subscription(Image,'camera',self.__camera_callback, 10)

def __camera_callback(self, message):
    frame = message.Image
    cv2.imshow('camera', frame)

And I also tried to use cv_bridge to convert the Image flow into the format opencv supporting. Like this:

def __init__(self):
    super().__init__('cvcamera')
    self.create_subscription(Image,'camera',self.__camera_callback, 10)

def __camera_callback(self, data):
    try:
        frame = self.bridge.imgmsg_to_cv2(data,"bgr8")
    except CvBridgeError as e:
        print ("e")

    cv2.imshow("camera", frame)
    cv2.waitKey(1)

But I found the ros2 foxy in Windows 10 does not contain such package so that the cmd shows that ModuleNotFoundError: No module named 'cv_bridge'.

I tried to build it from source but the cmd showed build failed.

So how can I subscribe Image messages in webots and show the flow in opencv?

edit retag flag offensive close merge delete