-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_ui.py
More file actions
86 lines (62 loc) · 2.45 KB
/
web_ui.py
File metadata and controls
86 lines (62 loc) · 2.45 KB
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
from flask import Flask, request, render_template
from flask import redirect, url_for, flash
from flask import send_from_directory
import os
from werkzeug.utils import secure_filename
import argparse
import cv2 as cv
from carver import Engine
DEFAULT_ADDR = os.environ['HOSTNAME']
DEFAULT_PORT = 10800
UPLOAD_FOLDER = './uploads'
IMAGE_FOLDER = './results'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['IMAGE_FOLDER'] = IMAGE_FOLDER
carver = Engine()
@app.route('/', methods=['GET', 'POST'])
def upload_file():
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)
filename = secure_filename(file.filename)
srcPath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(srcPath)
target_width = int(request.form['width'])
target_height = int(request.form['height'])
image = cv.imread(srcPath)
output = carver.run(image, target_width, target_height)
if output is not None:
desPath = os.path.join(app.config['IMAGE_FOLDER'], filename)
cv.imwrite(desPath, output)
return redirect(url_for('show_file', filename=filename))
else:
return "Sorry, something went wrong"
return render_template('upload.html')
@app.route('/images/<filename>')
def show_file(filename):
return send_from_directory(app.config['IMAGE_FOLDER'], filename)
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
def parser():
p = argparse.ArgumentParser()
p.add_argument('--ip', default=DEFAULT_ADDR, help='ip address')
p.add_argument('--port', default=DEFAULT_PORT, type=int, help=f'port number. Default to {DEFAULT_PORT}')
args = p.parse_args()
return args
if __name__ == '__main__':
opts = parser()
if not os.path.isdir(app.config['UPLOAD_FOLDER']):
os.mkdir(app.config['UPLOAD_FOLDER'])
if not os.path.isdir(app.config['IMAGE_FOLDER']):
os.mkdir(app.config['IMAGE_FOLDER'])
# app.config['ENV'] = 'development'
app.config['MAX_CONTENT_LENGTH'] = 124 * 1024 * 1024
app.run(host=opts.ip, port=opts.port)