-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_socket_cvcam.py
53 lines (45 loc) · 1.54 KB
/
server_socket_cvcam.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
import cv2
import struct
import numpy as np
import time
from socket import *
# Opens server
serverPort = 8888
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print("The server is ready to receive on port", serverPort)
def recvall(sock, count):
buf = b''
while count:
newbuf = sock.recv(count)
if not newbuf: return None
buf += newbuf
count -= len(newbuf)
return buf
while True:
# Accepts connection
(connectionSocket, clientAddress) = serverSocket.accept()
print('Connection requested from', clientAddress)
intervals = []
try:
previousTime = time.time()
for i in range(2000):
# Get message
length = recvall(connectionSocket, 16)
print('image size:', int(length), 'bytes')
stringData = recvall(connectionSocket, int(length))
frame = cv2.imdecode(np.fromstring(stringData, dtype='uint8'), 1)
# See image
# cv2.imshow('frame', frame)
# cv2.waitKey(1)
currentTime = time.time()
intervals.append(currentTime - previousTime)
previousTime = currentTime
intervalArray = np.array(intervals)
print('average:', np.mean(intervalArray))
print('max:', np.max(intervalArray))
print('min:', np.min(intervalArray))
print(np.sort(intervalArray)[:-10:-1])
except:
print("Waiting for connection...")