-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamServer.py
39 lines (29 loc) · 911 Bytes
/
streamServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import cv2
import socket
import pickle
import struct
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("0.0.0.0", 9999))
server_socket.listen(1)
print("Waiting for a connection...")
client_socket, client_address = server_socket.accept()
print("Connected to:", client_address)
data = b""
payload_size = struct.calcsize("L")
while True:
while len(data) < payload_size:
data += client_socket.recv(4096)
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("L", packed_msg_size)[0]
while len(data) < msg_size:
data += client_socket.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data)
cv2.imshow("Server", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
client_socket.close()
cv2.destroyAllWindows()
server_socket.close()