# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/tiagosardi/emotionrecognition/blob/main/human_emotion_detection.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="wG91ueXbo_iP"
# !git clone  https://github.com/tiagosardi/emotionrecognition.git

# + id="NvbknSweQRAH"
# !pip install -q kaggle

# + id="m_b5mPd5Qr56"
from google.colab import files
files.upload()

# + id="tVd0bfL9PLAv"
# ! mkdir ~/.kaggle
# ! cp kaggle.json ~/.kaggle/

# + id="mVsRFaCzRSIJ"
# !chmod 600 ~/.kaggle/kaggle.json

# + id="OKWBZVHHRk3C"
# !kaggle datasets list

# + id="ff-rBJVJTK9R"
# !kaggle competitions download -c challenges-in-representation-learning-facial-expression-recognition-challenge

# + id="rWcD7op3LsZS"
# !unzip train.csv.zip -d train

# + id="Qqm7wGHNLu_w"
# !unzip test.csv.zip -d test

# + id="q35rMhnfQCiP"
# !tar -vzxf fer2013.tar.gz

# + id="_YMJ2L-9Tuj_"
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow

# + id="_xRYbz6bBNKP"
data = pd.read_csv('fer2013/fer2013.csv')

# + id="2w_junKmSsLv"
data.tail()

# + id="6epqK1x_SwH6"
plt.figure(figsize=(12,6))
plt.hist(data['emotion'], bins= 30)
plt.title('Img X emotions')

# + id="G2dOPU9iTHsH"
height, width = 48,48
faces = []
samples = 0
for pixel_sequence in  pixels:
  face = [int(pixel) for pixel in pixel_sequence.split(' ')]
  face = np.asarray(face).reshape(height,width)
  faces.append(face)

  if(samples<10):
    cv2_imshow(face)
  amostras+=1

# + id="o9OKwnL0QSQa"
X_train, X_test , y_train, y_test = train_test_split(faces, emocoes, test_size = .1, random_state = 42)
X_train, X_val ,y_train, y_val = train_test_split (X_train, y_train , test_size = .1, random_state = 41)



# + id="KYueDhDDEwNr"
print("Numero de imagens no conjunto de treinamento: ", len(X_train))
print("Numero de imagens no conjunto de teste: ", len(X_test))
print("Numero de imagens no conjunto de validacao: ", len(X_val))

# + id="SjA6H8T6FDAd"
np.save('mod_xtest' , X_test)
np.save('mod_ytest', y_test)

# + id="pRmlPucGFs5s"
num_features = 64
num_labels = 7
batch_size = 64
epochs = 100
width, height = 48,48


# + id="6MvcbmBDF-mS"
model.compile (loss= 'categorical_crossentropy',
               optimizer = Adam(lr=.001, beta_1=.9, beta_2= .999, epsilon=1e-7),
               metrics=['accuracy'])

arquivo_modelo = 'modelo_01_expressoes.h5'
arquivo_modelo_json = 'modelo_01_expressoes.json'
lr_reducer = ReduceLROnPlateau(monitor='val_loss', factor = .9, patience=3, verbose = 1)
early_stopper = EarlyStoppping(monitor='val_loss', min_delta=0,patience=8, verbose=1, mode='auto')
checkpointer = ModelCheckpoint(arquivo_modelo, monitor='val_loss', verbose=1, save_best_only=True)

# + id="VMbVb8bOJ4rQ"
history = model.fit(np.array(X_train), np.array(y_train),
                    batch_size = batch_size,
                    epochs = epochs,
                    verbose = 1,
                    validation_data = (np.array(X_val),np.array(y_val)),
                    shuffle = True,
                    callbacks = [lr_reducer, early_stopper, checkpointer])

StarcoderdataJupyterScriptsDedupFiltered