-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (57 loc) · 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
from fileinput import filename
from flask import Flask, redirect, render_template, request, url_for, flash
import tensorflow as tf
from werkzeug.utils import secure_filename
from tensorflow.keras.preprocessing.image import load_img
import numpy as np
import os
def model_predict(filename, loaded_model):
image = load_img(os.path.join(os.getcwd(), 'uploads', filename))
classes = ['Speed limit (20km/hr)', 'Speed limit (30km/hr)', 'Speed limit (50km/hr)',
'Speed limit (60km/hr)', 'Speed limit (70km/hr)', 'Speed limit (80km/hr)',
'End of speed limit (80km/hr)', 'Speed limit (100km/hr)', 'Speed limit (120km/hr)',
'No passing', 'No passing for vehicles over 3.5 metric tons',
'Right-of-way at the next intersection', 'Priority road', 'Yield',
'Stop', 'No vehicles', 'Vehicles over 3.5 metric tons prohibited',
'No entry', 'General caution', 'Dangerous curve to the left',
'Dangerous curve to the right', 'Double curve', 'Bumpy road', 'Slippery road',
'Road narrows to the right', 'Road work', 'Traffic signals', 'Pedestrians',
'Children crossing', 'Bicycles crossing', 'Beware of ice/snow',
'Wild animals crossing', 'End of all speed and passing limits', 'Turn right ahead',
'Turn left ahead', 'Ahead only', 'Go straight or right', 'Go straight or left',
'Keep right', 'Keep left', 'Roundabout mandatory', 'End of no passing',
'End of no passing by vechiles over 3.5 metric tons']
image = image.resize((50, 50))
img = np.array(image)
img = img.reshape(1, img.shape[0], img.shape[1], img.shape[2])
prediction = loaded_model.predict(img)
idx = np.argmax(prediction[0])
probability = prediction[0][idx]
return classes[idx], probability*100, str(idx)
app = Flask(__name__)
app.secret_key = "secret key"
base_dir = os.path.dirname(os.path.abspath(__file__))
model = tf.keras.models.load_model(os.path.join(base_dir, 'my_model'))
app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'uploads')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/', methods=['GET', 'POST'])
def submit_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 file selected for uploading')
return redirect(request.url)
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
label, acc, idx = model_predict(filename, model)
img_path = os.path.join('..\static\images', idx+'.png')
return render_template('predict.html', predictions={'class': label,
'acc': acc}, img_path=img_path)
if __name__ == '__main__':
app.run()