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

add loaders

parent 1665a5a3
No related branches found
No related tags found
No related merge requests found
No preview for this file type
File added
File added
No preview for this file type
No preview for this file type
......@@ -29,11 +29,6 @@ def imageProcess(image, writeEmotion=True):
face_color = image[y:y+h, x:x+w]
facesList.append(face_color)
#Detect eyes on the face, create green rectangle
eyes = eye_cascade.detectMultiScale(face_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(face_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)
#Write emotion on the image
if writeEmotion:
emotion = fa.detectEmotion(face_color)
......
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 cv2
from utils import *
from config import input_shape
import imageProcess as ip
import numpy as np
def loadExpWData(nbrMaxImages=float('inf'), onlyDetected=False):
print(f"\nCHARGEMENT DE {nbrMaxImages} DONNEES DEPUIS EXPW...")
folderImages = 'data/expW/images/'
fileLabels = 'data/expW/labels.lst'
file = open(fileLabels, 'r')
nbrImages = 0
k = 0
X = []
Y = []
for line in file:
if nbrImages>=nbrMaxImages: break
k+= 1
#Face extraction, according to the dataset annotations AND the face detector (cascade)
imageName, Id, top, left, right, bottom, cofidence, label = line.strip().split(' ')
image = cv2.imread(folderImages+imageName)
faceAccordingToDS = image[int(top):int(bottom), int(left):int(right)]
facesDetected = ip.imageProcess(faceAccordingToDS, writeEmotion=False)
#Suivi visuel (facultatif, fait un peu peur sans attendre 1000ms entre deux images...)
if False:
cv2.imshow("ExpW importation...", faceAccordingToDS)
if cv2.waitKey(1000) & 0xFF == ord('q'):
break
#Add extracted data to our dataset
if len(facesDetected) == 1 or not onlyDetected: #Otherwise no face were detected or a no-face was detected as face
#Select in priority image detected by detector
if len(facesDetected) != 0:
face = facesDetected[0]
else:
face = faceAccordingToDS
#Colored N*M*3 face to gray 48*48*1 image.
gray = normAndResize(face, input_shape)
X.append(gray)
Y.append(label) #Emotion order is the same as fer2013.
nbrImages += 1
else: print("Erreur pour la donnée : Aucun ou plusieurs visages détectés")
print(f"{nbrImages} données chargées depuis expW (sur {k} données traités).\n")
X = np.array(X)
Y = np.array(Y)
return X, Y
......@@ -31,7 +31,8 @@ def strToArray(string): # Fer2013 provides images as string so it needs to be t
# LOAD DATA AS ARRAY
def loadFer2013Data(maxNbrImages=35887):
c = 0
print(f"\nCHARGEMENT DE {maxNbrImages} DONNEES DEPUIS FER2013 ...")
nbrImagesFer2013 = 35887
filename = "data/fer2013.csv"
emotions = ["Angry", "Disgust", "Fear",
......@@ -56,8 +57,9 @@ def loadFer2013Data(maxNbrImages=35887):
X.append(strToArray(stringImage))
Y.append(emotionNbr)
print(f"Image {i} sur {maxNbrImages} chargée")
print(f"Donnée {i} sur {maxNbrImages} chargée", end='\r')
X = np.array(X)
Y = np.array(Y)
print(f"{maxNbrImages} données chargées depuis fer2013.")
return X, Y
......@@ -5,11 +5,10 @@ import imageProcess as ip
from config import input_shape
def extractDataFromVideo(filename, videoName, facesList, labelsList):
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.
# Start capture of a video
print("Lecture vidéo de", videoName)
frameRate = 1
cap = cv2.VideoCapture(filename)
if (cap.isOpened() == False):
......@@ -40,12 +39,14 @@ def extractDataFromVideo(filename, videoName, facesList, labelsList):
if len(newFaces) == 1:
facesList += newFaces
labelsList.append(emotionNbr)
print("Donnée ajoutée, Images:", len(
facesList), "Labels:", len(labelsList))
else: print("Erreur pour la donnée : Aucun ou plusieurs visages détectés")
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(25) & 0xFF == ord('q'):
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
......@@ -68,7 +69,8 @@ def extractDataFromVideo(filename, videoName, facesList, labelsList):
# LOAD DATA
def loadRavdessData(maxNbrImages=float('inf')):
def loadRavdessData(maxNbrImages=10000000000):
print(f"\nCHARGEMENT DE {maxNbrImages} DONNEES DEPUIS RAVDESS...")
foldername = "data/ravdessTest/videos/"
emotions = ["_", "Neutral", "Calm", "Happy",
......@@ -79,35 +81,36 @@ def loadRavdessData(maxNbrImages=float('inf')):
# For each actor...
for actorName in os.listdir(foldername):
# If we overreach the maximum number of images desired, stop
if len(facesList) > maxNbrImages:
break
print(f"\nTRAITEMENT ACTEUR N°{actorName[-2:]}\n")
print(f"TRAITEMENT ACTEUR N°{actorName[-2:]}")
videoNames = os.listdir(foldername+actorName)
nbrOfVideos = len(videoNames)
k = 0
# For each video...
for videoName in videoNames:
# If we overreach the maximum number of images desired, stop
if len(facesList) >= maxNbrImages:
break
k += 1
print(f"Traitement de {videoName}, video {k}/{nbrOfVideos}")
filename = foldername+actorName+'/'+videoName
# Press Q on keyboard to exit ONE video
if cv2.waitKey(25) & 0xFF == ord('q'):
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if videoName[7] == '2':
# Doesnt take Calm emotion into account
print("Emotion 'Calme', non prise en compte")
else:
#Add datas extracted from the specified video to features and labels
facesList, labelsList = extractDataFromVideo(
filename, videoName, facesList, labelsList)
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 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]
......
......@@ -15,9 +15,13 @@ import random as rd
import cv2
import csv
from loadFer2013ds import *
from loadRavdessDs import *
from loadFer2013DS import *
from loadRavdessDS import *
from loadExpWDS import *
from loadAffwild import *
from utils import *
X, Y = loadFer2013Data(100)
W, Z = loadRavdessData(100)
\ No newline at end of file
# X, Y = loadFer2013Data(10)
# W, Z = loadRavdessData(10)
# A, B = loadExpWData(10)
C, D = loadAffwildData(1000)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment