发布者订阅者模型重载的导入名称

时间:2016-11-06 15:18:22

标签: python ros rospy

所以我尝试使用发布图像的Python在ROS中创建一个程序,但是我导入的两个东西都被称为' Image'。当我运行程序时,我收到此错误消息。

  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 812, in __init__
    super(Publisher, self).__init__(name, data_class, Registration.PUB)
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 138, in __init__
    raise ValueError("data_class [%s] is not a class"%data_class) 
ValueError: data_class [<module 'PIL.Image' from '/usr/lib/python2.7/dist-packages/PIL/Image.pyc'>] is not a class

当我取出导入PIL Image和ImageFilter的行,初始化image1的行和行发布image1时,程序运行正常,因为(我认为)两个导入的Image对象之间没有混淆。有没有办法告诉程序区分两个图像?

#!/usr/bin/env python

import rospy
from sensor_msgs.msg import Image
from PIL import Image, ImageFilter

def camera():
    pub = rospy.Publisher('rgb', Image, queue_size=10)
    image1 = Image.open('dog.png')
    pub.publish(image1)
if __name__ == '__main__':
    try:
        camera()
    except rospy.ROSInterruptException:
        pass

1 个答案:

答案 0 :(得分:1)

您可以在导入期间重命名:

from sensor_msgs.msg import Image as ImageMsg

这样可以避免名称冲突。