Commit 02f3f431 authored by tihmels's avatar tihmels

Kommentare geschrieben

parent 6f1eabdc
...@@ -25,64 +25,78 @@ parser.add_argument('-a', '--arousal', action='store', default='-2', dest='arous ...@@ -25,64 +25,78 @@ parser.add_argument('-a', '--arousal', action='store', default='-2', dest='arous
arguments = parser.parse_args() arguments = parser.parse_args()
logging.debug(arguments) logging.debug(arguments)
emotidict = {0: 'neutral', 1: 'happy', 2: 'sadness', 3: 'surprise', 4: 'fear', 5: 'disgust', 6: 'anger', 7: 'contempt', # the numbers define the particular emotions in the .csv file
8: 'none', 9: 'uncertain', 10: 'noface'} emotiondict = {0: 'neutral', 1: 'happy', 2: 'sadness', 3: 'surprise', 4: 'fear', 5: 'disgust', 6: 'anger', 7: 'contempt',
8: 'none', 9: 'uncertain', 10: 'noface'}
emoticounter = {} # count emotions
for e in arguments.emotions: emotioncounter = {}
emoticounter[e] = 0 for emotion in arguments.emotions:
emotioncounter[emotion] = 0
curdir = os.path.abspath(os.path.dirname(__file__))
csv = os.path.join(curdir, arguments.csv)
source = os.path.join(curdir, arguments.source)
destination = os.path.join(curdir, arguments.destination)
# define some useful paths
currentdir = os.path.abspath(os.path.dirname(__file__))
csv = os.path.join(currentdir, arguments.csv)
source = os.path.join(currentdir, arguments.source)
destination = os.path.join(currentdir, arguments.destination)
def generate_records(csvfile): def generate_records(csvfile):
# read the .csv file
data = pd.read_csv(csvfile, delimiter=',', dtype='a') data = pd.read_csv(csvfile, delimiter=',', dtype='a')
# get the necessary columns
imagepaths = pd.np.array(data['subDirectory_filePath'], pd.np.str) imagepaths = pd.np.array(data['subDirectory_filePath'], pd.np.str)
labels = pd.np.array(data['expression'], pd.np.int) labels = pd.np.array(data['expression'], pd.np.int)
valence = pd.np.array(data['valence'], pd.np.float) valence = pd.np.array(data['valence'], pd.np.float)
arousal = pd.np.array(data['arousal'], pd.np.float) arousal = pd.np.array(data['arousal'], pd.np.float)
for e in arguments.emotions: for emotion in arguments.emotions:
dest = os.path.join(destination, e) # join the destination path with the emotion identifier and create it if it isn't..
if not os.path.exists(dest): dest_subfolder = os.path.join(destination, emotion)
os.makedirs(dest) if not os.path.exists(dest_subfolder):
os.makedirs(dest_subfolder)
else: else:
emoticounter[e] = len(os.listdir(dest)) # .. otherwise, count images
emotioncounter[emotion] = len(os.listdir(dest_subfolder))
data = zip(labels, imagepaths, valence, arousal) # wrap tupels for iterating
csvrows = zip(labels, imagepaths, valence, arousal)
for d in data: for c in csvrows:
emotion = emotidict.get(d[0]) # get emotion
emotion = emotiondict.get(c[0])
if emotion not in arguments.emotions or d[2] < arguments.valence or d[3] < arguments.arousal or emoticounter[ # check if the row fits the argument conditions
if emotion not in arguments.emotions or c[2] < arguments.valence or c[3] < arguments.arousal or emotioncounter[
emotion] >= arguments.limit: emotion] >= arguments.limit:
continue continue
img = d[1] # the image path
img = c[1]
pathfrom = os.path.join(source, img) pathfrom = os.path.join(source, img)
# check if the respective file exists
if not os.path.exists(pathfrom): if not os.path.exists(pathfrom):
logging.info(img + ' not found') logging.info(img + ' not found')
continue continue
folder = os.path.join(destination, emotion) dest_subfolder = os.path.join(destination, emotion)
filePath = os.path.join(folder, 'V' + str(round(d[2], 1)) + 'A' + str(round(d[3], 1)) + '_' + img.split('/')[1]) # join destination path with filename
filePath = os.path.join(dest_subfolder, img.split('/')[1])
# if not exists, copy file to destination
if not os.path.isfile(filePath): if not os.path.isfile(filePath):
copyfile(pathfrom, filePath) copyfile(pathfrom, filePath)
logging.info('wrote file ' + filePath) logging.info('wrote file ' + filePath)
emoticounter[emotion] += 1 emotioncounter[emotion] += 1
if all(count >= arguments.limit for count in emoticounter.values()): # break if enough files were copied
if all(count >= arguments.limit for count in emotioncounter.values()):
break break
if __name__ == '__main__': if __name__ == '__main__':
generate_records(csv) generate_records(csv)
logging.info('finish') print('finish')
logging.info(emoticounter) print(emotioncounter)
""" """
Diese Klasse sortiert die source_images anhand der source_emotions in die Ordner des sorted_set ein This script maps the source_emotions to the source_images and creates a sorted set
""" """
import glob import glob
from shutil import copyfile from shutil import copyfile
"""
Array der Emotionen
Gibt die Reihenfolge der Emotionen an (Labels von 0-7)
"""
emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"] emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"]
""" # returns a list with each subfolder representing a participant number
Liste mit den Dateiordnern aller Teilnehmer
"""
participants = glob.glob("../resources/img_data/source_emotion/*") participants = glob.glob("../resources/img_data/source_emotion/*")
for x in participants: for x in participants:
""" # participant number
Teilnehmernummer
"""
number = "%s" % x[-4:] number = "%s" % x[-4:]
for sessions in glob.glob("%s/*" % x): for sessions in glob.glob("%s/*" % x):
for files in glob.glob("%s/*" % sessions): for files in glob.glob("%s/*" % sessions):
"""
Sessionnummer # session number
"""
current_session = files[39:-30] current_session = files[39:-30]
""" # opens the correlating .txt file
Öffnet die zur aktuellen Emotion korrelierende .txt Datei
"""
file = open(files, 'r') file = open(files, 'r')
""" # the respective emotion is encoded as a float value
In der Datei steht die aktuell betrachtete Emotion, kodiert als float-Wert
"""
emotion = int(float(file.readline())) emotion = int(float(file.readline()))
source_emotions = glob.glob("../resources/img_data/source_images/%s/%s/*.png" % (number, current_session)) # get a sorted list with the image sequence from the considered session
source_emotions.sort() source_images = glob.glob("../resources/img_data/source_images/%s/%s/*.png" % (number, current_session))
source_images.sort()
""" # the last image is the distinct emotion
Das letzte Bild einer Sequenz ist eine ausgeprägte Emotion sourcefile_emotion = source_images[-1]
"""
sourcefile_emotion = source_emotions[-1]
""" # the first image is the neutral expression
Das erste Bild ist ein neutraler Ausdruck sourcefile_neutral = source_images[0]
"""
sourcefile_neutral = source_emotions[0]
""" # create new paths
Erstellt neue Pfade zum einsortieren destination_neutral = "../resources/img_data/sorted_set/neutral/%s" % sourcefile_neutral[44:]
destination_emotion = "../resources/img_data/sorted_set/%s/%s" % (emotions[emotion], sourcefile_emotion[44:])
Für den neutralen Ausdruck
Für die Emotion
"""
dest_neut = "../resources/img_data/sorted_set/neutral/%s" % sourcefile_neutral[44:]
dest_emot = "../resources/img_data/sorted_set/%s/%s" % (emotions[emotion], sourcefile_emotion[44:])
"""Kopiert Dateien""" # copy files
copyfile(sourcefile_neutral, dest_neut) copyfile(sourcefile_neutral, destination_neutral)
copyfile(sourcefile_emotion, dest_emot) copyfile(sourcefile_emotion, destination_emotion)
...@@ -5,29 +5,36 @@ import cv2 ...@@ -5,29 +5,36 @@ import cv2
import numpy as np import numpy as np
import pandas as pd import pandas as pd
curdir = os.path.abspath(os.path.dirname(__file__)) currentdir = os.path.abspath(os.path.dirname(__file__))
def gen_record(csvfile, channel): def gen_record(csvfile):
# read the .csv file
data = pd.read_csv(csvfile, delimiter=',', dtype='a') data = pd.read_csv(csvfile, delimiter=',', dtype='a')
# the emotion labels encoded as floats
labels = np.array(data['emotion'], np.float) labels = np.array(data['emotion'], np.float)
# print(labels,'\n',data['emotion'])
# the images are stored as pixel values
imagebuffer = np.array(data['pixels']) imagebuffer = np.array(data['pixels'])
# convert them to images
images = np.array([np.fromstring(image, np.uint8, sep=' ') for image in imagebuffer]) images = np.array([np.fromstring(image, np.uint8, sep=' ') for image in imagebuffer])
del imagebuffer
num_shape = int(np.sqrt(images.shape[-1])) num_shape = int(np.sqrt(images.shape[-1]))
images.shape = (images.shape[0], num_shape, num_shape) images.shape = (images.shape[0], num_shape, num_shape)
# img=images[0];cv2.imshow('test',img);cv2.waitKey(0);cv2.destroyAllWindow();exit()
# creates a set from a total of three usage types
dirs = set(data['Usage']) dirs = set(data['Usage'])
subdirs = set(labels) subdirs = set(labels)
class_dir = {} class_dir = {}
for dr in dirs: for dr in dirs:
dest = os.path.join(curdir, dr) # create destination directories
dest = os.path.join(currentdir, dr)
class_dir[dr] = dest class_dir[dr] = dest
if not os.path.exists(dest): if not os.path.exists(dest):
os.mkdir(dest) os.mkdir(dest)
# wrap data for iterating
data = zip(labels, images, data['Usage']) data = zip(labels, images, data['Usage'])
for d in data: for d in data:
...@@ -35,8 +42,8 @@ def gen_record(csvfile, channel): ...@@ -35,8 +42,8 @@ def gen_record(csvfile, channel):
if not os.path.exists(destdir): if not os.path.exists(destdir):
os.mkdir(destdir) os.mkdir(destdir)
img = d[1] img = d[1]
# create a unique name
filepath = unique_name(destdir, d[-1]) filepath = unique_name(destdir, d[-1])
print('[^_^] Write image to %s' % filepath)
if not filepath: if not filepath:
continue continue
sig = cv2.imwrite(filepath, img) sig = cv2.imwrite(filepath, img)
...@@ -55,9 +62,5 @@ def unique_name(pardir, prefix, suffix='jpg'): ...@@ -55,9 +62,5 @@ def unique_name(pardir, prefix, suffix='jpg'):
if __name__ == '__main__': if __name__ == '__main__':
filename = 'fer2013.csv' filename = 'fer2013.csv'
filename = os.path.join(curdir, filename) filename = os.path.join(currentdir, filename)
gen_record(filename, 1) gen_record(filename)
# ##################### test
# tmp = unique_name('./Training','Training')
# print(tmp)
...@@ -142,6 +142,7 @@ logging.info("Fisherface training finished - {}% average\n".format(np.mean(metas ...@@ -142,6 +142,7 @@ logging.info("Fisherface training finished - {}% average\n".format(np.mean(metas
if not arguments.test: if not arguments.test:
fishface.write('resources/models/detection_model.xml') fishface.write('resources/models/detection_model.xml')
logging.info('saved trained classifier')
if arguments.email: if arguments.email:
sendMail('Fisherface training finished') sendMail('Fisherface training finished', filepath='resources/models/detection_model.xml')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment