Advertisement
Guest User

Untitled

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