from sklearn.utils import shuffle
import numpy as np
import os
import cv2
import utils


def read_image(path):
    image = cv2.imread(path)
    try:
        image = cv2.resize(image, (utils.IMAGE_WIDTH, utils.IMAGE_HEIGHT))
        normalizer_image = image / 255.0 - 0.5
        return normalizer_image
    except:
        print(path)


def read_flowers(training=True):
    flowers_dir = "./flowers_images/"

    if training:
        tao = flowers_dir + "training/tao/"
        mei = flowers_dir + "training/mei/"
        mudan = flowers_dir + "training/mudan/"
        qianniu = flowers_dir + "training/qianniu/"
        meigui = flowers_dir + "training/meigui/"
        moli = flowers_dir + "training/moli/"
        pugongying = flowers_dir + "training/pugongying/"
    else:
        tao = flowers_dir + "test/tao/"
        mei = flowers_dir + "test/mei/"
        mudan = flowers_dir + "test/mudan/"
        qianniu = flowers_dir + "test/qianniu/"
        meigui = flowers_dir + "test/meigui/"
        moli = flowers_dir + "test/moli/"
        pugongying = flowers_dir + "test/pugongying/"

    images = []
    labels = []
    image_name = []

    for f in os.listdir(tao):
        images.append(read_image(tao + f))
        labels.append(utils.TAO)
        image_name.append(f)

    for f in os.listdir(mei):
        images.append(read_image(mei + f))
        labels.append(utils.MEI)
        image_name.append(f)

    for f in os.listdir(mudan):
        images.append(read_image(mudan + f))
        labels.append(utils.MUDAN)
        image_name.append(f)

    for f in os.listdir(qianniu):
        images.append(read_image(qianniu + f))
        labels.append(utils.QIANNIU)
        image_name.append(f)

    for f in os.listdir(meigui):
        images.append(read_image(meigui + f))
        labels.append(utils.MEIGUI)
        image_name.append(f)

    for f in os.listdir(moli):
        images.append(read_image(moli + f))
        labels.append(utils.MOLI)
        image_name.append(f)

    for f in os.listdir(pugongying):
        images.append(read_image(pugongying + f))
        labels.append(utils.PUGONGYING)
        image_name.append(f)

    return shuffle(np.array(images), np.array(labels), np.array(image_name))
