udp_client.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import cv2
  2. import socket
  3. import math
  4. import pickle
  5. import sys
  6. #max_length = 65000
  7. max_length = 65000
  8. host ="192.168.50.57"
  9. port = 5000
  10. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  11. #cap = cv2.VideoCapture('rtsp://admin:admin@192.168.50.182/av2_0')
  12. #cap = cv2.VideoCapture('rtsp://admin:abcd1234@192.168.51.48/av2_0')
  13. #cap = cv2.VideoCapture('rtsp://admin:admin@192.168.51.101/av2_0')
  14. cap = cv2.VideoCapture(1)
  15. ret, frame = cap.read()
  16. while ret:
  17. # compress frame
  18. frame = cv2.resize(frame, (240,180), interpolation=cv2.INTER_AREA)
  19. retval, buffer = cv2.imencode(".jpg", frame)
  20. if retval:
  21. # convert to byte array
  22. buffer = buffer.tobytes()
  23. # get size of the frame
  24. buffer_size = len(buffer)
  25. num_of_packs = 1
  26. if buffer_size > max_length:
  27. num_of_packs = math.ceil(buffer_size/max_length)
  28. frame_info = {"packs":num_of_packs}
  29. # send the number of packs to be expected
  30. print("Number of packs:", num_of_packs)
  31. sock.sendto(pickle.dumps(frame_info), (host, port))
  32. left = 0
  33. right = max_length
  34. for i in range(num_of_packs):
  35. print("left:", left)
  36. print("right:", right)
  37. # truncate data to send
  38. data = buffer[left:right]
  39. left = right
  40. right += max_length
  41. # send the frames accordingly
  42. sock.sendto(data, (host, port))
  43. ret, frame = cap.read()
  44. print("done")