Advertisement
Guest User

Untitled

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