python - 如何在没有 cvbridge 的情况下在 ros 上使用 opencv 发布图像主题
问题描述
我试图使用 python 脚本发布实时视频。
#!/usr/bin/env python3
import rospy
from sensor_msgs import msg
import cv2
from sensor_msgs.msg import Image
def takes_data_from_camera():
rate = rospy.Rate(10)
vid = cv2.VideoCapture(0)
while not rospy.is_shutdown():
ret, frame = vid.read()
#video_bridge = bridge.cv2_to_imgmsg(frame, "passthrough")
pub.publish(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
vid.release()
cv2.destroyAllWindows()
break
if __name__ == '__main__':
rospy.init_node('Server',anonymous = True)
pub = rospy.Publisher('TestOps/Camera', Image, queue_size=10)
takes_data_from_camera()
但是当我尝试运行时出现以下错误
File "/home/akash-j/catkin_ws/src/test_package/src/py-server.py", line 19, in takes_data_from_camera
pub.publish(frame)
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 879, in publish
data = args_kwds_to_message(self.data_class, args, kwds)
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/msg.py", line 122, in args_kwds_to_message
return data_class(*args)
File "/opt/ros/noetic/lib/python3/dist-packages/sensor_msgs/msg/_Image.py", line 77, in __init__
super(Image, self).__init__(*args, **kwds)
File "/opt/ros/noetic/lib/python3/dist-packages/genpy/message.py", line 354, in __init__
raise TypeError('Invalid number of arguments, args should be %s' % str(self.__slots__) + ' args are' + str(args))
TypeError: Invalid number of arguments, args should be ['header', 'height', 'width', 'encoding', 'is_bigendian', 'step', 'data'] args are(array([[[209, 200, 193],
我不知道这里出了什么问题,我不想使用 cv bridge。并且代码在 cv bridge 上运行良好。我正在使用 ubuntu 20 和 ros noetic 和 python 3
解决方案
Image 消息类型需要下面的这种数据结构。
>>> from sensor_msgs.msg import Image
>>> Image.__slots__
['header', 'height', 'width', 'encoding', 'is_bigendian', 'step', 'data']
>>> Image._slot_types
['std_msgs/Header', 'uint32', 'uint32', 'string', 'uint8', 'uint32', 'uint8[]']
看起来只发布了视频“数据”帧。
pub.publish(frame)
希望这足以让您进一步排除故障
推荐阅读
- javascript - JavaScript 中 arguments 对象的重要性
- git - 使用 Git 了解远程是否有新更新以及要下载多少字节
- sonarqube - SonarQube - “类 '...' 无法与其原始源文件匹配。”
- google-places-api - Google Places API 自动完成 我们可以重复使用会话令牌吗?
- c++ - CMAKE_CXX_COMPILER_VERSION 指向旧的 GCC 版本
- java - 记录大字符串会导致 OutOfMemoryError
- ios - 如何在 Swift 中保存增量
- excel - Office 加载项 Excel VSTO office 365(桌面)支持
- vb.net - 表单加载时 DataGridView 不会自动出现
- javascript - 动态多表单:根据先前的选择值更改选择选项