forked from kevinkit/Camera2TCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientD.py
97 lines (78 loc) · 2.18 KB
/
clientD.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
91
92
93
94
95
96
97
# -*- 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 time
host = socket.gethostbyname(socket.gethostname())
host = '141.19.87.118'
port = 2004
BUFFER_SIZE = 305280
MESSAGE_ASK = "Connecting2KinectDepth"
MESSAGE = "Depth"
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 data
print shaping
for i in range(0,len(shaping)):
shaping[i] = int(shaping[i])
BUFFER_SIZE =2*( int(shaping[0])*int(shaping[1])*int(shaping[2]))
# BUFFER_SIZE = 434176
break;
print("Buffer size: " + str(BUFFER_SIZE));
while True:
print("trying")
t0 = time.time()
tcpClientA.send(MESSAGE)
print("message was send")
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 data
if data != "no depth frame" and len(data) == BUFFER_SIZE:
temp = np.frombuffer(data,dtype=np.uint8)
h = np.asarray(temp[0::2],dtype=np.uint16)
l = np.asarray(temp[1::2],dtype=np.uint16)
res = (h << 8) + l
img = res.reshape(shaping[1],shaping[0])
print(img.dtype)
print(max(img[0]))
#Sinec it is 12 bit
rep = np.asarray(img >> 5,dtype=np.uint8)
else:
continue;
try:
cv2.imshow('something',rep)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
cv2.imwrite('depth.png',rep)
tcpClientA.close()