udp_client.py 1.5 KB

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