""" This file aims to image processing an entire folder of pictures using OpenCV

Marjorie ROSCIAN M2 CCI 2017-2018 """

from scipy.spatial import procrustes
from Images.Refdictionary import *


def imProcess(foldname):
    # Define the folder path
    # foldname comes from the interface function loadpicture()
    # For test : foldame is created manually

    foldname = '/home/roscian/Bureau/roscian/Images/Test2'
    os.chdir(foldname)

    # Open all picutres in a folder
    # Does not work
    # images = ([cv2.imread(file) for file in glob.glob(foldname+"/*.tif")])

    # or :
    # image_list = []
    # images = glob.glob(foldname+"/*.jpeg")
    # for image in images:
    #    img = Image.open(image)
    #    image_list.append(img)
    # Does not work

    # Test : Opening manually
    img_test = cv2.imread('picture_25.tif', 0)
    img_test_2 = cv2.imread('picture_49.tif', 0)
    img_test_3 = cv2.imread('picture_54.tif', 0)
    img_test_4 = cv2.imread('picture_75.tif', 0)
    img_test_5 = cv2.imread('picture_78.tif', 0)
    img_test_6 = cv2.imread('picture_95.tif', 0)
    img_test_7 = cv2.imread('picture_104.tif', 0)
    img_test_8 = cv2.imread('picture_121.tif', 0)
    img_test_9 = cv2.imread('picture_149.tif', 0)
    img_test_10 = cv2.imread('picture_157.tif', 0)
    # img_test_11 = cv2.imread('picture_157.tif', 0)
    # img_test_12 = cv2.imread('picture_122.tif', 0)
    # img_test_13 = cv2.imread('picture_133.tif', 0)

    img = [img_test, img_test_2, img_test_3, img_test_4, img_test_5, img_test_6, img_test_7, img_test_8,
           img_test_9, img_test_10]
    img = np.asarray(img)

    # Find the chain code of the contour shape
    img_result = []
    for toto in np.arange(0, len(img)):
        img_result.append(np.asarray(FreemanChainCode(img[toto])))

    # From discrete data to curve
    img_result = np.asarray(img_result)
    img_curve = data_to_function(img_result, 21, 2)
    img_curve = np.asarray(img_curve)

    # Procrustes analysis for each picture
    # Comparison between the picture and all the referenced shapes
    # See Refdictionary.py
    ref = dic_ref()

    final_name = []
    for sample in np.arange(0, len(img_curve)):
        dist_proc = 10000
        for key, values in ref.items():
            proc_res = procrustes(img_curve[sample], values)
            if proc_res[2] < dist_proc:
                dist_proc = proc_res[2]
                name = key
            else:
                continue
        final_name.append(name)

    # Print
    fig, axs = plt.subplots(nrows=2, ncols=5, sharex=True)

    axs[0][0].set_title(final_name[4])
    axs[0][0].imshow(img_test_5)

    axs[0][1].set_title(final_name[2])
    axs[0][1].imshow(img_test_3)

    axs[0][2].set_title(final_name[0])
    axs[0][2].imshow(img_test)

    axs[0][3].set_title(final_name[3])
    axs[0][3].imshow(img_test_4)

    axs[0][4].set_title(final_name[5])
    axs[0][4].imshow(img_test_6)

    axs[1][0].set_title(final_name[1])
    axs[1][0].imshow(img_test_2)

    axs[1][1].set_title(final_name[7])
    axs[1][1].imshow(img_test_8)

    axs[1][2].set_title(final_name[8])
    axs[1][2].imshow(img_test_9)

    axs[1][3].set_title(final_name[9])
    axs[1][3].imshow(img_test_10)

    axs[1][4].set_title(final_name[6])
    axs[1][4].imshow(img_test_7)

    plt.show()