43 lines
1.1 KiB
Python

import cv2
import socket
import numpy as np
from common import Packet
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
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
(cols, rows, colors) = frame.shape
# break the array down into 16-bit 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 = Packet(j, i, frame[i:i+16, j:j+16])
send_packet(sock, pkt.to_bytestr())
# Release the capture and close all windows
cap.release()