-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathupload.py
62 lines (47 loc) · 1.92 KB
/
upload.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
import os
from datetime import datetime
from flask import Flask, request, render_template, send_from_directory
from main import infer_by_web
__author__ = 'Susan'
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__)) # project abs path
@app.route("/")
def index():
return render_template("index.html")
@app.route("/upload_page", methods=["GET"])
def upload_page():
return render_template("upload.html")
@app.route("/upload", methods=["POST"])
def upload():
# folder_name = request.form['uploads']
target = os.path.join(APP_ROOT, 'static/')
print(target)
if not os.path.isdir(target):
os.mkdir(target)
print(request.files.getlist("file"))
option = request.form.get('optionsPrediction')
print("Selected Option:: {}".format(option))
for upload in request.files.getlist("file"):
print(upload)
print("{} is the file name".format(upload.filename))
filename = upload.filename
# This is to verify files are supported
ext = os.path.splitext(filename)[1]
if (ext == ".jpg") or (ext == ".png"):
print("File supported moving on...")
else:
render_template("Error.html", message="Files uploaded are not supported...")
savefname = datetime.now().strftime('%Y-%m-%d_%H_%M_%S') + "."+ext
destination = "/".join([target, savefname])
print("Accept incoming file:", filename)
print("Save it to:", destination)
upload.save(destination)
result = predict_image(destination, option)
print("Prediction: ", result)
# return send_from_directory("images", filename, as_attachment=True)
return render_template("complete.html", image_name=savefname, result=result)
def predict_image(path, type):
print(path)
return infer_by_web(path, type)
if __name__ == "__main__":
app.run(port=4555, debug=True)