Homework: KNN for facial emotions recognition
- quentinaudy
- 23 nov. 2022
- 2 min de lecture
In this article, we will try to perform facial emotions recognition, using KNN. The principle is that we compare one image with the others, and give the class of the nearest neighbours. Here, we compare to the K nearest neighbours, and choose the most represented class. More informations are available in the first AI Lab. I will comment the different parts of the code, with the results obtained. I worked with Google Colab to use the ipynb file that we were given.
First, we import the different modules used to collect the data. We link our drive to use the dataset (I used the original FER dataset that I already downloaded for another facial emotion recognition project). These data are stored into folders that are the emotions. Finally we fill one list with the emotions, and different lists composed of the images, spread into the right folders. Here is the code:

Now that we filled the different lists, we can show the sizes of the data, and the repartition (the number of pictures in each folder):

We are now able to watch some examples of images (creating batches of 5 pictures in each emotion folder). We can see for example, emotions like sadness or happiness here.

Now, we can compute local binary patterns, in order to have a great KNN recognition process. With an example, we can have this view:



Now, we can create data frames, remove the null rows and adding zeros, and finally use the KNN function to have at the end a list with zeros and one 1 for the predicted class. Here is the code:

We can do exactly the same for the testing part. Finally, we can compute the final accuracy with the testing batch. We can obtain around 34% of accuracy, which is better than just random guessing (1/7 which is around 14% accuracy).

Another solution to improve the score would be to use Convolutional Neural Networks. With this dataset, we can obtain a score around 65%. But to have more details about that, you can see the article about my PFE.


Commentaires