@@ -92,20 +92,28 @@ def convert_ros_img_to_cv2mat(msg: sensor_msgs.msg.Image) -> cv2.typing.MatLike:
92
92
return cv_image
93
93
94
94
95
- def convert_ros_img_to_base64 (msg : sensor_msgs .msg .Image ) -> str :
95
+ def convert_ros_img_to_base64 (
96
+ msg : sensor_msgs .msg .Image | sensor_msgs .msg .CompressedImage ,
97
+ ) -> str :
96
98
bridge = CvBridge ()
97
- cv_image = cast (cv2 .Mat , bridge .imgmsg_to_cv2 (msg , desired_encoding = "passthrough" )) # type: ignore
99
+ msg_type = type (msg )
100
+ if msg_type == sensor_msgs .msg .Image :
101
+ cv_image = bridge .imgmsg_to_cv2 (msg , desired_encoding = "passthrough" )
102
+ elif msg_type == sensor_msgs .msg .CompressedImage :
103
+ cv_image = bridge .compressed_imgmsg_to_cv2 (msg , desired_encoding = "passthrough" )
104
+ else :
105
+ raise ValueError (f"Unsupported message type: { msg_type } " )
106
+
98
107
if cv_image .shape [- 1 ] == 4 :
99
108
cv_image = cv2 .cvtColor (cv_image , cv2 .COLOR_BGRA2RGB )
100
109
return base64 .b64encode (bytes (cv2 .imencode (".png" , cv_image )[1 ])).decode (
101
110
"utf-8"
102
111
)
103
112
elif cv_image .shape [- 1 ] == 1 :
104
- cv_image = cv2 .cvtColor (cv_image , cv2 .GRAY2RGB )
113
+ cv_image = cv2 .cvtColor (cv_image , cv2 .COLOR_GRAY2RGB )
105
114
return base64 .b64encode (bytes (cv2 .imencode (".png" , cv_image )[1 ])).decode (
106
115
"utf-8"
107
116
)
108
-
109
117
else :
110
118
cv_image = cv2 .cvtColor (cv_image , cv2 .COLOR_BGR2RGB )
111
119
image_data = cv2 .imencode (".png" , cv_image )[1 ].tostring () # type: ignore
0 commit comments