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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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.VideoCapture("run.mp4")
  14. while True:
  15. ret, frame = cap.read()
  16. if frame == None:
  17. break
  18. image = cv2.imencode('.jpg', frame)
  19. stream = image.tobytes()
  20. # Write the length of the capture to the stream and flush to
  21. # ensure it actually gets sent
  22. connection.write(struct.pack('<L', stream.tell()))
  23. connection.flush()
  24. # Rewind the stream and send the image data over the wire
  25. stream.seek(0)
  26. connection.write(stream.read())
  27. # If we've been capturing for more than 30 seconds, quit
  28. if time.time() - start > 30:
  29. break
  30. # Reset the stream for the next capture
  31. stream.seek(0)
  32. stream.truncate()
  33. # Write a length of zero to the stream to signal we're done
  34. connection.write(struct.pack('<L', 0))
  35. finally:
  36. connection.close()
  37. client_socket.close()