first working implementation

This commit is contained in:
Camryn Thomas 2025-01-31 19:42:29 -06:00
parent fde1f10719
commit 107f5116d5
Signed by: cptlobster
GPG Key ID: 33D607425C830B4C
3 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,43 @@
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()

22
common.py Normal file
View File

@ -0,0 +1,22 @@
import numpy as np
class Packet:
def __init__(self, x: int, y: int, array: np.ndarray):
self.x = x
self.y = y
self.array = array
def to_bytestr(self) -> bytes:
bytestr = b""
bytestr += self.x.to_bytes(length=4, signed = False)
bytestr += self.y.to_bytes(length=4, signed = False)
bytestr += self.array.tobytes()
return bytestr
def from_bytes(b: bytes) -> Packet:
x = int.from_bytes(b[0:4], signed = False)
y = int.from_bytes(b[4:8], signed = False)
array = np.frombuffer(b[8:], np.uint8).reshape(16, 16, 3)
return Packet(x, y, array)

View File

@ -0,0 +1,41 @@
import cv2
import socket
import numpy as np
from common import Packet, from_bytes
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
HEIGHT = 480
WIDTH = 640
frame = np.ndarray((HEIGHT, WIDTH, 3), dtype=np.uint8)
while True:
# break the array down into 16-bit chunks, then transmit them as UDP packets
for i in range(0, HEIGHT, 16):
for j in range(0, WIDTH, 16):
data, addr = sock.recvfrom(776) # buffer size is 768 bytes
print("received packet from", addr)
pkt = from_bytes(data)
x = pkt.x
y = pkt.y
arr = pkt.array
frame[y:y+16, x:x+16] = arr
# Display the resulting frame
cv2.imshow('Frame', frame)
# Break the loop if 'q' key is pressed
if cv2.waitKey(1) == ord('q'):
break
# Release the capture and close all windows
cv2.destroyAllWindows()