forked from aosabook/500lines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
61 lines (52 loc) · 1.87 KB
/
Copy pathserver.py
File metadata and controls
61 lines (52 loc) · 1.87 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
import BaseHTTPServer
import json
from ocr import OCRNeuralNetwork
import numpy as np
HOST_NAME = 'localhost'
PORT_NUMBER = 8000
HIDDEN_NODE_COUNT = 15
# Load data samples and labels into matrix
data_matrix = np.loadtxt(open('data.csv', 'rb'), delimiter = ',')
data_labels = np.loadtxt(open('dataLabels.csv', 'rb'))
# Convert from numpy ndarrays to python lists
data_matrix = data_matrix.tolist()
data_labels = data_labels.tolist()
# If a neural network file does not exist, train it using all 5000 existing data samples.
# Based on data collected from neural_network_design.py, 15 is the optimal number
# for hidden nodes
nn = OCRNeuralNetwork(HIDDEN_NODE_COUNT, data_matrix, data_labels, list(range(5000)));
class JSONHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(s):
response_code = 200
response = ""
varLen = int(s.headers.get('Content-Length'))
content = s.rfile.read(varLen);
payload = json.loads(content);
if payload.get('train'):
nn.train(payload['trainArray'])
nn.save()
elif payload.get('predict'):
try:
response = {"type":"test", "result":nn.predict(str(payload['image']))}
except:
response_code = 500
else:
response_code = 400
s.send_response(response_code)
s.send_header("Content-type", "application/json")
s.send_header("Access-Control-Allow-Origin", "*")
s.end_headers()
if response:
s.wfile.write(json.dumps(response))
return
if __name__ == '__main__':
server_class = BaseHTTPServer.HTTPServer;
httpd = server_class((HOST_NAME, PORT_NUMBER), JSONHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
else:
print "Unexpected server exception occurred."
finally:
httpd.server_close()