diff --git a/client.py b/client.py index bf5a0bb..bc1a24e 100644 --- a/client.py +++ b/client.py @@ -11,32 +11,43 @@ from common import StdPacket, InterlacedPacket, DoublyInterlacedPacket, TiledIma def send_packet(sock, packet): sock.sendto(packet, (UDP_IP, UDP_PORT)) -def breakdown_image_norm(frame): +def breakdown_image_norm(frame, last_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()) + arr = frame[i:i + 16, j:j + 16] + last_arr = last_frame[i:i + 16, j:j + 16] + # only update if image segments are different + if not np.array_equal(arr, last_arr): + pkt = StdPacket(uuid, j, i, arr) + send_packet(sock, pkt.to_bytestr()) -def breakdown_image_interlaced(frame): +def breakdown_image_interlaced(frame, last_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()) + arr = frame[i:i + 32:2, j:j + 16] + last_arr = last_frame[i:i + 32:2, j:j + 16] + if not np.array_equal(arr, last_arr): + pkt = InterlacedPacket(uuid, j, i, False, arr) + 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()) + arr = frame[i + 1:i + 32:2, j:j + 16] + last_arr = last_frame[i + 1:i + 32:2, j:j + 16] + # only update if image segments are different + if not np.array_equal(arr, last_arr): + pkt = InterlacedPacket(uuid, j, i, True, arr) + send_packet(sock, pkt.to_bytestr()) -def breakdown_image_dint(frame): +def breakdown_image_dint(frame, last_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 @@ -46,8 +57,14 @@ def breakdown_image_dint(frame): # 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()) + + # breakdown image + arr = frame[i + i_even:i + 32:2, j + j_even:j + 32:2] + last_arr = last_frame[i + i_even:i + 32:2, j + j_even:j + 32:2] + # only update if image segments are different + if not np.array_equal(arr, last_arr): + pkt = DoublyInterlacedPacket(uuid, j, i, j_even, i_even, arr) + send_packet(sock, pkt.to_bytestr()) def breakdown_image_tiled(frame): (cols, rows, colors) = frame.shape @@ -93,7 +110,11 @@ if __name__ == '__main__': # create the socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + frame = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8) + last_frame = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8) + while True: + last_frame = frame.copy() # Capture frame-by-frame ret, frame = cap.read() @@ -102,7 +123,7 @@ if __name__ == '__main__': print("Can't receive frame (stream end?). Exiting ...") break - breakdown_image_dint(frame) + breakdown_image_dint(frame, last_frame) # Release the capture and close all windows cap.release() \ No newline at end of file