import cv2 import socket import math import pickle import sys import time #max_length = 65000 max_length = 65000 host = "60.250.156.230" port = 8000 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #cap = cv2.VideoCapture('rtsp://admin:admin@192.168.50.182/av2_0') #cap = cv2.VideoCapture('rtsp://admin:abcd1234@192.168.51.48/av2_0') #cap = cv2.VideoCapture('rtsp://admin:abcd1234@169.254.185.181/av2_0') cap = cv2.VideoCapture(0) ret, frame = cap.read() print("start") while ret: # compress frame #time.sleep(0.1) frame = cv2.resize(frame,(240,180), interpolation=cv2.INTER_AREA) retval, buffer = cv2.imencode(".jpg", frame) if retval: # convert to byte array buffer = buffer.tobytes() # get size of the frame buffer_size = len(buffer) num_of_packs = 1 if buffer_size > max_length: num_of_packs = math.ceil(buffer_size/max_length) frame_info = {"packs":num_of_packs} # send the number of packs to be expected #print("Number of packs:", num_of_packs) sock.sendto(pickle.dumps(frame_info), (host, port)) left = 0 right = max_length for i in range(num_of_packs): #print("left:", left) #print("right:", right) # truncate data to send data = buffer[left:right] left = right right += max_length # send the frames accordingly sock.sendto(data, (host, port)) ret, frame = cap.read() print("done")