【问题标题】:ROS Publisher NodeROS 发布者节点
【发布时间】:2021-09-13 00:20:20
【问题描述】:

我正在编写代码以将视频中的帧发布到 rostopic。在声明 pub 对象时,它给了我一个错误。

import rospy
import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import sys

class Video_Publisher:
    def __init__(self):
        self.pub = rospy.publisher('tennis_ball_image', Image , queue_size = 1)
        self.bridge= CvBridge()
    def framepublish(self):
        video = cv2.VideoCapture('video/tennis-ball-video.mp4')
        while not rospy.is_shutdown():
            ret, frame = video.read()
            #cv2.imshow("TennisVideo",frame)
            try:    
                ros_image = bridge.immsg_to_cv2(frame,"bgr8")
            except CvBridgeError as e:
                 print(e)
                 continue
            self.pub.publish(ros_image)
            self.rate.sleep

if __name__ == '__main__':
    rospy.init_node('tennis_ball_publisher',anonymous=True)
    frames = Video_Publisher()
    frames.framepublish('video/tennis-ball-video.mp4')

它给了我以下错误:

Traceback (most recent call last):
  File "tennis_ball_publisher.py", line 33, in <module>
    frames = Video_Publisher()
  File "tennis_ball_publisher.py", line 11, in __init__
    self.pub = rospy.publisher('tennis_ball_image', Image , queue_size = 1)
AttributeError: 'module' object has no attribute 'publisher'

【问题讨论】:

  • Python 区分大小写:应该是 Publisher 而不是 publisher。
  • 我试过这样做,但没有奏效!我再次使用 catkin build 使该错误消失了。但是现在我有一个关于 nparray 的新错误,如下所示:dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding) AttributeError: 'numpy.ndarray' object has no attribute 'encoding'
  • 我现在没有时间进行更详细的查看,但我认为您不想使用immsg_to_cv2,而是使用cv2_to_imgmsg,对吗?您的输入是cv2 图像,输出是未压缩的图像消息(注意还有一个压缩的!)。
  • 是的,明白了。如此愚蠢的错误,非常感谢!
  • 为了帮助减少未回答的 ros 问题的数量,帮助未来的访客,并奖励您一些代表,想要快速输入答案摘要吗?在这种情况下,似乎困难的错误消息是混合输入/功能的结果。 (除了出版商的错字,这有点明显)

标签: python opencv ros cv2 rospy


【解决方案1】:

Python 区分大小写:发布者 --> 发布者 和 cv2_to_imgmsg 代替 imgmsg_to_cv2 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2022-08-09
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多