def __init__(self, capture, topic, show_window=False, show_info=False, rate=32): self.show_window = show_window self.show_info = show_info self.videoCapture = capture self.topic = topic self.rate = rate self.bridge = CvBridge() self.KEY_ECS = 27 self.imgPlayer = ImagePlayer() self.mutex = threading.Lock() self.fps = FPS() self.msg = None self.digtal_recognizer = DigitalRegnition()
class VideoPub: def __init__(self, capture, topic, show_window=False, show_info=False, rate=32): self.show_window = show_window self.show_info = show_info self.videoCapture = capture self.topic = topic self.rate = rate self.bridge = CvBridge() self.KEY_ECS = 27 self.imgPlayer = ImagePlayer() self.mutex = threading.Lock() self.fps = FPS() self.msg = None self.digtal_recognizer = DigitalRegnition() def run(self): self.handle_pub() def handle_pub(self): rospy.init_node('video_publisher') pub = rospy.Publisher(topic, Image, queue_size=2) self.handle_threading() count = 0 self.fps.start() rate = rospy.Rate(self.rate) while not rospy.is_shutdown(): success, img = self.videoCapture.read() if not success: break img_recognized = self.digtal_recognizer.recognize(img) try: msg = self.bridge.cv2_to_imgmsg(img_recognized, "bgr8") except CvBridgeError as e: print(e) rospy.signal_shutdown(e) count += 1 self.fps.update() self.msg = self.add_timestamp(msg, count) pub.publish(self.msg) self.show_image(img_recognized) rate.sleep() # end wile # .... def handle_threading(self): threading.Thread(target=self.exit_key_event).start() if self.show_info: threading.Thread(target=self.show_data_info).start() def exit_key_event(self): while not rospy.is_shutdown(): with self.mutex: ch = getch() if len(ch) > 0 and (ch == 'q' or ord(ch) == imtools.KEY_CTRL_C): print 'user press crtl + c or q to exit' rospy.signal_shutdown('user press crtl + c or q to exit') break def add_timestamp(self, msg, count): now = rospy.Time.now() msg.header.frame_id = get_hostname() + '-' + str(count) msg.header.stamp.secs = now.secs msg.header.stamp.nsecs = now.nsecs return msg def show_image(self, img): if self.show_window: self.imgPlayer.show(img) key = self.imgPlayer.get_key() if 0xFF & key == self.KEY_ECS: rospy.signal_shutdown('user press ESC to exit') def show_data_info(self): import utility import time while not rospy.is_shutdown(): time.sleep(.5) with self.mutex: utility.show_msg_info(self.msg) print('fps: %s' % round(self.fps, 2)) print '%s' % (quit_msg) def cleanup(self): self.videoCapture.release() cv2.destroyAllWindows()