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.

52 lines
1.1 KiB

  1. import socket
  2. import cv2
  3. import pickle
  4. import struct ## new
  5. HOST=''
  6. PORT=8485
  7. s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  8. print('Socket created')
  9. s.bind((HOST,PORT))
  10. print('Socket bind complete')
  11. s.listen(10)
  12. print('Socket now listening')
  13. data = b""
  14. payload_size = struct.calcsize(">L")
  15. print("payload_size: {}".format(payload_size))
  16. switch = True
  17. while True:
  18. while switch:
  19. conn,addr = s.accept()
  20. switch = False
  21. try:
  22. while len(data) < payload_size:
  23. print("Recv: {}".format(len(data)))
  24. data += conn.recv(4096)
  25. print("Done Recv: {}".format(len(data)))
  26. packed_msg_size = data[:payload_size]
  27. data = data[payload_size:]
  28. msg_size = struct.unpack(">L", packed_msg_size)[0]
  29. print("msg_size: {}".format(msg_size))
  30. while len(data) < msg_size:
  31. try:
  32. data += conn.recv(4096)
  33. except:
  34. pass
  35. frame_data = data[:msg_size]
  36. data = data[msg_size:]
  37. frame=pickle.loads(frame_data, fix_imports=True, encoding="bytes")
  38. frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
  39. cv2.imshow('ImageWindow',frame)
  40. ex_c = [27, ord("q"), ord("Q")]
  41. if cv2.waitKey(1) & 0xFF in ex_c:
  42. break
  43. except:
  44. switch=True