static_virtual_camera.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import cv2
4 import numpy as np
5 try:
6  from scipy.misc import face
7  img = face()[:, :, ::-1]
8 except ImportError:
9  from scipy.misc import lena
10  img = cv2.cvtColor(lena().astype(np.uint8), cv2.COLOR_GRAY2BGR)
11 
12 import cv_bridge
13 import rospy
14 from sensor_msgs.msg import Image, CompressedImage
15 from sensor_msgs.msg import CameraInfo
16 
17 
18 def timer_cb(event):
19  imgmsg.header.stamp = rospy.Time.now()
20  info_msg.header.stamp = imgmsg.header.stamp
21  if pub_img.get_num_connections() > 0:
22  pub_img.publish(imgmsg)
23  if pub_img_compressed.get_num_connections() > 0:
24  pub_img_compressed.publish(compmsg)
25  pub_info.publish(info_msg)
26 
27 
28 if __name__ == '__main__':
29  rospy.init_node('static_virtual_camera')
30 
31  # get image_trasport before ConnectionBasedTransport subscribes ~input
32  transport_hint = rospy.get_param('~image_transport', 'raw')
33  rospy.loginfo("Using transport {}".format(transport_hint))
34 
35  pub_img = rospy.Publisher('~image_color', Image, queue_size=1)
36  pub_img_compressed = rospy.Publisher('~image_color/compressed', CompressedImage, queue_size=1)
37  pub_info = rospy.Publisher('~camera_info', CameraInfo, queue_size=1)
38 
39  bridge = cv_bridge.CvBridge()
40  imgmsg = bridge.cv2_to_imgmsg(img, encoding='bgr8')
41  imgmsg.header.frame_id = 'static_virtual_camera'
42  compmsg = CompressedImage()
43  compmsg.header = imgmsg.header
44  compmsg.format = "jpeg"
45  compmsg.data = np.array(cv2.imencode('.jpg', cv2.cvtColor(img, cv2.COLOR_BGR2RGB))[1]).tostring()
46 
47  info_msg = CameraInfo()
48  info_msg.header.frame_id = imgmsg.header.frame_id
49  height, width = img.shape[:2]
50  info_msg.height = height
51  info_msg.width = width
52 
53  rospy.Timer(rospy.Duration(0.1), timer_cb)
54  rospy.spin()


jsk_recognition_utils
Author(s):
autogenerated on Wed Mar 3 2021 04:00:53