import sys, os, warnings
import argparse
import numpy as np
from PIL import Image

from keras.preprocessing import image
from keras.models import load_model
from keras.applications.inception_v3 import preprocess_input
from keras import applications

warnings.filterwarnings("ignore")


def get_class_names(class_file):
    with open(class_file) as f:
        class_names = f.read().splitlines()
    return class_names


def load_model_h5(h5_file):
    return load_model(h5_file)


def predict_image(model, class_names, img):
    target_size = (224, 224)  # fixed size for InceptionV3 architecture
    if img.size != target_size:
        img = img.resize(target_size)

    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    # print("Good until for now")
    preds = model.predict(x)
    # preds = model._make_predict_function(x)
    predictions = preds[0]
    # print(predictions)
    sort_order = np.argsort(-1 * predictions)
    # print(dict(zip(np.array(class_names)[sort_order].tolist(), np.array(predictions)[sort_order].tolist())))
    return dict(zip(np.array(class_names)[sort_order].tolist(), np.array(predictions)[sort_order].tolist()))
