import os
from flask import Flask, request, redirect, url_for, send_from_directory, render_template
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from tensorflow.keras.models import Sequential, load_model, model_from_json
from werkzeug.utils import secure_filename
import numpy as np
import datetime


## Extensions for image upload
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg', 'png'])
IMAGE_SIZE = (224, 224) ## Change according to model

UPLOAD_FOLDER = 'uploads'

## Load the default image
dummy_img_path = os.path.join('static','dummy.png')

## Read model weight files
json_file = open(os.path.join("model","denseNet169_comb.json"), 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
# load weights into new model
model.load_weights(os.path.join("model","denseNet169_comb_weights.h5"))


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

def minMaxNorm(img):
    return (img - np.min(img))/(np.max(img) - np.min(img))


def predict(file):
    img  = load_img(file, target_size=IMAGE_SIZE)
    img = img_to_array(img)
    img = minMaxNorm(img)
    # img = np.expand_dims(img, axis=0)
    img = img.reshape((1,224,224,3))
    probs = model.predict(img)[0]
    # output = 'Probability of Infection:   {:.2f}%'.format(probs[0]*100)#, 'Dog': probs[1]}
    output =  float("{:.2f}".format(probs[0]*100))
    # output = "<p style='color:blue;'>Test Output</p>"
    return output

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


@app.route("/")
def template_test():
    return render_template('home.html', label='', imagesource=dummy_img_path)

@app.route('/', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']

        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file_path = os.path.join(app.config['UPLOAD_FOLDER'], datetime.datetime.now().strftime("%H-%M-%S")+filename)
            file.save(file_path)
            output = predict(file_path)
    return render_template("home.html", label=output, imagesource=file_path)


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080, debug=False)

feda86c39fc28189c41ffb5f16993e69f8c18063
<mask token>


def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


<mask token>


def predict(file):
    img = load_img(file, target_size=IMAGE_SIZE)
    img = img_to_array(img)
    img = minMaxNorm(img)
    img = img.reshape((1, 224, 224, 3))
    probs = model.predict(img)[0]
    output = float('{:.2f}'.format(probs[0] * 100))
    return output


<mask token>


@app.route('/', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file_path = os.path.join(app.config['UPLOAD_FOLDER'], datetime.
                datetime.now().strftime('%H-%M-%S') + filename)
            file.save(file_path)
            output = predict(file_path)
    return render_template('home.html', label=output, imagesource=file_path)


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)


<mask token>

<mask token>


def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


def minMaxNorm(img):
    return (img - np.min(img)) / (np.max(img) - np.min(img))


def predict(file):
    img = load_img(file, target_size=IMAGE_SIZE)
    img = img_to_array(img)
    img = minMaxNorm(img)
    img = img.reshape((1, 224, 224, 3))
    probs = model.predict(img)[0]
    output = float('{:.2f}'.format(probs[0] * 100))
    return output


<mask token>


@app.route('/')
def template_test():
    return render_template('home.html', label='', imagesource=dummy_img_path)


@app.route('/', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file_path = os.path.join(app.config['UPLOAD_FOLDER'], datetime.
                datetime.now().strftime('%H-%M-%S') + filename)
            file.save(file_path)
            output = predict(file_path)
    return render_template('home.html', label=output, imagesource=file_path)


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)


<mask token>

<mask token>
json_file.close()
<mask token>
model.load_weights(os.path.join('model', 'denseNet169_comb_weights.h5'))


def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


def minMaxNorm(img):
    return (img - np.min(img)) / (np.max(img) - np.min(img))


def predict(file):
    img = load_img(file, target_size=IMAGE_SIZE)
    img = img_to_array(img)
    img = minMaxNorm(img)
    img = img.reshape((1, 224, 224, 3))
    probs = model.predict(img)[0]
    output = float('{:.2f}'.format(probs[0] * 100))
    return output


<mask token>


@app.route('/')
def template_test():
    return render_template('home.html', label='', imagesource=dummy_img_path)


@app.route('/', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file_path = os.path.join(app.config['UPLOAD_FOLDER'], datetime.
                datetime.now().strftime('%H-%M-%S') + filename)
            file.save(file_path)
            output = predict(file_path)
    return render_template('home.html', label=output, imagesource=file_path)


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=False)

<mask token>
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg', 'png'])
IMAGE_SIZE = 224, 224
UPLOAD_FOLDER = 'uploads'
dummy_img_path = os.path.join('static', 'dummy.png')
json_file = open(os.path.join('model', 'denseNet169_comb.json'), 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights(os.path.join('model', 'denseNet169_comb_weights.h5'))


def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


def minMaxNorm(img):
    return (img - np.min(img)) / (np.max(img) - np.min(img))


def predict(file):
    img = load_img(file, target_size=IMAGE_SIZE)
    img = img_to_array(img)
    img = minMaxNorm(img)
    img = img.reshape((1, 224, 224, 3))
    probs = model.predict(img)[0]
    output = float('{:.2f}'.format(probs[0] * 100))
    return output


app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


@app.route('/')
def template_test():
    return render_template('home.html', label='', imagesource=dummy_img_path)


@app.route('/', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file_path = os.path.join(app.config['UPLOAD_FOLDER'], datetime.
                datetime.now().strftime('%H-%M-%S') + filename)
            file.save(file_path)
            output = predict(file_path)
    return render_template('home.html', label=output, imagesource=file_path)


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=False)

import os
from flask import Flask, request, redirect, url_for, send_from_directory, render_template
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from tensorflow.keras.models import Sequential, load_model, model_from_json
from werkzeug.utils import secure_filename
import numpy as np
import datetime


## Extensions for image upload
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg', 'png'])
IMAGE_SIZE = (224, 224) ## Change according to model

UPLOAD_FOLDER = 'uploads'

## Load the default image
dummy_img_path = os.path.join('static','dummy.png')

## Read model weight files
json_file = open(os.path.join("model","denseNet169_comb.json"), 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
# load weights into new model
model.load_weights(os.path.join("model","denseNet169_comb_weights.h5"))


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

def minMaxNorm(img):
    return (img - np.min(img))/(np.max(img) - np.min(img))


def predict(file):
    img  = load_img(file, target_size=IMAGE_SIZE)
    img = img_to_array(img)
    img = minMaxNorm(img)
    # img = np.expand_dims(img, axis=0)
    img = img.reshape((1,224,224,3))
    probs = model.predict(img)[0]
    # output = 'Probability of Infection:   {:.2f}%'.format(probs[0]*100)#, 'Dog': probs[1]}
    output =  float("{:.2f}".format(probs[0]*100))
    # output = "<p style='color:blue;'>Test Output</p>"
    return output

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


@app.route("/")
def template_test():
    return render_template('home.html', label='', imagesource=dummy_img_path)

@app.route('/', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']

        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file_path = os.path.join(app.config['UPLOAD_FOLDER'], datetime.datetime.now().strftime("%H-%M-%S")+filename)
            file.save(file_path)
            output = predict(file_path)
    return render_template("home.html", label=output, imagesource=file_path)


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080, debug=False)
