Commit 7216fb4f authored by tihmels's avatar tihmels
Browse files

AffectNet Preprocessing implementiert. Kleine Refactorings

parent 0e4410dd
import argparse
import logging
import os
import shutil
import sys
from shutil import copyfile
import pandas as pd
logfile = '../logs/affectnet.log'
# Erstellt und konfiguriert die Logdatei
logging.basicConfig(level=logging.NOTSET, format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
parser = argparse.ArgumentParser(description='AffectNet PrePipeline')
parser.add_argument('csv', action='store', help='the .csv file to process')
parser.add_argument('source', action='store', help='image source folder')
parser.add_argument('-0', action='append_const', dest='emotions', const='neutral', help='neutral')
parser.add_argument('-1', action='append_const', dest='emotions', const='happy', help='happy')
parser.add_argument('-2', action='append_const', dest='emotions', const='sadness', help='sadness')
parser.add_argument('-3', action='append_const', dest='emotions', const='surprise', help='surprise')
parser.add_argument('-4', action='append_const', dest='emotions', const='fear', help='fear')
parser.add_argument('-5', action='append_const', dest='emotions', const='disgust', help='disgust')
parser.add_argument('-6', action='append_const', dest='emotions', const='anger', help='anger')
parser.add_argument('-7', action='append_const', dest='emotions', const='contempt', help='contempt')
parser.add_argument('-8', action='append_const', dest='emotions', const='none', help='none')
parser.add_argument('-9', action='append_const', dest='emotions', const='uncertain', help='uncertain')
parser.add_argument('-d', action='store', dest='destination', default='sorted_set', help='destination to sorted set')
parser.add_argument('-l', '--limit', action='store', type=int, dest='limit', default=-1, help='image limit per emotion')
parser.add_argument('-v', '--valence', action='store', default='-2', dest='valence', type=float, help='min valence')
parser.add_argument('-a', '--arousal', action='store', default='-2', dest='arousal', type=float, help='min arousal')
arguments = parser.parse_args()
emodict = {0: 'neutral', 1: 'happy', 2: 'sadness', 3: 'surprise', 4: 'fear', 5: 'disgust', 6: 'anger', 7: 'contempt', 8: 'none', 9: 'uncertain', 10: 'noface'}
counter = {}
for e in arguments.emotions:
counter[e] = 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)
if os.path.exists(destination):
delete = input(destination + ' existiert. Durch diesen Prozess werden die Daten gelöscht. Fortfahren (y/n): ')
if delete == 'y':
else:'process aborted')
def generate_records(csvfile):
data = pd.read_csv(csvfile, delimiter=',', dtype='a')
imagepaths =['subDirectory_filePath'],
labels =['expression'],
valence =['valence'],
arousal =['arousal'],
for e in arguments.emotions:
dest = os.path.join(destination, e)
if not os.path.exists(dest):
data = zip(labels, imagepaths, valence, arousal)
for d in data:
img = d[1]
pathfrom = os.path.join(source, img)
if not os.path.exists(pathfrom): + ' not found')
if all(count == arguments.limit for count in counter.values()):
emotion = emodict.get(d[0])
if emotion not in arguments.emotions or d[2] < float(arguments.valence) or d[3] < float(arguments.arousal) or counter[emotion] == arguments.limit:
parent = os.path.join(destination, emotion)
pathto = os.path.join(parent, img.split('/')[1])
copyfile(pathfrom, pathto)'wrote file ' + pathto)
counter[emotion] += 1
if __name__ == '__main__':
......@@ -14,7 +14,7 @@ emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness
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:
......@@ -39,7 +39,7 @@ for x in participants:
emotion = int(float(file.readline()))
source_emotions = glob.glob("resources/img_data/source_images/%s/%s/*.png" % (number, current_session))
source_emotions = glob.glob("../resources/img_data/source_images/%s/%s/*.png" % (number, current_session))
......@@ -58,8 +58,8 @@ for x in participants:
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:])
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"""
copyfile(sourcefile_neutral, dest_neut)
import numpy as np
import cv2
import mxnet as mx
import pandas as pd
import random
import os
curdir = os.path.abspath(os.path.dirname(__file__))
def gen_record(csvfile,channel):
data = pd.read_csv(csvfile,delimiter=',',dtype='a')
labels = np.array(data['emotion'],np.float)
# print(labels,'\n',data['emotion'])
imagebuffer = np.array(data['pixels'])
images = np.array([np.fromstring(image,np.uint8,sep=' ') for image in imagebuffer])
del imagebuffer
num_shape = int(np.sqrt(images.shape[-1]))
images.shape = (images.shape[0],num_shape,num_shape)
# img=images[0];cv2.imshow('test',img);cv2.waitKey(0);cv2.destroyAllWindow();exit()
dirs = set(data['Usage'])
subdirs = set(labels)
class_dir = {}
for dr in dirs:
dest = os.path.join(curdir,dr)
class_dir[dr] = dest
if not os.path.exists(dest):
data = zip(labels,images,data['Usage'])
for d in data:
destdir = os.path.join(class_dir[d[-1]],str(int(d[0])))
if not os.path.exists(destdir):
img = d[1]
filepath = unique_name(destdir,d[-1])
print('[^_^] Write image to %s' % filepath)
if not filepath:
sig = cv2.imwrite(filepath,img)
if not sig:
def unique_name(pardir,prefix,suffix='jpg'):
filename = '{0}_{1}.{2}'.format(prefix,random.randint(1,10**8),suffix)
filepath = os.path.join(pardir,filename)
if not os.path.exists(filepath):
return filepath
if __name__ == '__main__':
filename = 'fer2013.csv'
filename = os.path.join(curdir,filename)
# ##################### test
# tmp = unique_name('./Training','Training')
# print(tmp)
\ No newline at end of file
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