cv_bridge missing channel data?

asked 2019-04-05 05:58:25 -0500

kk2105 gravatar image

updated 2019-04-09 04:51:44 -0500

Hi,

I am working on a task where I need to get the Image data from a bag file and process it for further use. Below is the approach and issue I am facing. Kindly help me.

  1. Written a python script which subscribes to the Image topic.
  2. Converted the ros_msg to cv2 format by using bridge.imgmsg_to_cv2(data, desired_encoding="bgr8").
  3. The converted image needs to be directly fed into a model which does the AI related tasks.
  4. I am not able to see the channel when I try to feed the image data directly to the model. But the trained model needs channel as well.
  5. When I save the image to local disk and try to load, it works perfectly fine and I am able to see the channel as well.

Could you please let me know if I am missing anything here? Below is the code snippet.

Converting to CV2 and feeding into the model

def callback(data):
    bridge = CvBridge()
    test_image = bridge.imgmsg_to_cv2(data, desired_encoding="bgr8")
    print("Shape Before : {}".format(test_image.shape))    ---- (300, 300)
    test_image = np.expand_dims(test_image, axis = 0)  
    print("Shape After  : {}".format(test_image.shape))      ---- (1, 300, 300)

Saving to local disk and feeding to the model

def callback(data):
    bridge = CvBridge()
    test_image = bridge.imgmsg_to_cv2(data, desired_encoding="bgr8")
    cv2.imwrite("File.jpg", test_image)
    test_image = image.load_img(path = "/home/File.jpg", target_size = (300, 300))
    test_image = image.img_to_array(test_image)
    print("Shape Before : {}".format(test_image.shape))    ---- (300, 300, 3)
    test_image = np.expand_dims(test_image, axis = 0)  
    print("Shape After  : {}".format(test_image.shape))      ---- (1, 300, 300, 3)

Any help/updates on this?

Thank you, KK

edit retag flag offensive close merge delete