import tensorflow
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.text import text_to_word_sequence
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from keras.layers import Input
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Embedding, LSTM, Concatenate
from keras.models import Model
import os
from google.colab import drive
drive.mount('/content/drive')
!unzip /content/drive/MyDrive/DATACOLAB/ImagesPart1.zip
!unzip -q /content/drive/MyDrive/DATACOLAB/ImagesPart2.zip -d ImagesPart1
!mkdir AKRAPO7
!unzip -q /content/drive/MyDrive/DATACOLAB/train_gt_t13.zip -d AKRAPO7

os.system("clear")
import os
import shutil

dossier_source = '/content/ImagesPart1/ImagesPart2'
dossier_destination = '/content/ImagesPart1'

if os.path.exists(dossier_source):
    for fichier in os.listdir(dossier_source):
        chemin_source = os.path.join(dossier_source, fichier)
        chemin_destination = os.path.join(dossier_destination, fichier)

        try:
            shutil.move(chemin_source, chemin_destination)
        except FileNotFoundError:
            print(f"Le fichier {chemin_source} n'a pas été trouvé.")
        except shutil.Error as e:
            print(f"Erreur lors du déplacement du fichier {chemin_source} : {str(e)}")
        except Exception as e:
            print(f"Une erreur inattendue s'est produite lors du déplacement du fichier {chemin_source} : {str(e)}")

    try:
        os.rmdir(dossier_source)
    except OSError as e:
        print(f"Erreur lors de la suppression du dossier {dossier_source} : {str(e)}")
    except Exception as e:
        print(f"Une erreur inattendue s'est produite lors de la suppression du dossier {dossier_source} : {str(e)}")
else:
    print(f"Le dossier source {dossier_source} n'existe pas.")
import os
import shutil

def separate_images_by_language(source_folder):
    # Dictionnaire des plages d'index et des noms de dossiers correspondants
    language_ranges = {
        (1, 1000): "Arabic",
        (1001, 2000): "English",
        (2001, 3000): "French",
        (3001, 4000): "Chinese",
        (4001, 5000): "German",
        (5001, 6000): "Korean",
        (6001, 7000): "Japanese",
        (7001, 8000): "Italian",
        (8001, 9000): "Bangla",
        (9001, 10000): "Hindi"
    }

    # Extensions de fichiers d'images prises en charge
    image_extensions = (".jpg", ".jpeg", ".png", ".gif",".txt")

    # Parcourir les fichiers dans le dossier source
    for filename in os.listdir(source_folder):
        if filename.lower().endswith(image_extensions):
            # Extraire le numéro d'index de l'image à partir du nom de fichier
            index = int(filename.split("_")[2].split(".")[0])

            # Trouver le dossier de destination correspondant à l'index
            for range_, folder_name in language_ranges.items():
                if range_[0] <= index <= range_[1]:
                    destination_folder = os.path.join(source_folder, folder_name)
                    break

            # Créer le dossier de destination s'il n'existe pas
            os.makedirs(destination_folder, exist_ok=True)

            # Déplacer l'image vers le dossier de destination
            source_path = os.path.join(source_folder, filename)
            destination_path = os.path.join(destination_folder, filename)
            shutil.move(source_path, destination_path)

# Exemple d'utilisation
source_folder = "ImagesPart1"
separate_images_by_language(source_folder)
source_folder = "AKRAPO7"
separate_images_by_language(source_folder)
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
import os

# Définition de la taille du batch
batch_size = 32

# Chargement des données d'images
train_datagen = ImageDataGenerator(rescale=1./255)
x_train = train_datagen.flow_from_directory(
    "/content/ImagesPart1/French",
    target_size=(224, 224),
    batch_size=batch_size,
    class_mode=None,
    shuffle=False  # Ajout de shuffle=False pour garder l'ordre des fichiers
)

# Chargement des données de texte
y_train = []
for root, dirs, files in os.walk("/content/AKRAPO7/French/"):
    for file in sorted(files):  # Tri des fichiers pour correspondre à l'ordre des images
        if file.endswith(".txt"):
            file_path = os.path.join(root, file)
            with open(file_path, "r") as f:
                text = f.read()
            y_train.append(text)

# Tokenisation et encodage des textes de sortie
tokenizer = Tokenizer()
tokenizer.fit_on_texts(y_train)
y_train_sequences = tokenizer.texts_to_sequences(y_train)
y_train_padded = pad_sequences(y_train_sequences)
y_train_categorical = to_categorical(y_train_padded, num_classes=len(tokenizer.word_index) + 1)
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
import os

# Définition de la taille du batch
batch_size = 32

# Langue sélectionnée
language = "French"

# Chargement des données d'images
image_dir = f"/content/ImagesPart1/{language}"
if not os.path.exists(image_dir):
    print(f"Le dossier d'images pour la langue {language} n'existe pas : {image_dir}")
    exit()

train_datagen = ImageDataGenerator(rescale=1./255)
x_train = train_datagen.flow_from_directory(
    image_dir,
    target_size=(224, 224),
    batch_size=batch_size,
    class_mode=None,
    shuffle=False  # Ajout de shuffle=False pour garder l'ordre des fichiers
)

# Chargement des données de texte
text_dir = f"/content/AKRAPO7/{language}/"
if not os.path.exists(text_dir):
    print(f"Le dossier de textes pour la langue {language} n'existe pas : {text_dir}")
    exit()

y_train = []
for root, dirs, files in os.walk(text_dir):
    for file in sorted(files):  # Tri des fichiers pour correspondre à l'ordre des images
        if file.endswith(".txt"):
            file_path = os.path.join(root, file)
            with open(file_path, "r") as f:
                text = f.read()
            y_train.append(text)

# Tokenisation et encodage des textes de sortie
tokenizer = Tokenizer()
tokenizer.fit_on_texts(y_train)
y_train_sequences = tokenizer.texts_to_sequences(y_train)
y_train_padded = pad_sequences(y_train_sequences)
y_train_categorical = to_categorical(y_train_padded, num_classes=len(tokenizer.word_index) + 1)

# Séparer les images par langue
def separate_images_by_language(source_folder):
    language_ranges = {
        (1, 1000): "Arabic",
        (1001, 2000): "English",
        (2001, 3000): "French",
        (3001, 4000): "Chinese",
        (4001, 5000): "German",
        (5001, 6000): "Korean",
        (6001, 7000): "Japanese",
        (7001, 8000): "Italian",
        (8001, 9000): "Bangla",
        (9001, 10000): "Hindi"
    }

    image_extensions = (".jpg", ".jpeg", ".png", ".gif", ".txt")

    for filename in os.listdir(source_folder):
        if filename.lower().endswith(image_extensions):
            index = int(filename.split("_")[2].split(".")[0])

            for range_, folder_name in language_ranges.items():
                if range_[0] <= index <= range_[1]:
                    destination_folder = os.path.join(source_folder, folder_name)
                    break

            os.makedirs(destination_folder, exist_ok=True)

            source_path = os.path.join(source_folder, filename)
            destination_path = os.path.join(destination_folder, filename)
            os.rename(source_path, destination_path)

separate_images_by_language("ImagesPart1")
separate_images_by_language("AKRAPO7")

# Définir les paramètres
img_width, img_height = 224, 224
batch_size = 32
epochs = 10

# Charger les données d'images
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
    '/content/ImagesPart1',
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode=None
)

# Charger les données de texte
def load_text_data(directory):
    text_data = []
    for root, dirs, files in os.walk(directory):
        for file in sorted(files):
            if file.endswith(".txt"):
                file_path = os.path.join(root, file)
                with open(file_path, "r") as f:
                    text = f.read()
                    text_data.append(text)
    return text_data

text_data = load_text_data('/content/AKRAPO7')

# Tokeniser et encoder les textes
tokenizer = Tokenizer()
tokenizer.fit_on_texts(text_data)
sequences = tokenizer.texts_to_sequences(text_data)
max_length = max([len(seq) for seq in sequences])
padded_sequences = pad_sequences(sequences, maxlen=max_length, padding='post')


image_input = Input(shape=(img_width, img_height, 3))
text_input = Input(shape=(max_length,))

# Branche du modèle d'image
x = Conv2D(32, (3, 3), activation='relu')(image_input)
x = MaxPooling2D((2, 2))(x)
x = Conv2D(64, (3, 3), activation='relu')(x)
x = MaxPooling2D((2, 2))(x)
x = Conv2D(64, (3, 3), activation='relu')(x)
x = Flatten()(x)
x = Dense(64, activation='relu')(x)

# Branche du modèle de texte
y = Embedding(len(tokenizer.word_index) + 1, 128)(text_input)
y = LSTM(64)(y)

# Fusionner les branches d'image et de texte
merged = Concatenate()([x, y])
output = Dense(len(tokenizer.word_index) + 1, activation='softmax')(merged)

# Créer le modèle
model = Model(inputs=[image_input, text_input], outputs=output)

# Compiler le modèle
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Entraîner le modèle
model.fit(
    [train_generator, padded_sequences],
    np.expand_dims(padded_sequences, -1),
    epochs=epochs,
    steps_per_epoch=train_generator.samples // batch_size
)

# Sauvegarder le modèle
model.save('model.h5')

Found 9996 images belonging to 10 classes.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-31-37f17f3d180f> in <cell line: 99>()
     97 
     98 # Entraîner le modèle
---> 99 model.fit(
    100     [train_generator, padded_sequences],
    101     np.expand_dims(padded_sequences, -1),

1 frames
/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs)
     68             # To get the full stack trace, call:
     69             # `tf.debugging.disable_traceback_filtering()`
---> 70             raise e.with_traceback(filtered_tb) from None
     71         finally:
     72             del filtered_tb

/usr/local/lib/python3.10/dist-packages/keras/src/engine/data_adapter.py in select_data_adapter(x, y)
   1103     if not adapter_cls:
   1104         # TODO(scottzhu): This should be a less implementation-specific error.
-> 1105         raise ValueError(
   1106             "Failed to find data adapter that can handle input: {}, {}".format(
   1107                 _type_name(x), _type_name(y)

ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'numpy.ndarray'>", "<class 'keras.src.preprocessing.image.DirectoryIterator'>"}), <class 'numpy.ndarray'>