-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
58 lines (39 loc) · 1.34 KB
/
server.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
52
53
54
55
56
57
58
import numpy as np
import cv2
import socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('0.0.0.0', 7777)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
def applyImage(data):
decoded = np.frombuffer(data, dtype=np.uint8)
decoded = decoded.reshape((270, 480,3))
return decoded;
while True:
# Wait for a connection
print( 'waiting for a connection')
connection, client_address = sock.accept()
try:
print( 'connection from', client_address)
# Receive the data in once
while True:
#Image size is (480x270), with 3 color channel : (480 x 270) x 3 = 388800 bytes
data = connection.recv(388800)
if data:
#Visualize the received data
cv2.imshow('IMG',applyImage(data))
if (cv2.waitKey(1) and 0xFF == ord('q')):
break
else:
print('no more data from', client_address);
break;
finally:
# Clean up the connection
connection.close()
cv2.waitKey(0)
cv2.destroyAllWindows()
connection.close()