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

clarification, debut code du notebook

parent 7651a8bd
Branches master
No related tags found
No related merge requests found
File added
No preview for this file type
No preview for this file type
No preview for this file type
This diff is collapsed.
This diff is collapsed.
import os
import cv2
from utils import *
import imageProcess as ip
from config import input_shape
def extractDataFromVideo_(filename, videoName, facesList, labelsList, maxNbrImages):
# Extract every faces in a specified video and add it to a list of faces as well as labels corresponding.
frameRate = 1/15
emotions = ["Neutral", "Angry", "Disgust", "Fear", "Happy", "Sad", "Suprise"]
# Start capture of a video and reading of a label file
cap = cv2.VideoCapture(filename)
if (cap.isOpened() == False):
print("Error opening video")
file = open("data/affwild/labels/"+videoName[:-4]+'.txt', 'r')
file.readline()
# Read until video is completed
k = 0
while (cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
line = file.readline()
if ret == True:
k += 1
if k*frameRate >= 1: # Read a frame each N frames where N=1/frameRate
k = 0
# Load image and labels
#Detect faces on the image
newFaces = ip.imageProcess(frame, writeEmotion=False)
#If 2 faces were detected, it means an error was made since there is only single-person videos here.
if len(newFaces) == 1:
facesList += newFaces
emotionNbr = emotionToNumber(emotions[int(line[0])])
labelsList.append(emotionNbr)
print("Donnée ajoutée, donnée :", len(facesList))
elif True: print("Erreur pour la donnée : Aucun ou plusieurs visages détectés")
# If we overreach the maximum number of images desired, stop
if len(facesList) > maxNbrImages:
break
# Press Q on keyboard to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
if False:
cv2.imshow('AffWild data extraction...', frame)
# Break the loop
else:
break
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
#Close file
file.close()
# Return face and label lists with new datas
return facesList, labelsList
# LOAD DATA
def loadAffwildData(maxNbrImages=10000000000):
print(f"\nCHARGEMENT DE {maxNbrImages} DONNEES DEPUIS AFFWILD...")
foldername = "data/affwild/videos/"
facesList = []
labelsList = []
k = 1
nbrOfVideos = len(os.listdir(foldername))
# For each video...
for videoName in os.listdir(foldername):
# If we overreach the maximum number of images desired, stop
if len(facesList) >= maxNbrImages:
break
elif videoName+'_left' in os.listdir("data/affwild/labels") or videoName+'_right' in os.listdir("data/affwild/labels"):
print("Vidéo à deux visages, non pris en compte")
else:
k+=1
print(f"Traitement de {videoName}, video {k}/{nbrOfVideos}")
filename = foldername+videoName
# Press Q on keyboard to exit ONE video
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#Add datas extracted from the specified video to features and labels
facesList, labelsList = extractDataFromVideo_(
filename, videoName, facesList, labelsList, maxNbrImages)
# List of colored images N*M*3 faces to array of gray images 48*48*1
N = len(facesList)
print(f"TRAITEMENT AFFWILD: traitement des {N} visages détectés sur les vidéos de AffWild...")
for k in range(N):
visage = facesList[k]
facesList[k] = normAndResize(visage, input_shape)
X = np.array(facesList)
Y = np.array(labelsList)
print(N, "données chargées depuis AffWild.")
return X, Y
import os
import cv2
from utils import *
import imageProcess as ip
from config import input_shape
def extractDataFromVideo_(filename, videoName, facesList, labelsList, maxNbrImages, frameRate):
# Extract every faces in a specified video and add it to a list of faces as well as labels corresponding.
emotions = ["Neutral", "Angry", "Disgust",
"Fear", "Happy", "Sad", "Suprise"]
# Start capture of a video and reading of a label file
cap = cv2.VideoCapture(filename)
if (cap.isOpened() == False):
print("Error opening video")
file = open("data/affwild/labels/"+videoName[:-4]+'.txt', 'r')
file.readline()
# Read until video is completed
k = 0
while (cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
line = file.readline()
if ret == True:
k += 1
if k*frameRate >= 1: # Read a frame each N frames where N=1/frameRate
k = 0
# Load image and labels
# Detect faces on the image
newFaces = ip.imageProcess(frame, writeEmotion=False)
# If 2 faces were detected, it means an error was made since there is only single-person videos here.
if len(newFaces) == 1:
facesList += newFaces
emotionNbr = emotionToNumber(emotions[int(line[0])])
labelsList.append(emotionNbr)
elif False:
print(
"Erreur pour la donnée : Aucun ou plusieurs visages détectés", end='\r')
# If we overreach the maximum number of images desired, stop
if len(facesList) > maxNbrImages:
break
# Press Q on keyboard to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
if False:
cv2.imshow('AffWild data extraction...', frame)
# Break the loop
else:
break
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
# Close file
file.close()
# Return face and label lists with new datas
return facesList, labelsList
# LOAD DATA
def loadAffwildData(maxNbrImages=10000000000, frameRate=1/20):
print(f"\nCHARGEMENT DE {maxNbrImages} DONNEES DEPUIS AFFWILD...")
foldername = "data/affwild/videos/"
facesList = []
labelsList = []
maxNbrImages -= 1
k = 0
nbrOfVideos = len(os.listdir(foldername))
# For each video...
for videoName in os.listdir(foldername):
# If we overreach the maximum number of images desired, stop
if len(facesList) >= maxNbrImages:
break
elif videoName+'_left' in os.listdir("data/affwild/labels") or videoName+'_right' in os.listdir("data/affwild/labels"):
print("Vidéo à deux visages, non pris en compte")
else:
k += 1
print(f"Traitement de {videoName}, video {k}/{nbrOfVideos}")
filename = foldername+videoName
# Press Q on keyboard to exit ONE video
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Add datas extracted from the specified video to features and labels
facesList, labelsList = extractDataFromVideo_(
filename, videoName, facesList, labelsList, maxNbrImages, frameRate)
# List of colored images N*M*3 faces to array of gray images 48*48*1
N = len(facesList)
print(
f"TRAITEMENT AFFWILD: traitement des {N} visages détectés sur les vidéos de AffWild...")
for k in range(N):
visage = facesList[k]
facesList[k] = normAndResize(visage, input_shape)
X = np.array(facesList)
Y = np.array(labelsList)
print(N, "données chargées depuis AffWild.")
return X, Y
......@@ -3,6 +3,8 @@ import csv
import numpy as np
import cv2
import matplotlib.pyplot as plt
from config import input_shape
from utils import *
def strToArray(string): # Fer2013 provides images as string so it needs to be transformed
......@@ -23,7 +25,7 @@ def strToArray(string): # Fer2013 provides images as string so it needs to be t
A.append(int(nbr))
A = np.array(A)
A = np.reshape(A, (48, 48))
A = np.reshape(A, input_shape)
return A
......@@ -33,7 +35,7 @@ def strToArray(string): # Fer2013 provides images as string so it needs to be t
def loadFer2013Data(maxNbrImages=35887):
print(f"\nCHARGEMENT DE {maxNbrImages} DONNEES DEPUIS FER2013 ...")
nbrImagesFer2013 = 35887
maxNbrImages = min(maxNbrImages, 35887)
filename = "data/fer2013.csv"
emotions = ["Angry", "Disgust", "Fear",
"Happy", "Sad", "Suprise", "Neutral"]
......@@ -54,7 +56,7 @@ def loadFer2013Data(maxNbrImages=35887):
emotionNbr, stringImage, typeImage = row
X.append(strToArray(stringImage))
X.append(normAndResize(strToArray(stringImage), input_shape))
Y.append(emotionNbr)
print(f"Donnée {i} sur {maxNbrImages} chargée", end='\r')
......
......@@ -21,6 +21,9 @@ def extractDataFromVideo(filename, videoName, facesList, labelsList, maxNbrImage
ret, frame = cap.read()
if ret == True:
k += 1
# If we overreach the maximum number of images desired, stop
if len(facesList) > maxNbrImages:
break
if k*frameRate >= 1: # Read a frame each N frames where N=1/frameRate
k = 0
......@@ -39,18 +42,15 @@ def extractDataFromVideo(filename, videoName, facesList, labelsList, maxNbrImage
if len(newFaces) == 1:
facesList += newFaces
labelsList.append(emotionNbr)
elif True: print("Erreur pour la donnée : Aucun ou plusieurs visages détectés")
# If we overreach the maximum number of images desired, stop
if len(facesList) > maxNbrImages:
break
elif True:
print("Erreur pour la donnée : Aucun ou plusieurs visages détectés")
# Press Q on keyboard to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
if True:
if False:
cv2.imshow('Frame', frame)
# Break the loop
......@@ -77,6 +77,7 @@ def loadRavdessData(maxNbrImages=10000000000):
"Sad", "Angry", "Fear", "Disgust", "Suprise"]
facesList = []
labelsList = []
maxNbrImages -= 1
# For each actor...
for actorName in os.listdir(foldername):
......@@ -110,7 +111,8 @@ def loadRavdessData(maxNbrImages=10000000000):
# List of colored images N*M*3 faces to array of gray images 48*48*1
N = len(facesList)
print(f"TRAITEMENT RAVDESS: traitement des {N} visages détectés sur les vidéos de Ravdess...")
print(
f"TRAITEMENT RAVDESS: traitement des {N} visages détectés sur les vidéos de Ravdess...")
for k in range(N):
visage = facesList[k]
......
......@@ -21,7 +21,7 @@ from loadExpWDS import *
from loadAffwild import *
from utils import *
# X, Y = loadFer2013Data(10)
# W, Z = loadRavdessData(10)
# A, B = loadExpWData(10)
C, D = loadAffwildData(1000)
\ No newline at end of file
X, Y = loadFer2013Data(10)
W, Z = loadRavdessData(10)
A, B = loadExpWData(10)
C, D = loadAffwildData(10)
\ No newline at end of file
......@@ -2,6 +2,8 @@ import numpy as np
import cv2
import matplotlib.pyplot as plt
from config import emotions
import tensorflow as tf
def afficher(image):
if len(image.shape) == 3:
......@@ -34,6 +36,8 @@ def normAndResize(image, input_shape):
return image
def emotionToNumber(emotion):
emotions = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Suprise", "Neutral"]
emotions = ["Angry", "Disgust", "Fear",
"Happy", "Sad", "Suprise", "Neutral"]
return emotions.index(emotion)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment