Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

video_client_pi.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import io
  2. import socket
  3. import struct
  4. import time
  5. import picamera
  6. # Connect a client socket to my_server:8000 (change my_server to the
  7. # hostname of your server)
  8. client_socket = socket.socket()
  9. client_socket.connect(('my_server', 8000))
  10. # Make a file-like object out of the connection
  11. connection = client_socket.makefile('wb')
  12. try:
  13. with picamera.PiCamera() as camera:
  14. camera.resolution = (640, 480)
  15. # Start a preview and let the camera warm up for 2 seconds
  16. camera.start_preview()
  17. time.sleep(2)
  18. # Note the start time and construct a stream to hold image data
  19. # temporarily (we could write it directly to connection but in this
  20. # case we want to find out the size of each capture first to keep
  21. # our protocol simple)
  22. start = time.time()
  23. stream = io.BytesIO()
  24. for foo in camera.capture_continuous(stream, 'jpeg'):
  25. # Write the length of the capture to the stream and flush to
  26. # ensure it actually gets sent
  27. connection.write(struct.pack('<L', stream.tell()))
  28. connection.flush()
  29. # Rewind the stream and send the image data over the wire
  30. stream.seek(0)
  31. connection.write(stream.read())
  32. # If we've been capturing for more than 30 seconds, quit
  33. if time.time() - start > 30:
  34. break
  35. # Reset the stream for the next capture
  36. stream.seek(0)
  37. stream.truncate()
  38. # Write a length of zero to the stream to signal we're done
  39. connection.write(struct.pack('<L', 0))
  40. finally:
  41. connection.close()
  42. client_socket.close()