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(1)
  16. ret, frame = cap.read()
  17. print("start")
  18. while ret:
  19. # compress frame
  20. #time.sleep(0.1)
  21. frame = cv2.resize(frame,(240,180), interpolation=cv2.INTER_AREA)
  22. retval, buffer = cv2.imencode(".jpg", frame)
  23. if retval:
  24. # convert to byte array
  25. buffer = buffer.tobytes()
  26. # get size of the frame
  27. buffer_size = len(buffer)
  28. num_of_packs = 1
  29. if buffer_size > max_length:
  30. num_of_packs = math.ceil(buffer_size/max_length)
  31. frame_info = {"packs":num_of_packs}
  32. # send the number of packs to be expected
  33. #print("Number of packs:", num_of_packs)
  34. sock.sendto(pickle.dumps(frame_info), (host, port))
  35. left = 0
  36. right = max_length
  37. for i in range(num_of_packs):
  38. #print("left:", left)
  39. #print("right:", right)
  40. # truncate data to send
  41. data = buffer[left:right]
  42. left = right
  43. right += max_length
  44. # send the frames accordingly
  45. sock.sendto(data, (host, port))
  46. ret, frame = cap.read()
  47. print("done")