00001
00002
00003
00004
00005
00006
00007 import os, sys, string, time, getopt
00008 import urllib
00009
00010 import roslib; roslib.load_manifest('clearpath_sensors')
00011 import rospy
00012
00013 from sensor_msgs.msg import CompressedImage, CameraInfo
00014
00015 import threading
00016
00017
00018 class StreamThread(threading.Thread):
00019 def __init__(self, axis):
00020 threading.Thread.__init__(self)
00021
00022 self.axis = axis
00023 self.daemon = True
00024
00025 self.axis_frame_id = "axis_camera_optical_frame"
00026
00027 def run(self):
00028 while True:
00029 try:
00030 self.stream()
00031 except:
00032 import traceback
00033 traceback.print_exc()
00034 time.sleep(1)
00035
00036 def stream(self):
00037 url = 'http://%s/mjpg/video.mjpg' % self.axis.hostname
00038 url = url + "?resolultion=%dx%d" % (self.axis.width, self.axis.height)
00039 fp = urllib.urlopen(url)
00040
00041 while True:
00042 boundary = fp.readline()
00043
00044 header = {}
00045 while 1:
00046 line = fp.readline()
00047 if line == "\r\n": break
00048 line = line.strip()
00049
00050 parts = line.split(": ", 1)
00051 header[parts[0]] = parts[1]
00052
00053 content_length = int(header['Content-Length'])
00054
00055 img = fp.read(content_length)
00056 line = fp.readline()
00057
00058 msg = CompressedImage()
00059 msg.header.stamp = rospy.Time.now()
00060 msg.header.frame_id = self.axis_frame_id
00061 msg.format = "jpeg"
00062 msg.data = img
00063
00064 self.axis.pub.publish(msg)
00065
00066 """
00067 cimsg = CameraInfo()
00068 cimsg.header.stamp = msg.header.stamp
00069 cimsg.header.frame_id = self.axis_frame_id
00070 cimsg.width = self.axis.width
00071 cimsg.height = self.axis.height
00072
00073 # Adding the best calibration we have for these cameras
00074 cimsg.D = [-0.26129794156876202, 0.053510647147691104, -0.004329961180682111, 0.0002979023290858089, 0]
00075 cimsg.K = [259.79888071407669, 0.0, 332.0316187674498, 0.0, 258.00868558667878, 252.46066959143357, 0.0, 0.0, 1.0]
00076 cimsg.R = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
00077 cimsg.P = [259.79888071407669, 0.0, 332.0316187674498, 0.0, 0.0, 258.00868558667878, 252.46066959143357, 0.0, 0.0, 0.0, 1.0, 0.0]
00078
00079 self.axis.caminfo_pub.publish(cimsg)
00080 """
00081
00082 class Axis:
00083 def __init__(self, hostname, username, password, width, height):
00084 self.hostname = hostname
00085 self.username = username
00086 self.password = password
00087 self.width = width
00088 self.height = height
00089
00090 self.st = None
00091 self.pub = rospy.Publisher("compressed", CompressedImage, self)
00092 self.caminfo_pub = rospy.Publisher("camera_info", CameraInfo, self)
00093
00094 def peer_subscribe(self, topic_name, topic_publish, peer_publish):
00095
00096 if self.st is None:
00097 self.st = StreamThread(self)
00098 self.st.start()
00099
00100
00101 def main():
00102 rospy.init_node("axis")
00103
00104 arg_defaults = {
00105 'hostname': '192.168.0.90',
00106 'username': '',
00107 'password': '',
00108 'width': 640,
00109 'height': 480
00110 }
00111 args = {}
00112 for name in arg_defaults:
00113 args[name] = rospy.get_param(rospy.search_param(name), arg_defaults[name])
00114
00115 Axis(**args)
00116 rospy.spin()
00117
00118
00119 if __name__ == "__main__":
00120 main()