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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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(('192.168.1.107', 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 is None:
  17. break
  18. print(type(frame))
  19. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  20. print(type(gray))
  21. _, image = cv2.imencode('.jpg', gray)
  22. print(type(image))
  23. stream = image.tobytes()
  24. print(type(stream))
  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()