-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
97 lines (73 loc) · 2.83 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
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
import os
from io import BytesIO
import base64
import sys
from flask import Flask, render_template, escape, send_from_directory, request, jsonify
from werkzeug import secure_filename
from werkzeug.exceptions import RequestEntityTooLarge
from PIL import Image
import numpy as np
import cv2
sys.path.append('ssd_pytorch')
from ssd_pytorch.inference import run_inference
predict_simple = None
SAVE_DATA = False
INPUT_IMG_PATH = 'static/images/input/'
OUTPUT_IMG_PATH = 'static/images/output/'
def cv2_img_from_upload(req_file):
req_file.stream.seek(0) # return to the start of the stream
data = np.fromstring(req_file.stream.read(), dtype=np.uint8)
img = cv2.imdecode(data, cv2.IMREAD_COLOR)
img_converted = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return img_converted
def base64_from_numpy_img(img):
im = Image.fromarray(img.astype('uint8'))
rawBytes = BytesIO()
im.save(rawBytes, 'JPEG')
rawBytes.seek(0) # return to the start of the file
base64img = base64.encodestring(rawBytes.read())
return base64img.decode().replace('\n', '')
app = Flask(__name__, static_folder='static')
# Max file upload size will be 16 MB
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
@app.errorhandler(413)
@app.errorhandler(RequestEntityTooLarge)
def app_handle_413(err):
return 'Attempted to upload file that was too large.', 413
@app.route('/<path:filename>')
def send_file(filename):
return send_from_directory(app.static_folder, filename)
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
# show the subpath after /path/
return 'Subpath %s' % escape(subpath)
@app.route('/contact', methods = ['POST'])
def contact():
return render_template('index.html', thankyou=request.form['name'])
@app.route('/')
def index():
return render_template('index.html', message='using machine learning to locate accessible parking spots')
@app.route('/file-upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
sfname = secure_filename(file.filename)
if SAVE_DATA:
input_path = os.path.join(INPUT_IMG_PATH, sfname)
file.save(input_path)
input_img = cv2_img_from_upload(file)
output_img, pts = predict_simple(input_img)
if SAVE_DATA:
output_path = os.path.join(OUTPUT_IMG_PATH, f"{sfname.split('.')[0]}-output.jpg")
cv2.imwrite(output_path, output_img)
return jsonify({
'base64': base64_from_numpy_img(output_img)
})
if __name__ == '__main__':
if SAVE_DATA:
if not os.path.exists(INPUT_IMG_PATH):
os.makedirs(INPUT_IMG_PATH)
if not os.path.exists(OUTPUT_IMG_PATH):
os.makedirs(OUTPUT_IMG_PATH)
predict_simple = run_inference('model/custom.pth', cuda=False)
app.run(debug=True, port=5000)