working
This commit is contained in:
62
receiver.py
Normal file
62
receiver.py
Normal file
@ -0,0 +1,62 @@
|
||||
import socket
|
||||
import struct
|
||||
import numpy as np
|
||||
import cv2
|
||||
import time
|
||||
|
||||
HOST = '0.0.0.0'
|
||||
PORT = 9999
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.bind((HOST, PORT))
|
||||
sock.listen(1)
|
||||
conn, addr = sock.accept()
|
||||
print(f"Connected by {addr}")
|
||||
|
||||
total_bytes_received = 0
|
||||
start_time = time.time()
|
||||
|
||||
|
||||
def recvall(sock, n):
|
||||
data = b''
|
||||
while len(data) < n:
|
||||
packet = sock.recv(n - len(data))
|
||||
if not packet:
|
||||
return None
|
||||
data += packet
|
||||
return data
|
||||
|
||||
|
||||
try:
|
||||
while True:
|
||||
# Odbiór długości
|
||||
packed_len = recvall(conn, 4)
|
||||
if not packed_len:
|
||||
break
|
||||
length = struct.unpack('!I', packed_len)[0]
|
||||
|
||||
# Odbiór danych
|
||||
data = recvall(conn, length)
|
||||
if not data:
|
||||
break
|
||||
|
||||
total_bytes_received += length
|
||||
|
||||
# Dekodowanie JPEG
|
||||
img_array = np.frombuffer(data, dtype=np.uint8)
|
||||
frame = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
||||
|
||||
cv2.imshow("Stream", frame)
|
||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
break
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
if elapsed >= 1.0:
|
||||
print(f"Download speed: {total_bytes_received * 8 / 1e6:.2f} Mbps")
|
||||
total_bytes_received = 0
|
||||
start_time = time.time()
|
||||
|
||||
finally:
|
||||
conn.close()
|
||||
sock.close()
|
||||
cv2.destroyAllWindows()
|
||||
Reference in New Issue
Block a user