-
Notifications
You must be signed in to change notification settings - Fork 5
/
depthmq.py
122 lines (94 loc) · 2.49 KB
/
depthmq.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import depth
import mqpy
import numpy as np
import argparse
"""
Header is an ASCII string of key=value pairs. It ends with `!HEADEREND\n`.
The rest is the data bytestring.
***************************
ptype=REQ
pname=HANDSHAKE_DEPTH
pversion=2
client_program=DepthViewer
client_program_version=v0.8.11
!HEADEREND
***************************
ptype=RES
pname=HANDSHAKE_DEPTH
model_type=dpt_hybrid_384
accepted_input_formats=jpg,ppm
output_format=pfm
depth_map_type=Inverse
server_program=depthmq
server_program_version=v0.8.11
!HEADEREND
***************************
***************************
ptype=REQ
pname=DEPTH
input_format=jpg
!HEADEREND
(bytestring)
***************************
ptype=RES
pname=DEPTH
!HEADEREND
(bytestring)
***************************
Key "data" is reserved.
"""
model_type = "NotSetYet"
accepted_input_formats = "jpg, ppm"
output_format = "pfm"
def on_req_handshake_depth(mdict, data=None):
pversion = mdict["pversion"]
if int(pversion) > mqpy.PVERSION:
return mqpy.create_error_message(f"Unsupported pversion: {pversion}")
return mqpy.create_message({
"ptype": "RES",
"pname": mdict["pname"],
"model_type": model_type,
"accepted_input_formats": accepted_input_formats,
"output_format": output_format,
"depth_map_type": runner.depth_map_type,
"server_program": "depthmq",
"server_program_version": depth.VERSION,
})
def on_req_depth(mdict, data):
input_format = mdict["input_format"]
if input_format in ["jpg", "ppm"]:
img = runner.read_image_bytes(data)
res = runner.run_frame(img) #ndarray
res = runner.get_pfm(res)
else:
error_msg = f"Error: unknown input_format: {input_format}"
print(error_msg)
return mqpy.create_error_message(error_msg)
return mqpy.create_message({
"ptype": "RES",
"pname": mdict["pname"],
}, data=res)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
default_port = 5555
parser.add_argument("-p", "--port",
help=f"port number. defaults to {default_port}.",
default=None
)
depth.add_runner_argparser(parser)
args = parser.parse_args()
print("depthmq: Init.")
model_type = args.model_type
runner = depth.get_loaded_runner(args)
print("depthmq: Preparing the model. This may take some time.")
dummy = np.zeros((512, 512, 3), dtype=np.float32)
runner.run_frame(dummy)
print("depthmq: Done.")
port = args.port if args.port is not None else default_port
mq = mqpy.MQ({
("REQ", "HANDSHAKE_DEPTH"): on_req_handshake_depth,
("REQ", "DEPTH"): on_req_depth,
})
mq.bind(port)
while True:
mq.receive()