from django.shortcuts import render
# Create your views here.

from django.core.files.storage import FileSystemStorage

from keras.models import load_model
from keras.preprocessing import image
import tensorflow as tf
import json
from tensorflow import Graph, Session

from django.views.generic import ListView, DetailView
from .models import FruitDatabase
from .filter import FruitFilter 

img_height, img_width=224,224
with open('./models/imagenet_classes.json','r') as f:
    labelInfo=f.read()

labelInfo=json.loads(labelInfo)


model_graph = Graph()
with model_graph.as_default():
    tf_session = Session()
    with tf_session.as_default():
        model=load_model('./models/MobileNetModelImagenet.h5')



def index(request):
    context={'a':1}
    return render(request,'index.html',context)



def predictImage(request):
    print (request)
    print (request.POST.dict())
    fileObj=request.FILES['filePath']
    fs=FileSystemStorage()
    filePathName=fs.save(fileObj.name,fileObj)
    filePathName=fs.url(filePathName)
    testimage='.'+filePathName
    img = image.load_img(testimage, target_size=(img_height, img_width))
    x = image.img_to_array(img)
    x=x/255
    x=x.reshape(1,img_height, img_width,3)
    with model_graph.as_default():
        with tf_session.as_default():
            predi=model.predict(x)

    import numpy as np
    global predictedLabel
    predictedLabel=labelInfo[str(np.argmax(predi[0]))]
    
    context={'filePathName':filePathName,'predictedLabel':predictedLabel[1]}
    return render(request,'index.html',context) 

def viewDataBase(request):
    import os
    listOfImages=os.listdir('./media/')
    listOfImagesPath=['./media/'+i for i in listOfImages]
    context={'listOfImagesPath':listOfImagesPath}
    return render(request,'viewDB.html',context) 

class InfoView(DetailView):
    model = FruitDatabase
    template_name = 'more-info.html'
    context_object_name = 'fruitdatabase'
    
    def get(self, request):
        predictedLabel = request.GET.get('predictedLabel', None)
        if manager:
            profiles_set = FruitDatabase.objects.filter(fruit_name=predictedLabel)
        else:
            profiles_set = FruitDatabase.objects.all()
            

# def search(request):
#     search = FruitDatabase.objects.all().filter(fruit_name = predictedLabel)
#     fruit_filter = FruitFilter(request.GET, queryset=search)
#     return render(request, 'users/fruit_filter.html', {'filter': fruit_filter})
