5 from __future__
import print_function
12 from BaseHTTPServer
import BaseHTTPRequestHandler
13 from BaseHTTPServer
import HTTPServer
14 from SocketServer
import ThreadingMixIn
21 html_suffix = [
'.html']
22 mjpg_suffix = [
'.mjpg',
'.avi',
'.mov']
28 path, ext = os.path.splitext(self.path)
30 self.send_response(200)
36 self.send_response(404)
39 self.send_header(
'Content-type',
'text/html')
46 <img src="http://127.0.0.1:8080/camera.mjpg"/> 52 'Content-type',
'multipart/x-mixed-replace; boundary=--jpgboundary')
56 self.
capture = cv2.VideoCapture(VIDEO_PATH)
58 nframes = self.capture.get(cv2.CAP_PROP_FRAME_COUNT)
63 ok, img = self.capture.read()
69 self.capture.open(VIDEO_PATH)
70 ok, img = cv2.imencode(
'.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, self.
jpeg_quality])
74 self.wfile.write(
'--jpgboundary')
75 self.send_header(
'Content-type',
'image/jpeg')
76 self.send_header(
'Content-length', str(len(jpg)))
80 except KeyboardInterrupt:
90 p = argparse.ArgumentParser()
91 p.add_argument(
'video_path')
92 p.add_argument(
'--port', default=8080, type=int)
94 if rospy.myargv() == sys.argv:
98 rospy.init_node(
'mjpg_server')
100 loginfo = rospy.loginfo
103 args = p.parse_args(argv[1:])
105 VIDEO_PATH = args.video_path
106 if not os.path.exists(VIDEO_PATH):
107 raise OSError(
'video not found: {}'.format(VIDEO_PATH))
111 server = ThreadedHTTPServer((
'localhost', args.port), MJPGStreamHandler)
112 loginfo(
'Server Started')
113 server.serve_forever()
114 except Exception
as ex:
115 loginfo(
'Got exception: {}'.format(str(ex)))
118 server.socket.close()
119 loginfo(
'Server Ended')
122 if __name__ ==
'__main__':