-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion_server.py
More file actions
115 lines (93 loc) · 4.09 KB
/
Copy pathmotion_server.py
File metadata and controls
115 lines (93 loc) · 4.09 KB
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
# -*- coding: utf8 -*-
# socket 과 select 모듈 임포트
import init_path
from socket import *
from select import *
import sys, os
import csv
import numpy as np
import readData, tools
from time import ctime, gmtime, strftime
from keras.models import Sequential, model_from_json
from keras.layers import LSTM, Dense
active_class = ['stop', 'motion']
motion_class = ['left', 'right', 'left_r', 'right_r', 'up', 'down', 'arm_down', 'arm_up']
m_threshold = 0.94
a_threshold = 0.94
print np.version.version
print "model load..."
filePath = os.path.join("server", "result", "active_model_config.json");
active_model = model_from_json(open(filePath).read())
filePath = os.path.join("server", "result", "active_model_weight.h5");
active_model.load_weights(filePath)
active_model.compile(optimizer='adam', loss='categorical_crossentropy')
filePath = os.path.join("server", "result", "motion_model_config.json");
motion_model = model_from_json(open(filePath).read())
filePath = os.path.join("server", "result", "motion_model_weight.h5");
motion_model.load_weights(filePath)
motion_model.compile(optimizer='adam', loss='categorical_crossentropy')
print "model load complete"
winSize = 30
data_dim = 6
sensorWindow = np.zeros((1, winSize, data_dim));
sensorWindow[0][0:29] = sensorWindow[0][1:30]
# print "test"
# sensor_test_data = readData.read_sensor_data("server/data/processed_data/all_test_motion.txt");
# X_data, y_data = tools.data_processing(sensor_test_data, 30, 6, 8)
# # print X_data.shape
# result = motion_model.predict(X_data[0:1])
# index = np.where(result==max(result))[0][0]
# print motion_class[index]
# print result, sensorWindow.shape
# 호스트, 포트와 버퍼 사이즈를 지정
HOST = ''
PORT = 3300
BUFSIZE = 4096
ADDR = (HOST, PORT)
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(ADDR)
serverSocket.listen(10)
connection_list = [serverSocket]
while connection_list:
try:
print('[INFO] Waiting connection...')
# select 로 요청을 받고, 10초마다 블럭킹을 해제하도록 함
read_socket, write_socket, error_socket = select(connection_list, [], [], 10)
for sock in read_socket:
# 새로운 접속
if sock == serverSocket:
clientSocket, addr_info = serverSocket.accept()
connection_list.append(clientSocket)
print('[INFO][%s] Client(%s) is connected.' % (ctime(), addr_info[0]))
else:
data = sock.recv(BUFSIZE)
if data:
data_lines = data.split('\n')
for line in data_lines:
split_data = line.split(' ')
# print line
# print '/////////////////////////////////////////////////////////////'
if len(split_data) != 6: break
split_data = [float(x) for x in split_data]
sensorWindow[0][0:29] = sensorWindow[0][1:30]
sensorWindow[0][29] = split_data
# softmax_motion = motion_model.predict(sensorWindow)
# print max(softmax_motion[0])
softmax_active = active_model.predict(sensorWindow)
a_index = np.where(softmax_active[0]==max(softmax_active[0]))[0][0]
# print max(softmax_active), active_class[index]
softmax_motion = motion_model.predict(sensorWindow)
m_index = np.where(softmax_motion[0]==max(softmax_motion[0]))[0][0]
if a_index == 1:
if max(softmax_motion[0]) > m_threshold:
print motion_class[m_index], max(softmax_motion[0])
else:
if max(softmax_active[0]) > a_threshold:
print active_class[a_index], max(softmax_active[0])
else:
connection_list.remove(sock)
sock.close()
print('[INFO][%s] Disconnected' % ctime())
except KeyboardInterrupt:
serverSocket.close()
sys.exit()