from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.xception import (
Xception, preprocess_input, decode_predictions)
from tensorflow.keras.models import load_model
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
model = load_model('xception.h5')
"""Use Xception to label image"""
img = image.load_img(image_path, target_size=image_size)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
predictions = model.predict(x)
result = decode_predictions(predictions, top=3)[0]
print('Predicted:', result)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
result = predict(filename)
strTable += '<td>' + str(r[1]) + '</td>'
strTable += '<td>' + str(r[2]) + '</td>'
strHTML = f'Successfully uploaded {filename} <BR><BR> Prediction results:<BR> {strTable}'
@app.route('/', methods=['GET', 'POST'])
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return classify(filename)
<title>Upload new File</title>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
if __name__ == "__main__":
No comments:
Post a Comment