forked from schollz/find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rf.py
219 lines (203 loc) · 6.47 KB
/
rf.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
import json
import sys
import os
import pickle
import sklearn
import random
import numpy
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction import DictVectorizer
from sklearn.pipeline import make_pipeline
from random import shuffle
random.seed(123)
class RF(object):
#data = []
def __init__(self):
self.size = 0
self.data = []
self.nameX = []
self.trainX = numpy.array([])
self.testX = numpy.array([])
self.nameY = []
self.trainY = []
self.testY = []
self.macSet = set()
self.locationSet = set()
def get_data(self, fname, splitRatio):
# First go through once and get set of macs/locations
X = []
with open("data/" + fname + ".rf.json",'r') as f_in:
for fingerprint in f_in:
data = json.loads(fingerprint)
X.append(data)
self.locationSet.add(data['location'])
for signal in data['wifi-fingerprint']:
self.macSet.add(signal['mac'])
# Convert them to lists, for indexing
self.nameX = list(self.macSet)
self.nameY = list(self.locationSet)
# Go through the data again, in a random way
shuffle(X)
# Split the dataset for training / learning
trainSize = int(len(X)*splitRatio)
# Initialize X, Y matricies for training and testing
self.trainX=numpy.zeros((trainSize, len(self.nameX)))
self.testX=numpy.zeros((len(X)-trainSize, len(self.nameX)))
self.trainY =[0]*trainSize
self.testY =[0]*(len(X)-trainSize)
curRowTrain = 0
curRowTest = 0
for i in range(len(X)):
newRow = numpy.zeros(len(self.nameX))
for signal in X[i]['wifi-fingerprint']:
newRow[self.nameX.index(signal['mac'])] = signal['rssi']
if i < trainSize: # do training
self.trainX[curRowTrain,:] = newRow
self.trainY[curRowTrain] = self.nameY.index(X[i]['location'])
curRowTrain = curRowTrain + 1
else:
self.testX[curRowTest,:] = newRow
self.testY[curRowTest] = self.nameY.index(X[i]['location'])
curRowTest = curRowTest + 1
def learn(self, dataFile,splitRatio):
self.get_data(dataFile,splitRatio)
clf = RandomForestClassifier(n_estimators=10, max_depth=None,
min_samples_split=2, random_state=0)
clf.fit(self.trainX, self.trainY)
score = clf.score(self.testX, self.testY)
with open('data/'+dataFile+'.rf.pkl','wb') as fid:
pickle.dump([clf,self.nameX,self.nameY],fid)
return score
def classify(self,groupName,fingerpintFile):
with open('data/' + groupName + '.rf.pkl','rb') as pickle_file:
[clf,self.nameX,self.nameY] = pickle.load(pickle_file)
# As before, we need a row that defines the macs
newRow = numpy.zeros(len(self.nameX))
data = {}
with open(fingerpintFile,'r') as f_in:
for line in f_in:
data = json.loads(line)
if len(data) == 0:
return
for signal in data['wifi-fingerprint']:
# Only add the mac if it exists in the learning model
if signal['mac'] in self.nameX:
newRow[self.nameX.index(signal['mac'])] = signal['rssi']
prediction = clf.predict_proba(newRow.reshape(1,-1))
predictionJson = {}
for i in range(len(prediction[0])):
predictionJson[self.nameY[i]] = prediction[0][i]
return predictionJson
# import socket
#
#
# TCP_IP = '127.0.0.1'
# TCP_PORT = 5006
# BUFFER_SIZE = 1024 # Normally 1024, but we want fast response
#
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.bind((TCP_IP, TCP_PORT))
# s.listen(1)
#
# while True:
# conn, addr = s.accept()
# print ('Connection address:', addr)
# while 1:
# data = conn.recv(BUFFER_SIZE)
# if not data: break
# data = data.decode('utf-8')
# print ("received data:", data)
# group = data.split('=')[0].strip()
# filename = data.split('=')[1].strip()
# randomF = RF()
# payload = "error".encode('utf-8')
# try:
# payload = json.dumps(randomF.classify(group,filename+".rftemp")).encode('utf-8')
# except:
# pass
#
# conn.send(payload) # echo
# conn.close()
import socketserver
class EchoRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
# Echo the back to the client
data = self.request.recv(1024)
data = data.decode('utf-8').strip()
print ("received data:'%s'" % data)
group = data.split('=')[0].strip()
filename = data.split('=')[1].strip()
payload = "error".encode('utf-8')
if len(group) == 0:
self.request.send(payload)
return
randomF = RF()
if len(filename) == 0:
payload = json.dumps(randomF.learn(group,0.9)).encode('utf-8')
else:
payload = json.dumps(randomF.classify(group,filename+".rftemp")).encode('utf-8')
self.request.send(payload)
return
if __name__ == '__main__':
# if len(sys.argv)==2:
# # Learn print("python3 rf.py groupName")
# # Requires writing a file to disk, groupName.rf.json
# randomF = RF()
# print(randomF.learn(sys.argv[1],0.5))
# elif len(sys.argv)==3:
# randomF = RF()
# print(randomF.classify(sys.argv[1],sys.argv[2]))
# else:
import socket
import threading
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("port")
args = parser.parse_args()
socketserver.TCPServer.allow_reuse_address = True
address = ('localhost', int(args.port)) # let the kernel give us a port
server = socketserver.TCPServer(address, EchoRequestHandler)
ip, port = server.server_address # find out what port we were given
server.serve_forever()
# from flask import Flask, request
# app = Flask(__name__)
#
# @app.route('/learn')
# def learn():
# group = request.args.get('group', '')
# if len(group) == 0:
# return "error"
# randomF = RF()
# randomF.learn(group,0.7)
# return 'done'
#
# @app.route('/track')
# def track():
# import time
# start_time = time.time()
# group = request.args.get('group', '')
# if len(group) == 0:
# return "error"
# filename = request.args.get('filename', '')
# if len(filename) == 0:
# return "error"
# randomF = RF()
# print("--- %s seconds ---" % (time.time() - start_time))
# return json.dumps(randomF.classify(group,filename+".rftemp"))
# # python3 rf.py groupName
# try:
# # randomF = RF()
# # randomF.classify(sys.argv[2],sys.argv[3])
# # randomF.learn(fname,0.5) # file, and percentage of data to use to learn
# if len(sys.argv)==2:
# # Learn print("python3 rf.py groupName")
# # Requires writing a file to disk, groupName.rf.json
# randomF = RF()
# randomF.learn(sys.argv[1],0.7)
# elif len(sys.argv)==3:
# randomF = RF()
# randomF.classify(sys.argv[1],sys.argv[2])
# else:
# print("error")
# except:
# print("error")