forked from kevinkit/Camera2TCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientB.py
90 lines (73 loc) · 2.04 KB
/
clientB.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
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 15 20:52:38 2017
@author: kevin
This is a simple client for getting HD-RGB data from the Kinect v2
"""
# Python TCP Client A
import socket
import numpy as np
import cv2
import binascii
import time
host = socket.gethostbyname(socket.gethostname())
host = '141.19.87.118'
port = 2004
BUFFER_SIZE = 305280
MESSAGE_ASK = "Connecting2KinectWebcam"
MESSAGE = "KinectWebcam"
tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpClientA.connect((host, port))
#Send request
while True:
try:
print("trying on " + str(host))
tcpClientA.send(MESSAGE_ASK);
data = tcpClientA.recv(BUFFER_SIZE)
except Exception:
print data
print("host unreachable...will...try..to..connect again");
continue;
shaping = data.split(',')
print shaping
for i in range(0,len(shaping)):
shaping[i] = int(shaping[i])
BUFFER_SIZE = int(shaping[0])*int(shaping[1])*int(shaping[2])
break;
print("Buffer size: " + str(BUFFER_SIZE));
print("Shape:" + str(shaping))
while True:
print("trying")
t0 = time.time()
tcpClientA.send(MESSAGE)
print("message was send")
print shaping
sem = 0;
data = ''
try:
print("waiting...")
while len(data) < BUFFER_SIZE:
if sem == 0:
data = tcpClientA.recv(BUFFER_SIZE)
sem = 1;
else:
data += tcpClientA.recv(BUFFER_SIZE)
print("recieving data")
except Exception:
print("host unreachable...will try...forever...")
continue;
print ("Recieved data len: " + str(len(data)))
# print np.frombuffer(data,dtype=np.uint8)
if data != "no frame":
img = np.frombuffer(data,dtype=np.uint8).reshape((int(shaping[0]),int(shaping[1]),int(shaping[2])))
t1 = time.time()
print("FPS: " + str(1/(t1-t0)))
# if (float(t1) - float(t0)) < 1./30:
# continue;
try:
cv2.imshow('something',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
cv2.imwrite('fu.png',img);
tcpClientA.close()