-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·117 lines (96 loc) · 3.51 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
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
import concurrent.futures as Processor
import os
from flask import Flask, request, flash, redirect, Response, jsonify
from werkzeug.utils import secure_filename
from constants import DBTYPES
from train import Trainer
app = Flask(__name__)
app.secret_key = "841578451845asdasdasfasf"
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
whiteListTrainer = Trainer(DBTYPES.WHITELIST)
blackListTrainer = Trainer(DBTYPES.BLACKLIST)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/<dbType>/names")
def getNames(dbType):
if dbType == DBTYPES.WHITELIST:
return jsonify(whiteListTrainer.readNames())
else:
return jsonify(blackListTrainer.readNames())
@app.route("/<dbType>/<name>/images", methods=['POST'])
def upload_file(name,dbType):
print(dbType,)
if dbType == DBTYPES.WHITELIST:
path = whiteListTrainer.imagesPath % name
else:
path=blackListTrainer.imagesPath % name
if not os.path.exists(path):
os.mkdir(path)
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename) and os.path.exists(path):
filename = secure_filename(file.filename)
file.save(os.path.join(path, filename))
return Response(status=200)
else:
return Response(status=404)
@app.route("/<dbType>/add_name", methods=['POST'])
def addUser(dbType):
if request.method == "POST":
try:
name = request.json["name"]
if dbType == DBTYPES.WHITELIST:
whiteListTrainer.addName(name)
else:
blackListTrainer.addName(name)
return Response(status=200)
except:
return Response(status=500)
@app.route("/<dbType>/remove_name", methods=['POST'])
def removeUser(dbType):
if request.method == "POST":
try:
name = request.json["name"]
if dbType == DBTYPES.WHITELIST:
whiteListTrainer.removeName(name)
else:
blackListTrainer.removeName(name)
return Response(status=200)
except:
return Response(status=500)
@app.route("/<dbType>/train", methods=['POST'])
def train(dbType):
if request.method == "POST":
try:
name = request.json["name"]
if dbType == DBTYPES.WHITELIST:
whiteListTrainer.trainPerson(name)
else:
blackListTrainer.trainPerson(name)
return Response(status=200)
except:
return Response(status=500)
@app.route("/<dbType>/train_image", methods=['POST'])
def trainImage(dbType):
if request.method == "POST":
try:
name = request.json["name"]
image = request.json["image"]
if dbType == DBTYPES.WHITELIST:
with Processor.ProcessPoolExecutor() as Executor:
Executor.map(whiteListTrainer.trainImage(name, image))
else:
with Processor.ProcessPoolExecutor() as Executor:
Executor.map(blackListTrainer.trainImage(name, image))
return Response(status=200)
except:
return Response(status=500)
if __name__ == '__main__':
app.run(host="0.0.0.0")