This commit is contained in:
Camryn Thomas 2025-01-31 20:38:27 -06:00
parent 50128ac4a6
commit 275e5eec3e
Signed by: cptlobster
GPG Key ID: 33D607425C830B4C
3 changed files with 38 additions and 20 deletions

View File

@ -1,6 +1,9 @@
import cv2 import cv2
import socket import socket
import numpy as np import numpy as np
import uuid
uuid = uuid.uuid4()
from common import StdPacket, InterlacedPacket from common import StdPacket, InterlacedPacket
@ -26,7 +29,7 @@ def breakdown_image_norm(frame):
for i in range(0, cols, 16): for i in range(0, cols, 16):
for j in range(0, rows, 16): for j in range(0, rows, 16):
# print("Sending frame segment (%d, %d)", i, j) # print("Sending frame segment (%d, %d)", i, j)
pkt = StdPacket(j, i, frame[i:i + 16, j:j + 16]) pkt = StdPacket(uuid, j, i, frame[i:i + 16, j:j + 16])
send_packet(sock, pkt.to_bytestr()) send_packet(sock, pkt.to_bytestr())
def breakdown_image_interlaced(frame): def breakdown_image_interlaced(frame):
@ -36,13 +39,13 @@ def breakdown_image_interlaced(frame):
for i in range(0, cols, 32): for i in range(0, cols, 32):
for j in range(0, rows, 16): for j in range(0, rows, 16):
# print("Sending frame segment (%d, %d)", i, j) # print("Sending frame segment (%d, %d)", i, j)
pkt = InterlacedPacket(j, i, False, frame[i:i + 32:2, j:j + 16]) pkt = InterlacedPacket(uuid, j, i, False, frame[i:i + 32:2, j:j + 16])
send_packet(sock, pkt.to_bytestr()) send_packet(sock, pkt.to_bytestr())
for i in range(0, cols, 32): for i in range(0, cols, 32):
for j in range(0, rows, 16): for j in range(0, rows, 16):
# print("Sending frame segment (%d, %d)", i, j) # print("Sending frame segment (%d, %d)", i, j)
pkt = InterlacedPacket(j, i, True, frame[i + 1:i + 32:2, j:j + 16]) pkt = InterlacedPacket(uuid, j, i, True, frame[i + 1:i + 32:2, j:j + 16])
send_packet(sock, pkt.to_bytestr()) send_packet(sock, pkt.to_bytestr())
while True: while True:

View File

@ -1,29 +1,35 @@
import numpy as np import numpy as np
from uuid import UUID
class StdPacket: class StdPacket:
def __init__(self, x: int, y: int, array: np.ndarray): size = 16 + 4 + 4 + 768
def __init__(self, uuid: UUID, x: int, y: int, array: np.ndarray):
self.uuid = uuid
self.x = x self.x = x
self.y = y self.y = y
self.array = array self.array = array
def to_bytestr(self) -> bytes: def to_bytestr(self) -> bytes:
bytestr = b"" bytestr = b""
bytestr += self.uuid.bytes
bytestr += self.x.to_bytes(length=4, signed = False) bytestr += self.x.to_bytes(length=4, signed = False)
bytestr += self.y.to_bytes(length=4, signed = False) bytestr += self.y.to_bytes(length=4, signed = False)
bytestr += self.array.tobytes() bytestr += self.array.tobytes()
return bytestr return bytestr
def from_bytes_std(b: bytes) -> StdPacket: def from_bytes_std(b: bytes) -> StdPacket:
x = int.from_bytes(b[0:4], signed = False) uuid = UUID(bytes = b[0:16])
y = int.from_bytes(b[4:8], signed = False) x = int.from_bytes(b[16:20], signed = False)
array = np.frombuffer(b[8:], np.uint8).reshape(16, 16, 3) y = int.from_bytes(b[20:24], signed = False)
array = np.frombuffer(b[24:], np.uint8).reshape(16, 16, 3)
return StdPacket(x, y, array) return StdPacket(uuid, x, y, array)
class InterlacedPacket: class InterlacedPacket:
def __init__(self, x: int, y: int, even: bool, array: np.ndarray): size = 16 + 4 + 4 + 4 + 768
def __init__(self, uuid: UUID, x: int, y: int, even: bool, array: np.ndarray):
self.uuid = uuid
self.x = x self.x = x
self.y = y self.y = y
self.even = even self.even = even
@ -31,6 +37,7 @@ class InterlacedPacket:
def to_bytestr(self) -> bytes: def to_bytestr(self) -> bytes:
bytestr = b"" bytestr = b""
bytestr += self.uuid.bytes
bytestr += self.x.to_bytes(length=4, signed=False) bytestr += self.x.to_bytes(length=4, signed=False)
bytestr += self.y.to_bytes(length=4, signed=False) bytestr += self.y.to_bytes(length=4, signed=False)
bytestr += self.even.to_bytes(length=4) bytestr += self.even.to_bytes(length=4)
@ -39,9 +46,10 @@ class InterlacedPacket:
def from_bytes_int(b: bytes) -> InterlacedPacket: def from_bytes_int(b: bytes) -> InterlacedPacket:
x = int.from_bytes(b[0:4], signed=False) uuid = UUID(bytes=b[0:16])
y = int.from_bytes(b[4:8], signed=False) x = int.from_bytes(b[16:20], signed=False)
even = bool.from_bytes(b[8:12]) y = int.from_bytes(b[20:24], signed=False)
array = np.frombuffer(b[12:], np.uint8).reshape(16, 16, 3) even = bool.from_bytes(b[24:28])
array = np.frombuffer(b[28:], np.uint8).reshape(16, 16, 3)
return InterlacedPacket(x, y, even, array) return InterlacedPacket(uuid, x, y, even, array)

View File

@ -1,3 +1,5 @@
from typing import Dict
import cv2 import cv2
import socket import socket
import numpy as np import numpy as np
@ -15,27 +17,32 @@ sock.bind((UDP_IP, UDP_PORT))
HEIGHT = 480 HEIGHT = 480
WIDTH = 640 WIDTH = 640
frame = np.ndarray((HEIGHT, WIDTH, 3), dtype=np.uint8) frames: Dict[str, np.ndarray] = {}
while True: while True:
# break the array down into 16-bit chunks, then transmit them as UDP packets # break the array down into 16-bit chunks, then transmit them as UDP packets
for i in range(0, HEIGHT, 16): for i in range(0, HEIGHT, 16):
for j in range(0, WIDTH, 16): for j in range(0, WIDTH, 16):
data, addr = sock.recvfrom(780) # buffer size is 768 bytes data, addr = sock.recvfrom(InterlacedPacket.size) # buffer size is 768 bytes
# print("received packet from", addr) # print("received packet from", addr)
pkt = from_bytes_int(data) pkt = from_bytes_int(data)
uuid = str(pkt.uuid)
x = pkt.x x = pkt.x
y = pkt.y y = pkt.y
arr = pkt.array arr = pkt.array
if uuid not in frames.keys():
frames[uuid] = frame = np.ndarray((HEIGHT, WIDTH, 3), dtype=np.uint8)
if pkt.even: if pkt.even:
frame[y+1:y+32:2, x:x+16] = arr frames[uuid][y+1:y+32:2, x:x+16] = arr
else: else:
frame[y:y + 32:2, x:x + 16] = arr frames[uuid][y:y + 32:2, x:x + 16] = arr
# Display the resulting frame # Display the resulting frame
cv2.imshow('Frame', frame) for id in frames.keys():
cv2.imshow(id, frames[id])
# Break the loop if 'q' key is pressed # Break the loop if 'q' key is pressed
if cv2.waitKey(1) == ord('q'): if cv2.waitKey(1) == ord('q'):