import cv2 import socket import numpy as np import uuid uuid = uuid.uuid4() from common import StdPacket, InterlacedPacket, DoublyInterlacedPacket UDP_IP = "127.0.0.1" UDP_PORT = 5005 # Create a VideoCapture object cap = cv2.VideoCapture(0) # 0 represents the default camera # Check if camera opened successfully if not cap.isOpened(): print("Error opening video stream or file") def send_packet(sock, packet): sock.sendto(packet, (UDP_IP, UDP_PORT)) sock = socket.socket(socket.AF_INET, # Internet socket.SOCK_DGRAM) # UDP def breakdown_image_norm(frame): (cols, rows, colors) = frame.shape # break the array down into 16x16 chunks, then transmit them as UDP packets for i in range(0, cols, 16): for j in range(0, rows, 16): # print("Sending frame segment (%d, %d)", i, j) pkt = StdPacket(uuid, j, i, frame[i:i + 16, j:j + 16]) send_packet(sock, pkt.to_bytestr()) def breakdown_image_interlaced(frame): (cols, rows, colors) = frame.shape # break the array into 16x32 chunks. we'll split those further into odd and even rows # and send each as UDP packets. this should make packet loss less obvious for i in range(0, cols, 32): for j in range(0, rows, 16): # print("Sending frame segment (%d, %d)", i, j) pkt = InterlacedPacket(uuid, j, i, False, frame[i:i + 32:2, j:j + 16]) send_packet(sock, pkt.to_bytestr()) for i in range(0, cols, 32): for j in range(0, rows, 16): # print("Sending frame segment (%d, %d)", i, j) pkt = InterlacedPacket(uuid, j, i, True, frame[i + 1:i + 32:2, j:j + 16]) send_packet(sock, pkt.to_bytestr()) def breakdown_image_dint(frame): (cols, rows, colors) = frame.shape # break the array into 16x32 chunks. we'll split those further into odd and even rows # and send each as UDP packets. this should make packet loss less obvious for l in range(0, 4): for i in range(0, cols, 32): for j in range(0, rows, 32): # print("Sending frame segment (%d, %d)", i, j) i_even = l % 2 == 0 j_even = l >= 2 pkt = DoublyInterlacedPacket(uuid, j, i, j_even, i_even, frame[i + i_even:i + 32:2, j + j_even:j + 32:2]) send_packet(sock, pkt.to_bytestr()) while True: # Capture frame-by-frame ret, frame = cap.read() # If frame is read correctly, ret is True if not ret: print("Can't receive frame (stream end?). Exiting ...") break breakdown_image_dint(frame) # Release the capture and close all windows cap.release()