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_cv2.py 1.6KB

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