mjpg_server.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
4 
5 from __future__ import print_function
6 import argparse
7 import cv2
8 import os
9 import rospy
10 import sys
11 import time
12 from BaseHTTPServer import BaseHTTPRequestHandler
13 from BaseHTTPServer import HTTPServer
14 from SocketServer import ThreadingMixIn
15 
16 
17 VIDEO_PATH = None
18 
19 
20 class MJPGStreamHandler(BaseHTTPRequestHandler):
21  html_suffix = ['.html']
22  mjpg_suffix = ['.mjpg', '.avi', '.mov']
23  capture = None
24  jpeg_quality = 80
25  loop_rate = 20.0
26 
27  def do_GET(self):
28  path, ext = os.path.splitext(self.path)
29  if ext in self.html_suffix + self.mjpg_suffix:
30  self.send_response(200)
31  if ext in self.html_suffix:
32  self.html_response()
33  elif ext in self.mjpg_suffix:
34  self.mjpg_response()
35  else:
36  self.send_response(404)
37 
38  def html_response(self):
39  self.send_header('Content-type', 'text/html')
40  self.end_headers()
41 
42  self.wfile.write('''
43  <html>
44  <head></head>
45  <body>
46  <img src="http://127.0.0.1:8080/camera.mjpg"/>
47  </body>
48  </html>''')
49 
50  def mjpg_response(self):
51  self.send_header(
52  'Content-type', 'multipart/x-mixed-replace; boundary=--jpgboundary')
53  self.end_headers()
54 
55  if self.capture is None:
56  self.capture = cv2.VideoCapture(VIDEO_PATH)
57 
58  nframes = self.capture.get(cv2.CAP_PROP_FRAME_COUNT)
59  iframe = 0
60 
61  while True:
62  try:
63  ok, img = self.capture.read()
64  if not ok:
65  continue
66  iframe += 1
67  if iframe > nframes:
68  iframe = 0
69  self.capture.open(VIDEO_PATH)
70  ok, img = cv2.imencode('.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, self.jpeg_quality])
71  if not ok:
72  continue
73  jpg = img.tostring()
74  self.wfile.write('--jpgboundary')
75  self.send_header('Content-type', 'image/jpeg')
76  self.send_header('Content-length', str(len(jpg)))
77  self.end_headers()
78  self.wfile.write(jpg)
79  time.sleep(1.0 / self.loop_rate)
80  except KeyboardInterrupt:
81  break
82 
83 
84 class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
85  pass
86 
87 
88 def main():
89  global VIDEO_PATH
90  p = argparse.ArgumentParser()
91  p.add_argument('video_path')
92  p.add_argument('--port', default=8080, type=int)
93 
94  if rospy.myargv() == sys.argv:
95  argv = sys.argv
96  loginfo = print
97  else:
98  rospy.init_node('mjpg_server')
99  argv = rospy.myargv()
100  loginfo = rospy.loginfo
101 
102 
103  args = p.parse_args(argv[1:])
104 
105  VIDEO_PATH = args.video_path
106  if not os.path.exists(VIDEO_PATH):
107  raise OSError('video not found: {}'.format(VIDEO_PATH))
108 
109  server = None
110  try:
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)))
116  finally:
117  if server:
118  server.socket.close()
119  loginfo('Server Ended')
120 
121 
122 if __name__ == '__main__':
123  main()
def main()
Definition: mjpg_server.py:88


video_stream_opencv
Author(s): Sammy Pfeiffer
autogenerated on Wed Jun 19 2019 19:58:35