Advertisement
Cpt_Jellyfish

EEEE4008: Object Manipulator Auxiliary Platform: Live Video Feed Code

May 13th, 2021
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. # Web streaming example
  2. # Source code from the official PiCamera package
  3. # http://picamera.readthedocs.io/en/latest/recipes2.html#web-streaming
  4.  
  5. import io
  6. import picamera
  7. import logging
  8. import socketserver
  9. from threading import Condition
  10. from http import server
  11.  
  12. PAGE="""\
  13. <html>
  14. <head>
  15. <title>Raspberry Pi - Surveillance Camera</title>
  16. </head>
  17. <body>
  18. <center><h1>Raspberry Pi - Object Manipulator</h1></center>
  19. <center><img src="stream.mjpg" width="640" height="480"></center>
  20. </body>
  21. </html>
  22. """
  23.  
  24. class StreamingOutput(object):
  25.     def __init__(self):
  26.         self.frame = None
  27.         self.buffer = io.BytesIO()
  28.         self.condition = Condition()
  29.  
  30.     def write(self, buf):
  31.         if buf.startswith(b'\xff\xd8'):
  32.             # New frame, copy the existing buffer's content and notify all
  33.             # clients it's available
  34.             self.buffer.truncate()
  35.             with self.condition:
  36.                 self.frame = self.buffer.getvalue()
  37.                 self.condition.notify_all()
  38.             self.buffer.seek(0)
  39.         return self.buffer.write(buf)
  40.  
  41. class StreamingHandler(server.BaseHTTPRequestHandler):
  42.     def do_GET(self):
  43.         if self.path == '/':
  44.             self.send_response(301)
  45.             self.send_header('Location', '/index.html')
  46.             self.end_headers()
  47.         elif self.path == '/index.html':
  48.             content = PAGE.encode('utf-8')
  49.             self.send_response(200)
  50.             self.send_header('Content-Type', 'text/html')
  51.             self.send_header('Content-Length', len(content))
  52.             self.end_headers()
  53.             self.wfile.write(content)
  54.         elif self.path == '/stream.mjpg':
  55.             self.send_response(200)
  56.             self.send_header('Age', 0)
  57.             self.send_header('Cache-Control', 'no-cache, private')
  58.             self.send_header('Pragma', 'no-cache')
  59.             self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
  60.             self.end_headers()
  61.             try:
  62.                 while True:
  63.                     with output.condition:
  64.                         output.condition.wait()
  65.                         frame = output.frame
  66.                     self.wfile.write(b'--FRAME\r\n')
  67.                     self.send_header('Content-Type', 'image/jpeg')
  68.                     self.send_header('Content-Length', len(frame))
  69.                     self.end_headers()
  70.                     self.wfile.write(frame)
  71.                     self.wfile.write(b'\r\n')
  72.             except Exception as e:
  73.                 logging.warning(
  74.                     'Removed streaming client %s: %s',
  75.                     self.client_address, str(e))
  76.         else:
  77.             self.send_error(404)
  78.             self.end_headers()
  79.  
  80. class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
  81.     allow_reuse_address = True
  82.     daemon_threads = True
  83.  
  84. with picamera.PiCamera(resolution='640x480', framerate=24) as camera:
  85.     output = StreamingOutput()
  86.     #Uncomment the next line to change your Pi's Camera rotation (in degrees)
  87.     camera.rotation = 270
  88.     camera.start_recording(output, format='mjpeg')
  89.     try:
  90.         address = ('', 8000)
  91.         server = StreamingServer(address, StreamingHandler)
  92.         server.serve_forever()
  93.     finally:
  94.         camera.stop_recording()
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement