1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 import os
31 import cv2
32 import cv_bridge
33 import rospy
34
35 from sensor_msgs.msg import Image
36
37
39 """
40 Interface class for head display
41
42 Displays a given image file or multiple files on the robot's face.
43
44 Pass the relative or absolute file path to an image file on your
45 computer, and the example will read and convert the image using
46 cv_bridge, sending it to the screen as a standard ROS Image Message.
47
48 Notes:
49 Max screen resolution is 1024x600.
50 Images are always aligned to the top-left corner.
51 Image formats are those supported by OpenCv - LoadImage().
52 """
53
55 """
56 Constructor
57 """
58 self._image_pub = rospy.Publisher('/robot/head_display', Image, latch=True, queue_size=10)
59
61 """
62 Load the image located at the specified path
63
64 @type image_path: str
65 @param image_path: the relative or absolute file path to the image file
66
67 @rtype: sensor_msgs/Image or None
68 @param: Returns sensor_msgs/Image if image convertable and None otherwise
69 """
70 if not os.access(image_path, os.R_OK):
71 rospy.logerr("Cannot read file at '{0}'".format(image_path))
72 return None
73
74 img = cv2.imread(image_path)
75
76 return cv_bridge.CvBridge().cv2_to_imgmsg(img, encoding="bgr8")
77
78 - def display_image(self, image_path, display_in_loop=False, display_rate=1.0):
79 """
80 Displays image(s) to robot's head
81
82 @type image_path: list
83 @param image_path: the relative or absolute file path to the image file(s)
84
85 @type display_in_loop: bool
86 @param display_in_loop: Set True to loop through all image files infinitely
87
88 @type display_rate: float
89 @param display_rate: the rate to publish image into head
90 """
91 rospy.logdebug("Display images in loop:'{0}', frequency: '{1}'".format(display_in_loop, display_rate))
92
93 image_msg = []
94 image_list = image_path if isinstance(image_path, list) else [image_path]
95 for one_path in image_list:
96 cv_img = self._setup_image(one_path)
97 if cv_img:
98 image_msg.append(cv_img)
99
100 if not image_msg:
101 rospy.logerr("Image message list is empty")
102 else:
103 r = rospy.Rate(display_rate)
104 while not rospy.is_shutdown():
105 for one_msg in image_msg:
106 self._image_pub.publish(one_msg)
107 r.sleep()
108 if not display_in_loop:
109 break
110