udp_client.py 1.8 KB

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