12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import cv2
- import socket
- import math
- import pickle
- import sys
- #max_length = 65000
- max_length = 65000
- host ="192.168.50.57"
- port = 5000
- 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:admin@192.168.51.101/av2_0')
- cap = cv2.VideoCapture(1)
- ret, frame = cap.read()
- while ret:
- # compress frame
- 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")
|