import cv2 import socket import math import pickle import sys import time import datetime #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() x = datetime.datetime.now() t=x.minute*60+x.second y = 0 print(x) print(x.second) encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),75] while ret and (y-t)!=60: # compress frame #time.sleep(0.1) z = datetime.datetime.now() y = z.minute*60+z.second frame = cv2.resize(frame, (240,180), interpolation=cv2.INTER_AREA) retval, buffer = cv2.imencode(".jpg", frame,encode_param) 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() time.sleep(0.04) print("done") x = datetime.datetime.now() print(x)