# -*- coding: utf-8 -*-
"""
Created on Thu Feb  9 07:13:58 2023

@author: errobin
"""

import matplotlib.pyplot as plt
import tensorflow as tf
import random as ra
import numpy as np
import math


#lecture image ######################################
from PIL import Image #pour lire une image
image_file = Image.open("arbre.bmp") #mettre le nom de l'image qui doit etre ds le meme répertoire que le programme IMAQ_exercices.py
image_bmp = 1.*np.array(image_file) # on transforme le fichier en matrice nommée image_bmp
image_file.close()
#####################################################



# recupère les dimension de l'image ###############
dim=image_bmp.shape

dim_imageX=dim[0]
dim_imageY=dim[1]
image_traitee=np.zeros((dim_imageX,dim_imageY))
###################################################

X_train=np.zeros((dim_imageX*dim_imageY,2)) 
Y_target=np.zeros((dim_imageX*dim_imageY,1))

compt=0
for i in range(0,dim_imageX):
    for j in range(0,dim_imageY):

        X_train[compt,0]=2*i/(1*dim_imageX)-1
        X_train[compt,1]=2*j/(1*dim_imageY)-1
        
        
        Y_target[compt,0]=image_bmp[i,j]/255.
        compt=compt+1
        
################################################################### verification RESEAU NEURONES
loaded_model=tf.keras.models.load_model("arbre.h5")
#loaded_model=tf.keras.models.load_model("arbre_deep0.h5")
#loaded_model=tf.keras.models.load_model("arbre_deep1.h5")

tutu = loaded_model.predict(X_train)

tutu=np.reshape(tutu, (dim_imageX,dim_imageY))

# Affichage image ##################################
plt.figure()
plt.imshow(tutu)
plt.show()
####################################################

# TRAITEMENT IMAGE ################################
for i in range(0,dim_imageX):
    for j in range(0,dim_imageY):
        x=i
        y=j
        image_traitee[i,j]=image_bmp[int(x+0.5),int(y+0.5)]/(255.)

plt.figure()        
plt.imshow(image_traitee)
plt.show()
############

