-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
52 lines (41 loc) · 1.61 KB
/
app.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
from flask import Flask, request,send_from_directory
from detector import predict,train
import os
app = Flask(__name__)
app.config["IMAGE_UPLOADS"] = './uploads'
@app.route('/')
def indexApp():
return 'service is running'
@app.route('/train')
def trainApp():
classifier = train("./train_dir", model_save_path="trained_model.clf", n_neighbors=2,delete_unfit_files=True)
response = send_from_directory(directory="./",filename="trained_model.clf",as_attachment=True)
print("Training complete!")
return response
@app.route('/detect', methods=['POST', 'GET'])
def detectApp():
if request.method == "GET":
return str('use post method using multipart form data, "file" as param')
# filePath = './uploads/pic1.jpg'
# predictions = predict(filePath, model_path ='trained_model.clf')
# names = []
# for name, (top, right, bottom, left) in predictions:
# print(name)
# names.append(name)
# return str(names)
if request.method == "POST":
if request.files:
image = request.files["file"]
filePath = os.path.join(
app.config["IMAGE_UPLOADS"], image.filename).replace('\\', '/')
image.save(filePath)
print('\033[94m' + filePath + '\033[0m')
predictions = predict(filePath, model_path ='trained_model.clf')
names = []
for name, (top, right, bottom, left) in predictions:
print(name)
names.append(name)
return str(names)
return 'wrong input format'
if __name__ == "__main__":
app.run(debug=True)