-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_streaming_client_host.py
51 lines (42 loc) · 1.32 KB
/
tcp_streaming_client_host.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
40
41
42
43
44
45
46
47
48
49
50
51
# coding=utf-8
import re
import socket
import cv2
import numpy as np
def get_frame(socket, size):
bytes = socket.recv(4096)
while True:
read = 4096
if size - len(bytes) < read:
read = size - len(bytes)
bytes += socket.recv(read)
if size == len(bytes):
return bytes
def cli():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 5000))
server.listen()
print("connect")
print("Waiting for connection")
connection, client = server.accept()
try:
print("Connected to client IP: {}".format(client))
while True:
header = str(connection.recv(32), encoding="ascii")
chunks = re.split(" +", header)
if chunks[0] == "ABCDE":
# print(f">{header}<")
ts = float(chunks[1])
imgSize = int(chunks[2])
img = get_frame(connection, imgSize)
buf = np.frombuffer(img, dtype=np.byte)
# print(buf.shape, buf.size)
frame = cv2.imdecode(buf, cv2.IMREAD_COLOR)
cv2.imshow("color", frame)
if cv2.waitKey(1) == ord("q"):
break
except Exception as e:
print("Error:", e)
server.close()
if __name__ == "__main__":
cli()