-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain_user.py
359 lines (278 loc) · 11.6 KB
/
main_user.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# coding: utf-8
from flask import Flask, request
from app import db, models
import csv
import os #to get current path
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import importlib
from model import *
#algorithm part
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import scale
import matplotlib.pyplot as plt
PYTHONIOENCODING="UTF-8" #set the utf-8 encode mode
room_dic = {'0': 'the front of elevators', '1': 'Male Toilet', '2': 'Southern space', '3': 'Northern Space', '4': 'EE411', '5': 'EE405', '6': 'EE420'}
# create the application object
app = Flask(__name__)
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev = 0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.0, shape = shape)
return tf.Variable(initial)
def encode(x, e_weights_h1, e_weights_h2, e_weights_h3, e_biases_h1, e_biases_h2, e_biases_h3):
l1 = tf.nn.tanh(tf.add(tf.matmul(x,e_weights_h1),e_biases_h1))
l2 = tf.nn.tanh(tf.add(tf.matmul(l1,e_weights_h2),e_biases_h2))
l3 = tf.nn.tanh(tf.add(tf.matmul(l2,e_weights_h3),e_biases_h3))
return l3
def decode(x, d_weights_h1, d_weights_h2, d_weights_h3, d_biases_h1, d_biases_h2, d_biases_h3):
l1 = tf.nn.tanh(tf.add(tf.matmul(x,d_weights_h1),d_biases_h1))
l2 = tf.nn.tanh(tf.add(tf.matmul(l1,d_weights_h2),d_biases_h2))
l3 = tf.nn.tanh(tf.add(tf.matmul(l2,d_weights_h3),d_biases_h3))
return l3
def dnn(x, dnn_weights_h1, dnn_weights_h2, dnn_weights_out, dnn_biases_h1, dnn_biases_h2, dnn_biases_out):
l1 = tf.nn.relu(tf.add(tf.matmul(x,dnn_weights_h1),dnn_biases_h1))
#dropout = tf.nn.dropout(l1, 0.5)
l2 = tf.nn.relu(tf.add(tf.matmul(l1,dnn_weights_h2),dnn_biases_h2))
out = tf.nn.softmax(tf.add(tf.matmul(l2,dnn_weights_out),dnn_biases_out))
return out
def run_model(user_input):
with tf.Graph().as_default() as g:
n_input = 200
n_classes = 7
n_hidden_1 = 128
n_hidden_2 = 64
n_hidden_3 = 32
learning_rate = 0.01
training_epochs = 20
batch_size = 10
# --------------------- Encoder Variables --------------- #
X = tf.placeholder(tf.float32, shape=[None,n_input])
Y = tf.placeholder(tf.float32,[None,n_classes])
# --------------------- Encoder Variables --------------- #
e_weights_h1 = weight_variable([n_input, n_hidden_1])
e_biases_h1 = bias_variable([n_hidden_1])
e_weights_h2 = weight_variable([n_hidden_1, n_hidden_2])
e_biases_h2 = bias_variable([n_hidden_2])
e_weights_h3 = weight_variable([n_hidden_2, n_hidden_3])
e_biases_h3 = bias_variable([n_hidden_3])
# --------------------- Decoder Variables --------------- #
#d_weights_h1 = weight_variable([n_hidden_3, n_hidden_2])
d_weights_h1 = tf.transpose(e_weights_h3)
d_biases_h1 = bias_variable([n_hidden_2])
#d_weights_h2 = weight_variable([n_hidden_2, n_hidden_1])
d_weights_h2 = tf.transpose(e_weights_h2)
d_biases_h2 = bias_variable([n_hidden_1])
#d_weights_h3 = weight_variable([n_hidden_1, n_input])
d_weights_h3 = tf.transpose(e_weights_h1)
d_biases_h3 = bias_variable([n_input])
# --------------------- DNN Variables ------------------ #
dnn_weights_h1 = weight_variable([n_hidden_3, n_hidden_2])
dnn_biases_h1 = bias_variable([n_hidden_2])
dnn_weights_h2 = weight_variable([n_hidden_2, n_hidden_2])
dnn_biases_h2 = bias_variable([n_hidden_2])
dnn_weights_out = weight_variable([n_hidden_2, n_classes])
dnn_biases_out = bias_variable([n_classes])
init_op = tf.global_variables_initializer()
encoded = encode(X, e_weights_h1, e_weights_h2, e_weights_h3, e_biases_h1, e_biases_h2, e_biases_h3)
decoded = decode(encoded, d_weights_h1, d_weights_h2, d_weights_h3, d_biases_h1, d_biases_h2, d_biases_h3)
y_ = dnn(encoded, dnn_weights_h1, dnn_weights_h2, dnn_weights_out, dnn_biases_h1, dnn_biases_h2, dnn_biases_out)
room = tf.argmax(y_, 1)
#saver = tf.train.Saver()
with tf.Session(graph=g) as session:
session.run(init_op)
saver = tf.train.Saver()
saver.restore(session, ".\\trained_model\\trained_model.ckpt")
#print(session.run(y_, {X: user_input}))
#print(session.run(room, {X: user_input}))
return session.run(room, {X: user_input})
#Add apps info in database
def addAPs(list):
for row in range(0,200):
u = models.User(Room = list[row][2], BSSID = list[row][0], Level = list[row][1])
db.session.add(u)
db.session.commit()
def showAPs(num):
ap = models.User.query.get(num)
print(ap.Building, ap.Room, ap.Location_x, ap.BSSID, ap.Level)
# without SSID stored, encode mode is UTF-8
def addAllCSV(): #whole database
#csvfile = open('userinput.csv', 'w') #use file() rather than open() in python2.7
#Write mode 'a': write after... ‘w':clear all and write
with open('APs.csv', 'w', newline='') as csvfile:
if not os.path.getsize('./APs.csv'):
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
spamwriter.writerow([ 'Room', 'BSSID', 'Level', 'Model', 'Time'])
users = models.User.query.all()
for u in users:
data = ([
u.Room, u.BSSID, u.Level
])
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
spamwriter.writerow(data)
def addCSV(Building, Room, Location_x, Location_y, BSSID, Frequency, Level, Model, Time): #add one time's scanner result
with open('userinput.csv', 'a', newline='') as csvfile:
if not os.path.getsize('./userinput.csv'):
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
spamwriter.writerow(['Building', 'Room', 'Location_x', 'Location_y', 'BSSID', 'Frequency', 'Level', 'Model', 'Time'])
data = ([
Building, Room, Location_x, Location_y, BSSID, Frequency, Level, Model, Time
])
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
spamwriter.writerow(data)
def deleteDB():
users = models.User.query.all()
for u in users:
db.session.delete(u)
db.session.commit()
def initializeTempList():
with open('mapping.csv', 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
APs = [row[0] for row in reader]
APlength = len(APs)
lists = [[0 for col in range(5)] for row in range(APlength)]
row = 0
for AP in APs:
lists[row][0] = AP
lists[row][1] = '-110'
lists[row][2] = 'none'
lists[row][3] = 'none'
lists[row][4] = 'none'
row += 1
with open('tempList.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
spamwriter.writerow([ 'Room', 'BSSID', 'Level', 'Model', 'Time'])
for i in range(0,200):
data = ([
lists[i][0], lists[i][1], lists[i][2], lists[i][3], lists[i][4]
])
spamwriter.writerow(data)
def checkAP(list, AP): #check if the AP exist in AP list or not
row = 0
for row in range(0,200):
if AP == list[row][0]:
return row
return 'none'
def tempList(BSSID, Level, Room, Model, Time):
with open('tempList.csv', 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
RSS = [row for row in reader]
#print(RSS,RSS[0][0])
for row in range(1,201):
if RSS[row][0] == BSSID :
RSS[row][1] = Level
RSS[row][2] = Room
RSS[row][3] = Model
RSS[row][4] = Time
with open('tempList.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
spamwriter.writerow(['BSSID', 'Level', 'Room', 'Model', 'Time'])
for i in range(1,201):
data = ([
RSS[i][0], RSS[i][1], RSS[i][2], RSS[i][3], RSS[i][4]
])
spamwriter.writerow(data)
break
def isEmpty():
with open('xxx.csv', 'a+', newline='') as csvfile: #Check is tempList is empty
if not os.path.getsize('./xxx.csv'): #file not established
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
spamwriter.writerow(['BSSID', 'Level', 'Room', 'Model', 'Time'])
with open('mapping.csv', 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
APs = [row[0] for row in reader]
APlength = len(APs)
lists = [[0 for col in range(5)] for row in range(APlength)]
row = 0
for AP in APs:
lists[row][0] = AP
lists[row][1] = '-110'
lists[row][2] = 'none'
lists[row][3] = 'none'
lists[row][4] = 'none'
row += 1
with open('tempList.csv', 'a+', newline='') as csvfile: #Check is tempList is empty
if not os.path.getsize('./tempList.csv'): #file is empty
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
spamwriter.writerow(['BSSID', 'Level', 'Room', 'Model', 'Time'])
for i in range(0,200):
data = ([
lists[i][0], lists[i][1], lists[i][2], lists[i][3], lists[i][4]
])
spamwriter.writerow(data)
def refreshCSV(Room,Model,Time):
with open('tempList.csv', 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
RSS = [row for row in reader]
with open('tempList.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
spamwriter.writerow(['BSSID', 'Level', 'Room', 'Model', 'Time'])
for row in range(1,201):
RSS[row][2] = Room
RSS[row][3] = Model
RSS[row][4] = Time
room = ([
RSS[row][0], RSS[row][1], RSS[row][2], RSS[row][3], RSS[row][4]
])
spamwriter.writerow(room)
'''
with open('xxx.csv', 'a', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
for i in range(0,200):
data = ([
RSS[i][0], RSS[i][1], RSS[i][2]
])
spamwriter.writerow(data)
with open('oneTime.csv', 'a', newline='') as csvfile:
if not os.path.getsize('./oneTime.csv'): #file is empty
spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE)
spamwriter.writerow(['BSSID', 'Level', 'Room'])
for i in range(0,200):
data = ([
RSS[i][0], RSS[i][1], RSS[i][2]
])
spamwriter.writerow(data)
'''
@app.route('/', methods=['POST'])
def post():
isEmpty()
#lists = rawList()
#isTempListEmpty()
Building = request.form['Building']
Room = request.form['Room']
Location_x = request.form['Location_x']
Location_y = request.form['Location_y']
SSID = request.form['SSID']
BSSID = request.form['BSSID']
Frequency = request.form['Frequency']
Level = request.form['Level']
Model = request.form['Model']
Time = request.form['Time']
Done = request.form['Done']
tempList(BSSID, Level, Room, Model, Time)
#print(Level, ' ', SSID, BSSID, Down)
if(Done == 'YES'):
refreshCSV(Room,Model,Time)
user_input = pd.read_csv("tempList.csv",header = 0)
user_input = np.asarray(user_input.ix[0:200, 1]).reshape([1, 200])
user_input = scale(user_input, axis=1)
location = run_model(user_input)
#print('Location:', type(location))
initializeTempList()
print('\n=====================================================\n')
print('\tYou are now at ', room_dic[str(location[0])])
print('\n=====================================================\n')
return str(location[0])
#addAPs(list)
#addAllCSV()
#addAPs(Building, Room, Location_x, Location_y, SSID,BSSID, Frequency, Level)
#addCSV(Building, Room, Location_x, Location_y, BSSID, Frequency, Level)
#print('Building:'Building, 'Room:'Room,'Location_x:'Location_x, 'Location_y:'Location_y, 'SSID:'SSID, 'BSSID:'BSSID, 'Frequency:'Frequency, 'Level:'Level, 'Down?:'Down)
#print ("Building: %s, Room: %s, Location_x: %s, Location_y: %s, SSID: %s, BSSID: %s, Frequency: %s, Level: %s, Down?: %s" % (Building, Room, Location_x, Location_y, SSID, BSSID, Frequency, Level, Down))
return '[-1]'
if __name__ == "__main__":
#app.run(host='0.0.0.0', debug=False)
#app.run(host='192.168.43.202', debug=True)
app.run(host='10.8.222.33', debug=True)