-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
93 lines (73 loc) · 3.3 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
from flask import Flask, jsonify, request, render_template, flash, redirect, url_for
from flask_cors import CORS
import werkzeug.utils
import os,datetime
import cv2 as cv
from configs import *
from tools import *
from model_env.model import *
from model_env.logo_model import *
app = Flask(__name__)
CORS(app)
app.config['SECRET_KEY'] = SECRET_KEY
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/', methods=['GET', 'POST'])
def index():
files = sorted(os.listdir(os.path.join('static', 'uploads')),reverse=True)
set_date = list(set(map(lambda x:x.split('_')[0],files)))
set_date = sorted(set_date,reverse=True)
print(files)
print(set_date)
return render_template('Home.html', files=files, date=set_date)
@app.route('/test', methods=['GET', 'POST'])
def test():
return jsonify({"message": 2})
@app.route('/upload', methods=['GET', 'POST'])
def uploadfile():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect('/upload')
file = request.files['file']
if file.filename == '':
flash('No image selected for uploading')
return redirect('/upload')
if file and allowed_file(file.filename):
filename = file.filename.replace(' ', '_')
print(filename)
filename = datetime.datetime.now().strftime("%Y-%m-%d_%X._")
print(datetime.datetime.now().strftime("%Y-%m-%d_%X.3"))
filename = werkzeug.utils.secure_filename(filename)
if not os.path.exists(os.path.join(app.config['UPLOAD_FOLDER'], filename.split('.')[0])):
os.mkdir(os.path.join(app.config['UPLOAD_FOLDER'], filename.split('.')[0]))
os.mkdir(os.path.join(app.config['UPLOAD_FOLDER'], filename.split('.')[0], 'brand'))
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename.split('.')[0], 'original.jpg'))
img = cv.imread(os.path.join(app.config['UPLOAD_FOLDER'], filename.split('.')[0], 'original.jpg'))
xy, _ = usemodel(img.copy(), os.path.join(app.config['UPLOAD_FOLDER'], filename.split('.')[0], 'brand'))
print(xy)
visualize_model(img, filename, model_finetune)
return render_template('upload.html', filename=filename,logo = xy['name'])
else:
flash('Allowed image types are - png, jpg, jpeg, gif')
return redirect('/')
return render_template('upload.html')
@app.route('/display/<filename>/<file>')
def display_image(filename, file):
path = os.path.join('uploads', filename.split('.')[0], file)
return redirect(url_for('static', filename=path), code=301)
@app.route('/<filename>', methods=['GET', 'POST'])
def car(filename):
try:
data = {
'logo':glob.glob(os.path.join('static', 'uploads', filename,'brand', '*.jpg'))[0]
}
except: data = dict()
files = glob.glob(os.path.join('static', 'uploads', filename, '*.png'))
files = list(map(lambda x: x.replace('static/', ''), files))
print(files)
return render_template('Car.html', filename=filename, files=files,data=data)
@app.route('/clear')
def clear():
return jsonify({'status': 'remove all file success.', 'filenames': clear_file()}), 201
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0', port=PORT)