/
server_video.py
80 lines (68 loc) · 1.65 KB
/
server_video.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from socket import socket
import cv2
import dill
from lz4 import compress, decompress
import time
from threading import Thread
def chunks(inp):
sr = dill.dumps(inp)
inp = compress(sr)
bs = 2**14
out = []
for x in range((len(inp) / bs) + 1):
out.append(inp[(bs*x):(bs)*(x+1)])
return out
def chunk_image(chunks):
out = ''
for chunk in chunks:
out += chunk
out = decompress(out)
sr = dill.loads(out)
print type(sr)
return sr
def Ser():
addr = ('127.0.0.9', 5000)
s = socket()
s.bind(addr)
s.listen(1)
c, addr = s.accept()
vc = cv2.VideoCapture(0)
while True:
try:
_, frame = vc.read()
ch = chunks(frame)
pre = str(len(ch)) + ',' + str(len(ch[-1])) + ','
pre = pre.ljust(12)
c.send(pre)
for x in ch:
c.send(x)
print 'fine'
except:
pass
def Rev():
addr = ('127.0.0.9',5001)
s = socket()
s.connect(addr)
cv2.namedWindow('Chat1')
while True:
try:
out = []
pre = s.recv(12)
(num_chunks, len_last) = (int(pre.split(',')[0]), int(pre.split(',')[1]))
for rond in range(num_chunks):
if rond != num_chunks - 1:
chunk= s.recv(2**14)
out.append(chunk)
else:
chunk= s.recv(len_last)
out.append(chunk)
cv2.imshow('Chat', chunk_image(out))
key = cv2.waitKey(20)
print 'fine'
except:
pass
if __name__ == '__main__':
t1 = Thread(target = Rev)
t2 = Thread(target = Ser)
t1.start()
t2.start()