60 lines
1.9 KiB
Python

import cv2
import socket
import numpy as np
from common import StdPacket, InterlacedPacket
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(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(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(j, i, True, frame[i + 1:i + 32:2, j:j + 16])
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_interlaced(frame)
# Release the capture and close all windows
cap.release()