Advertisement
Guest User

Untitled

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