cv_bridge returns error during cvimage to rosimage conversion

asked 2020-10-12 14:01:25 -0500

rephaxe gravatar image

Hello, I am using Eloquent and OpenCV 3.4.1.43 on Ubuntu 18.04. I acquire frames of an IP camera with OpenCV and then to need to publish it. I try to use cv_bridge for image conversion but I get an error which says "TypeError: Your input type is not a numpy array"

#!/usr/bin/env python3

import rclpy
from sensor_msgs.msg import Image
import cv2
from rclpy.node import Node
from cv_bridge import CvBridge, CvBridgeError
import numpy as np

class ImagePublisher(Node):
    def __init__(self):
        super().__init__('image_publisher')

        self.publisher_ = self.create_publisher(Image, 'camera_image', 100)
        self.cap = cv2.VideoCapture('http://192.168.0.25:8080/video')
        self.bridge = CvBridge
        self.test()

    def test(self):
        while True:
            ret, frame = self.cap.read()
            cv2.imshow("Capturing", frame)

            #if cv2.waitKey(1) & 0xFF == ord('q'):
            #    break

            if  isinstance(frame, (np.ndarray, np.generic)):
                print("\n\nTRUE\n\n")

            try:
                msg = self.bridge.cv2_to_imgmsg(frame, "bgr8")
            except CvBridgeError as e:
                print(e)

            self.publisher_.publish(msg)


def main():
    rclpy.init()

    image_publisher = ImagePublisher()

    try:
        rclpy.spin(image_publisher)
    except KeyboardInterrupt:
        pass

    image_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

When I check the variable "frame" with the function "isinstance()" like in the code, it returns true. However it return false, when the function "cv2_to_imgmsg" does it itself (as I see in source code).

edit retag flag offensive close merge delete

Comments

Can you print out the shape of the input array to verify that it is indeed a three channel image, not just an ndarray?

kscottz gravatar image kscottz  ( 2020-10-15 23:23:22 -0500 )edit

Sorry for the late feedback but thanks for the hint. However it is an 3 channel image. Later i found the mistake, I forgot the round parenthesises. self.bridge = CvBridge()

rephaxe gravatar image rephaxe  ( 2020-11-08 11:36:22 -0500 )edit