The following code gives the error UnboundLocalError: local variable 'predictions' referenced before assignment
Edit: I add prediction = "" It not error! But when I choose file image. It not working and not showing on web app. What should I do?
app.py
from flask import Flask, render_template, request, redirect
import tensorflow as tf
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.models import model_from_json
from tensorflow.keras.preprocessing.image import img_to_array
import numpy as np
from PIL import Image
from io import BytesIO
from keras.models import load_model
model = None
app = Flask(__name__)
model = load_model('model.h5')
def process_image(image):
image = Image.open(BytesIO(image))
if image.mode != "RGB":
image = image.convert("RGB")
image = image.resize((224, 224))
image = img_to_array(image)
image = preprocess_input(image)
image = np.expand_dims(image, axis = 0)
return image
@app.route("/submit", methods=['GET', 'POST'])
def get_output():
if request.method == "POST":
if request.files:
try:
image = request.files["image"].read()
image = process_image(image)
out = model.predict(image)
if np.round(out[0][1], 2) > np.round(out[0][0], 2):
predictions = "Normal have an accuracy of " + str(np.round(out[0][1]*100, 2)) + "%"
else:
predictions = "Pneumonia have an accuracy of " + str(np.round(out[0][0]*100, 2)) + "%"
except:
redirect('/submit')
return render_template("Analysis.html", predictions = predictions)
if __name__ == "__main__":
app.run(debug = True, threaded = False)
I'm having troubles with flask. I get this error
What can I do?
html
<section class="upload">
<form id = "upload" action="/submit" method="POST" enctype="multipart/form-data">
<center style="margin-top: 1rem;">
<div class="form-group">
<input type="file" id="image" name="image" onchange="loadFile(event)">
</div>
<div class="form-group">
<button type="submit" id ="predict" class="btn btn-primary" style="display: none; margin-top: 1rem;">Predict Image</button>
</div>
</center>
</form>
</section>
<center style="margin-top: 1rem;">
<section class="Predictions">
<div>
{% if predictions %}
<div class="img-preview">
<p><img id="output" style="width: 100%; height: 480px;" /></p>
</div>
<button type="button" class="btn btn-dark" style="font-size: 20px; margin-top: 1rem;">{{predictions}}</button>
{% endif %}
</div>
</section>
<script>
window.onload = function() {
let profileImage = localStorage.getItem("profileImageData");
if (profileImage !== null) {
document.getElementById("output").src = profileImage
}
}
const loadFile = function(event) {
document.getElementById('predict').style.display = 'block'
let image = document.getElementById('output');
image.src = URL.createObjectURL(event.target.files[0]);
};
document.getElementById("image").addEventListener('change', function() {
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
localStorage.setItem("profileImageData", reader.result);
};
} );
</script>
</center>
How can I fix this?