Skip to content
Snippets Groups Projects
Commit 456f0806 authored by Timothé Boulet's avatar Timothé Boulet :alien:
Browse files

add model

parent 0a66de78
Branches
No related tags found
No related merge requests found
No preview for this file type
File added
%% Cell type:code id: tags:
``` python
#@title Imports
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets, layers, models, losses
import tensorflow_datasets as tfds
#from google.colab import files
from matplotlib import image
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import random as rd
import cv2
import csv
```
%% Cell type:code id: tags:
``` python
#from google.colab import drive
#drive.mount('/content/drive')
```
%% Cell type:code id: tags:
``` python
#@title Hyperparamètres
classes = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Suprise", "Neutral"]
Na = len(classes)
N = len(X)
maxNbrImagesForEachClasses = float('inf')
h = 48
l = 48
p = 1
input_shape = (h, l, p)
epochs = 5
batch_size = 128
validation_size = 0.1
```
%% Cell type:code id: tags:
``` python
#@title Fonction utils
def afficher(image):
if len(image.shape) == 3:
if image.shape[2] == 3: # (h,l,3)
plt.imshow(image)
elif image.shape[2] == 1: # (h,l,1)->(h,l)
image2 = image
plt.imshow(tf.squeeze(image))
elif len(image.shape)== 2: # (h,l)
plt.imshow(image)
def predir():
pass
def normAndResize(image):
#For an array image of shape (a,b,c) or (a,b), transform it into (h,l,p). Also normalize it.
image = cv2.resize(image, dsize=(h,l), interpolation=cv2.INTER_CUBIC) #resize for h and l #
if len(image.shape) == 3 and p==1 and image.shape[2] != 1 : #if we want (h,l,3) -> (h,l,1) , we first transform it in to (h,l) (grey the image)
image = image.mean(2)
image = np.reshape(image, (h,l,p)) #restore third dimension
image = image.astype("float32")
image = image/255 #normalisation
return image
```
%% Cell type:code id: tags:
``` python
#@title Load data as array
nbrImages = 35887
maxNbrImages = 10
emotions = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Suprise", "Neutral"]
def traitement(a,b,c): #For testing
pass
# arr = strToArray(b)
# print(a)
# plt.imshow(arr)
# plt.show()
# pass
def strToArray(string): #Fer2013 provides images as string so it needs to be transformed
A = []
lenght = len(string)
i=0
nbr = ""
while i<lenght:
car = string[i]
if car != " ":
nbr += car
else:
A.append(int(nbr))
nbr = ""
i+=1
A.append(int(nbr))
A = np.array(A)
A = np.reshape(A, (48, 48))
return A
#LOAD DATA AS ARRAY
X = []
Y = []
filename = "/content/drive/MyDrive/Colab Notebooks/facial emotion recognition/fer2013.csv"
filename = "data/fer2013.csv"
with open(filename,'r',encoding='utf-8') as file:
csv_reader = csv.reader(file, delimiter=",")
next(csv_reader) #Passe la ligne de titre
i=0
for row in csv_reader:
i+=1
if i>maxNbrImages: break
emotionNbr, stringImage, typeImage = row
traitement(emotionNbr, stringImage, typeImage)
image = normAndResize(strToArray(stringImage))
X.append(image)
Y.append(emotionNbr)
print(f"Image {i} sur {nbrImages} chargée", end='\r')
X = np.array(X)
Y = np.array(Y)
Y = keras.utils.to_categorical(Y)
```
%% Output
%% Cell type:code id: tags:
``` python
#@title Visualisation
N=5
plt.figure()
for i in range(N):
k = rd.randrange(X.shape[0])
plt.subplot(1, N, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
#plt.imshow(x[k])
afficher(X[k])
plt.title(classes[np.argmax(Y[k])])
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
#Images et labels
print('X:', X.shape)
print('Y:', Y.shape)
```
%% Output
X: (10, 48, 48, 1)\nY: (10, 7)
%% Cell type:code id: tags:
``` python
#@title Modèle
class MyModel(keras.Model):
def __init__(self, input_shape):
super(MyModel, self).__init__()
self.conv2D1 = keras.layers.Conv2D(32, kernel_size = (3, 3), activation = 'relu', input_shape = input_shape)
self.conv2D2 = keras.layers.Conv2D(64, kernel_size = (3, 3), activation = 'relu')
self.conv2D3 = keras.layers.Conv2D(128, kernel_size = (3, 3), activation = 'relu')
self.maxPooling = keras.layers.MaxPooling2D(pool_size = 2)
self.flatten = keras.layers.Flatten()
self.Dense1 = keras.layers.Dense(64, activation = 'relu')
self.Dense2 = keras.layers.Dense(Na, activation = 'softmax')
def call(self, x):
y = self.conv2D1(x)
y = self.maxPooling(y)
y = self.conv2D2(y)
y = self.maxPooling(y)
y = self.conv2D3(y)
y = self.maxPooling(y)
y = self.flatten(y)
y = self.Dense1(y)
y = self.Dense2(y)
return y
def predir(self, monImage):
return self.predict(np.array([monImage]))[0,:]
def compile_o(self):
self.compile(optimizer = 'adam', loss=losses.categorical_crossentropy, metrics = ['accuracy'])
myModel = MyModel(input_shape)
myModel.compile_o()
```
%% Cell type:code id: tags:
``` python
theImage = X[0]
myModel(np.array([theImage]))
```
%% Output
<tf.Tensor: shape=(1, 7), dtype=float32, numpy=
array([[2.96738029e-01, 1.24476401e-05, 2.17953697e-01, 1.91844806e-01,
1.76624253e-01, 1.78373352e-06, 1.16824925e-01]], dtype=float32)>
%% Cell type:code id: tags:
``` python
#Entrainement
history = myModel.fit(X, Y, epochs=epochs, validation_split=0.05)
myModel.save('modeleTest')
```
%% Output
Epoch 1/5
1/1 [==============================] - 0s 98ms/step - loss: 1.6300 - accuracy: 0.2222 - val_loss: 1.3779 - val_accuracy: 0.0000e+00
Epoch 2/5
1/1 [==============================] - 0s 58ms/step - loss: 1.5696 - accuracy: 0.2222 - val_loss: 1.4577 - val_accuracy: 0.0000e+00
Epoch 3/5
1/1 [==============================] - 0s 60ms/step - loss: 1.5309 - accuracy: 0.2222 - val_loss: 1.5352 - val_accuracy: 0.0000e+00
Epoch 4/5
1/1 [==============================] - 0s 70ms/step - loss: 1.5018 - accuracy: 0.5556 - val_loss: 1.5440 - val_accuracy: 0.0000e+00
Epoch 5/5
1/1 [==============================] - 0s 59ms/step - loss: 1.4829 - accuracy: 0.4444 - val_loss: 1.4377 - val_accuracy: 1.0000
INFO:tensorflow:Assets written to: firstModel/assets
%% Cell type:code id: tags:
``` python
#Affichage de l'historique de l'apprentissage
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.legend()
plt.ylim([min(history.history['val_accuracy']+history.history['accuracy']), 1])
plt.show()
```
%% Output
INFO:tensorflow:Assets written to: modeleEntraine/assets
%% Cell type:code id: tags:
``` python
# myModel = keras.models.load_model("modeleTest")
# print(myModel.predict(np.array([theImage]))[0,:])
```
%% Output
[2.96738029e-01 1.24476401e-05 2.17953697e-01 1.91844806e-01
1.76624253e-01 1.78373352e-06 1.16824925e-01]
[6.6510475e-01 8.1453304e-04 5.7720199e-02 1.3529294e-04 2.1474548e-01
2.6716415e-03 5.8808159e-02]
%% Cell type:code id: tags:
``` python
```
cagnol.jpg

4.76 KiB

#Objective of this file is to analyse a face
import keras
import numpy as np
import cv2
from utils import *
emotions = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Suprise", "Neutral"]
input_shape = (48,48,1)
def detectEmotion(face):
return "Happy?"
\ No newline at end of file
#Return the most likely emotion there is on a 48x48x1 gray face
#input = 48
face = normAndResize(face, input_shape)
model = keras.models.load_model('firstModel') #Load our model
emotionVect = predir(model, face)
emotionNbr = np.argmax(emotionVect)
emotion = emotions[emotionNbr]
return emotion
\ No newline at end of file
File added
File added
File added
#File to process images
import cv2
import numpy as np
import faceAnalysis as fa
input_shape = (48,48,1)
def imageProcess(image):
#Objectives : detect faces, identify emotion associated on it, modify the image by framing faces and writing their emotions associated
......@@ -31,6 +33,7 @@ def imageProcess(image):
#Write emotion on the image
emotion = fa.detectEmotion(face_color)
print(emotion)
cv2.putText(image, emotion, (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
......@@ -51,3 +54,10 @@ def selectFace(image):
x,y,w,h = faces[0]
face = image[y:y+h, x:x+w]
return face
image = cv2.imread("cagnol.jpg", 1) #Load Cagnol colored image
imageProcess(image)
cv2.imshow("Cagnol", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
File added
File added
File added
import numpy as np
import cv2
def afficher(image):
if len(image.shape) == 3:
if image.shape[2] == 3: # (h,l,3)
......@@ -8,12 +11,14 @@ def afficher(image):
elif len(image.shape)== 2: # (h,l)
plt.imshow(image)
def predir():
pass
def predir(modele, image):
#Return output of image from modele
modele.predict(np.array([image]))[0,:]
def normAndResize(image):
def normAndResize(image, input_shape):
#For an array image of shape (a,b,c) or (a,b), transform it into (h,l,p). Also normalize it.
h,l,p = input_shape
image = cv2.resize(image, dsize=(h,l), interpolation=cv2.INTER_CUBIC) #resize for h and l #
if len(image.shape) == 3 and p==1 and image.shape[2] != 1 : #if we want (h,l,3) -> (h,l,1) , we first transform it in to (h,l) (grey the image)
image = image.mean(2)
......
......@@ -2,7 +2,7 @@
import cv2
import imageProcess as ip
cap = cv2.VideoCapture(-1) #0 means we capture the first camera, your webcam probably
cap = cv2.VideoCapture(0) #0 means we capture the first camera, your webcam probably
while cap.isOpened():
ret, frame = cap.read() #Read next video frame, stop if frame not well read
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment