-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
74 lines (55 loc) · 2.31 KB
/
main.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
import os
import cv2
from app import app
from flask import flash, request, redirect, url_for, render_template
from werkzeug.utils import secure_filename
from fasterrcnn import object_detection
# allowed image extension
def allowed_image_extension(filename):
return '.' in filename and filename.rsplit('.', 1)[1].upper() in app.config["ALLOWED_IMAGE_EXTENSIONS"]
# No caching at all for API endpoints.
@app.after_request
def add_header(response):
# response.cache_control.no_store = True
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'
return response
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/', methods=['POST'])
def upload_image():
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No image selected for uploading')
return redirect(request.url)
if file and allowed_image_extension(file.filename):
filename = secure_filename(file.filename)
print(filename)
path = os.path.join(app.config['UPLOAD_FOLDER'], 'uploaded.jpg')
file.save(path)
print(path)
# print('upload_image filename: ' + filename)
count_classes = object_detection(path)
flash('Image successfully uploaded and displayed below')
return render_template('upload.html', predicted='predicted.jpg',count_classes=count_classes)
else:
flash('Allowed image types are -> png, jpg, jpeg, gif')
return redirect(request.url)
@app.route('/display/<filename>')
def display_image(filename):
# print('display_image filename: ' + filename)
# object_detection_api('static/uploads/'+filename, rect_th=2, text_th=1, text_size=1)
return redirect(url_for('static', filename='uploads/' + filename), code=301)
"""@app.route('/predicted/<filename>')
def predicted_image(filename):
# print('display_image filename: ' + filename)
# object_detection_api('static/uploads/'+filename, rect_th=2, text_th=1, text_size=1)
return redirect(url_for('static', filename='uploads/' + filename), code=301)
"""
if __name__ == "__main__":
app.run()