import math
import random
import os

from werkzeug.utils import secure_filename
import numpy as np
from flask import Flask, request, jsonify, render_template, flash, redirect, url_for
from PIL import Image
#from urllib import request
from io import BytesIO
from keras_preprocessing import image
import urllib.request
from datetime import date
from keras.models import load_model
import csv

UPLOAD_FOLDER = ''
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}

# Create flask app
flask_app = Flask(__name__)
flask_app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
print("worr")
model1 = load_model('imgClassification.h5')
print("wk")


# wt = random.randint(380, 490)

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


@flask_app.route("/")
def Home():
    return render_template("client.html")


@flask_app.route("/classify", methods = ["POST"])
def classify():
    image_path = "https://static9.depositphotos.com/1628352/1107/i/600/depositphotos_11071200-stock-photo-cabbage.jpg"
    class_names = ['banana', 'cabbage', 'cucumber', 'mango', 'orange', 'tomato']
    img = Image.open(BytesIO(request.urlopen(image_path).read())).resize((224, 224))
    # img = image.load_img(request.urlopen(image_path), target_size=(224,224,3))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    images = np.vstack([x])
    pred = model1.predict(images, batch_size=6)
    label = np.argmax(pred, axis=1)
    out = class_names[np.argmax(pred)]
    return render_template("predi.html", prediction_text_class = out)

@flask_app.route("/price", methods = ["POST"])
def price():
    today = date.today()
    dt = today.strftime("%d/%m/%Y")
    image_path = "http://192.168.8.103/capture?_cb=1634477807414"
    wt = random.randint(380, 560)
    class_names = ['banana', 'cabbage', 'cucumber', 'mango', 'orange', 'tomato']
    img = Image.open(BytesIO(request.urlopen(image_path).read())).resize((224, 224))
    # img = image.load_img(request.urlopen(image_path), target_size=(224,224,3))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    images = np.vstack([x])
    pred = model1.predict(images, batch_size=6)
    label = np.argmax(pred, axis=1)
    out = class_names[np.argmax(pred)]


    with open('data/Predict_vegetable_2021_SL.csv', encoding="utf8") as f:
        csv_reader = csv.DictReader(f)
        next(csv_reader)

        for line in csv_reader:

            if line['date'] == dt and line['item'] == out:
                para = line['price']
                print(line['price'])


    return render_template("resultsPrice.html", vegita = out, dte = dt, price = para, weight = wt, esti = math.trunc(int(para) * int(wt) / 1000))


@flask_app.route("/priceC", methods = ["POST"])
def priceC():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)

            print(filename)
            image_path = filename
            print("workinggggg")
            today = date.today()
            wt = random.randint(380, 560)
            dt = today.strftime("%d/%m/%Y")
            class_names = ['banana', 'cabbage', 'cucumber', 'mango', 'orange', 'tomato']
            img = image.load_img(image_path, target_size=(224,224,3))
    # img = image.load_img(request.urlopen(image_path), target_size=(224,224,3))
            x = image.img_to_array(img)
            x = np.expand_dims(x, axis=0)
            images = np.vstack([x])
            pred = model1.predict(images, batch_size=6)
            label = np.argmax(pred, axis=1)
            out = class_names[np.argmax(pred)]


            with open('data/Predict_vegetable_2021_SL.csv', encoding="utf8") as f:
                csv_reader = csv.DictReader(f)
                next(csv_reader)

                for line in csv_reader:

                    if line['date'] == dt and line['item'] == out:
                        para = line['price']
                        print(line['price'])


            return render_template("resultsPrice.html", vegita = out, dte = dt, price = para, weight = wt, esti = math.trunc(int(para) * int(wt) / 1000))

if __name__ == "__main__":
    flask_app.run(debug=False)