-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
76 lines (68 loc) · 3.15 KB
/
server.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
import tornado.web
import tornado.ioloop
import tornado.escape
import config
import json
import argparse
from inverse_kinematic_model import InverseKinematicModel
from transform import normalize_vector
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render('./web/index.html', port=ARGS.port)
class UpdateHandler(tornado.web.RequestHandler):
# def set_default_headers(self):
# self.set_header("Access-Control-Allow-Origin", "*")
# self.set_header("Access-Control-Allow-Headers", "x-requested-with")
# self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
# # self.set_header("Access-Control-Allow-Headers", "Access-Control-Allow-Headers,"
# # " Origin,Accept, X-Requested-With, Content-Type, "
# # "Access-Control-Request-Method, Access-Control-Request-Headers")
def post(self):
data = tornado.escape.json_decode(self.request.body)
left_hand_pos = data['leftHandPos']
right_hand_pos = data['rightHandPos']
left_foot_pos = data['leftFootPos']
right_foot_pos = data['rightFootPos']
positions = [normalize_vector([left_hand_pos], config.hand_target_radius),
normalize_vector([right_hand_pos], config.hand_target_radius),
normalize_vector([left_foot_pos], config.foot_target_radius),
normalize_vector([right_foot_pos], config.foot_target_radius)]
left_hand_rotation, right_hand_rotation, left_foot_rotation, right_foot_rotation = model.predict_rotations(positions)
rotations = {
'leftHandRotation': left_hand_rotation.tolist(),
'rightHandRotation': right_hand_rotation.tolist(),
'leftFootRotation': left_foot_rotation.tolist(),
'rightFootRotation': right_foot_rotation.tolist()
}
self.write(json.dumps(rotations))
# def options(self):
# self.set_status(204)
# self.finish()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--port', type=int, default=8000,
help='local server port, default 8000'
)
ARGS = parser.parse_args()
MODEL_PATH = './model/inverse_kinematic.h5'
model = InverseKinematicModel(config.d1,
config.d2,
config.l1,
config.l2,
config.left_hand_rotation_range,
config.right_hand_rotation_range,
config.left_foot_rotation_range,
config.right_foot_rotation_range
)
model.load_model(MODEL_PATH)
settings = {
"static_path": "./web"
}
application = tornado.web.Application([
(r"/", IndexHandler),
(r"/update", UpdateHandler)
], **settings)
application.listen(ARGS.port)
print('Inverse kinematic model application at http://localhost:{}'.format(ARGS.port))
tornado.ioloop.IOLoop.current().start()