# This file is a collection of helper functions

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import sys
import os
from PIL import Image
from cache import cache
import json

from tensorflow.python.keras import backend as K
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Input, Dense, GRU, Embedding
from tensorflow.python.keras.applications import VGG16
from tensorflow.python.keras.optimizers import RMSprop
from tensorflow.python.keras.callbacks import ModelCheckpoint, TensorBoard
from tensorflow.python.keras.preprocessing.text import Tokenizer
from tensorflow.python.keras.preprocessing.sequence import pad_sequences

import __main__

# HELPER FUNCTIONS

# to load the json files
def load_json(filename):
    filename='dataset/'+filename+'.json'
    with open(filename,'r') as infile:
        data=json.load(infile)
        data=tuple(data)
        return data

# to load the image
def load_image(path, size=None):
    """
    Load the image from the given file-path and resize it
    to the given size if not None.
    """

    # Load the image using PIL.
#    ADD EXCEPTION FOR railway233, as it contains corrupted data
    if path=='../../../../Desktop/parsingDataset/RSICD_images/railwaystation_233.jpg':
        path='../../../../Desktop/parsingDataset/RSICD_images/railwaystation_234.jpg'

    img = Image.open(path)

    # Resize image if desired.
#    if not size is None:
#        img = img.resize(size=size, resample=Image.LANCZOS)

    # Convert image to numpy array.
    img = np.array(img)

    # Scale image-pixels so they fall between 0.0 and 1.0
    img = img / 255.0

    # Convert 2-dim gray-scale array to 3-dim RGB array.
    if (len(img.shape) == 2):
        img = np.repeat(img[:, :, np.newaxis], 3, axis=2)

    return img

# To print the progress
def print_progress(count, max_count):
    # Percentage completion.
    pct_complete = count / max_count

    # Status-message. Note the \r which means the line should
    # overwrite itself.
    msg = "\r- Progress: {0:.3%}".format(pct_complete)

    # Print it.
    sys.stdout.write(msg)
    sys.stdout.flush()
    