Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Frank Tjado Ihmels
medienverarbeitung17.projectmood
Commits
02f3f431
Commit
02f3f431
authored
Jan 28, 2018
by
tihmels
Browse files
Kommentare geschrieben
parent
6f1eabdc
Changes
4
Hide whitespace changes
Inline
Side-by-side
projectmood/datasets/affectnet.py
View file @
02f3f431
...
...
@@ -25,64 +25,78 @@ parser.add_argument('-a', '--arousal', action='store', default='-2', dest='arous
arguments
=
parser
.
parse_args
()
logging
.
debug
(
arguments
)
emotidict
=
{
0
:
'neutral'
,
1
:
'happy'
,
2
:
'sadness'
,
3
:
'surprise'
,
4
:
'fear'
,
5
:
'disgust'
,
6
:
'anger'
,
7
:
'contempt'
,
8
:
'none'
,
9
:
'uncertain'
,
10
:
'noface'
}
# the numbers define the particular emotions in the .csv file
emotiondict
=
{
0
:
'neutral'
,
1
:
'happy'
,
2
:
'sadness'
,
3
:
'surprise'
,
4
:
'fear'
,
5
:
'disgust'
,
6
:
'anger'
,
7
:
'contempt'
,
8
:
'none'
,
9
:
'uncertain'
,
10
:
'noface'
}
emoticounter
=
{}
for
e
in
arguments
.
emotions
:
emoticounter
[
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
)
# count emotions
emotioncounter
=
{}
for
emotion
in
arguments
.
emotions
:
emotioncounter
[
emotion
]
=
0
# 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
):
# read the .csv file
data
=
pd
.
read_csv
(
csvfile
,
delimiter
=
','
,
dtype
=
'a'
)
# get the necessary columns
imagepaths
=
pd
.
np
.
array
(
data
[
'subDirectory_filePath'
],
pd
.
np
.
str
)
labels
=
pd
.
np
.
array
(
data
[
'expression'
],
pd
.
np
.
int
)
valence
=
pd
.
np
.
array
(
data
[
'valence'
],
pd
.
np
.
float
)
arousal
=
pd
.
np
.
array
(
data
[
'arousal'
],
pd
.
np
.
float
)
for
e
in
arguments
.
emotions
:
dest
=
os
.
path
.
join
(
destination
,
e
)
if
not
os
.
path
.
exists
(
dest
):
os
.
makedirs
(
dest
)
for
emotion
in
arguments
.
emotions
:
# join the destination path with the emotion identifier and create it if it isn't..
dest_subfolder
=
os
.
path
.
join
(
destination
,
emotion
)
if
not
os
.
path
.
exists
(
dest_subfolder
):
os
.
makedirs
(
dest_subfolder
)
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
:
emotion
=
emotidict
.
get
(
d
[
0
])
for
c
in
csvrows
:
# 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
:
continue
img
=
d
[
1
]
# the image path
img
=
c
[
1
]
pathfrom
=
os
.
path
.
join
(
source
,
img
)
# check if the respective file exists
if
not
os
.
path
.
exists
(
pathfrom
):
logging
.
info
(
img
+
' not found'
)
continue
folder
=
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
])
dest_subfolder
=
os
.
path
.
join
(
destination
,
emotion
)
# 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
):
copyfile
(
pathfrom
,
filePath
)
logging
.
info
(
'wrote file '
+
filePath
)
emoticounter
[
emotion
]
+=
1
emoti
on
counter
[
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
if
__name__
==
'__main__'
:
generate_records
(
csv
)
logging
.
info
(
'finish'
)
logging
.
info
(
emoticounter
)
print
(
'finish'
)
print
(
emoti
on
counter
)
projectmood/datasets/cohn_kanade.py
View file @
02f3f431
"""
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
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"
]
"""
Liste mit den Dateiordnern aller Teilnehmer
"""
# returns a list with each subfolder representing a participant number
participants
=
glob
.
glob
(
"../resources/img_data/source_emotion/*"
)
for
x
in
participants
:
"""
Teilnehmernummer
"""
# participant number
number
=
"%s"
%
x
[
-
4
:]
for
sessions
in
glob
.
glob
(
"%s/*"
%
x
):
for
files
in
glob
.
glob
(
"%s/*"
%
sessions
):
"""
Sessionnummer
"""
# session number
current_session
=
files
[
39
:
-
30
]
"""
Öffnet die zur aktuellen Emotion korrelierende .txt Datei
"""
# opens the correlating .txt file
file
=
open
(
files
,
'r'
)
"""
In der Datei steht die aktuell betrachtete Emotion, kodiert als float-Wert
"""
# the respective emotion is encoded as a float value
emotion
=
int
(
float
(
file
.
readline
()))
source_emotions
=
glob
.
glob
(
"../resources/img_data/source_images/%s/%s/*.png"
%
(
number
,
current_session
))
source_emotions
.
sort
()
# get a sorted list with the image sequence from the considered session
source_images
=
glob
.
glob
(
"../resources/img_data/source_images/%s/%s/*.png"
%
(
number
,
current_session
))
source_images
.
sort
()
"""
Das letzte Bild einer Sequenz ist eine ausgeprägte Emotion
"""
sourcefile_emotion
=
source_emotions
[
-
1
]
# the last image is the distinct emotion
sourcefile_emotion
=
source_images
[
-
1
]
"""
Das erste Bild ist ein neutraler Ausdruck
"""
sourcefile_neutral
=
source_emotions
[
0
]
# the first image is the neutral expression
sourcefile_neutral
=
source_images
[
0
]
"""
Erstellt neue Pfade zum einsortieren
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
:])
# create new paths
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
:])
"""Kopiert Dateien"""
copyfile
(
sourcefile_neutral
,
dest_neut
)
copyfile
(
sourcefile_emotion
,
dest_emot
)
# copy files
copyfile
(
sourcefile_neutral
,
dest
ination
_neut
ral
)
copyfile
(
sourcefile_emotion
,
dest
ination
_emot
ion
)
projectmood/datasets/fer2013.py
View file @
02f3f431
...
...
@@ -5,29 +5,36 @@ import cv2
import
numpy
as
np
import
pandas
as
pd
curdir
=
os
.
path
.
abspath
(
os
.
path
.
dirname
(
__file__
))
cur
rent
dir
=
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'
)
# the emotion labels encoded as floats
labels
=
np
.
array
(
data
[
'emotion'
],
np
.
float
)
# print(labels,'\n',data['emotion'])
# the images are stored as pixel values
imagebuffer
=
np
.
array
(
data
[
'pixels'
])
# convert them to images
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()
# creates a set from a total of three usage types
dirs
=
set
(
data
[
'Usage'
])
subdirs
=
set
(
labels
)
class_dir
=
{}
for
dr
in
dirs
:
dest
=
os
.
path
.
join
(
curdir
,
dr
)
# create destination directories
dest
=
os
.
path
.
join
(
currentdir
,
dr
)
class_dir
[
dr
]
=
dest
if
not
os
.
path
.
exists
(
dest
):
os
.
mkdir
(
dest
)
# wrap data for iterating
data
=
zip
(
labels
,
images
,
data
[
'Usage'
])
for
d
in
data
:
...
...
@@ -35,8 +42,8 @@ def gen_record(csvfile, channel):
if
not
os
.
path
.
exists
(
destdir
):
os
.
mkdir
(
destdir
)
img
=
d
[
1
]
# create a unique name
filepath
=
unique_name
(
destdir
,
d
[
-
1
])
print
(
'[^_^] Write image to %s'
%
filepath
)
if
not
filepath
:
continue
sig
=
cv2
.
imwrite
(
filepath
,
img
)
...
...
@@ -55,9 +62,5 @@ def unique_name(pardir, prefix, suffix='jpg'):
if
__name__
==
'__main__'
:
filename
=
'fer2013.csv'
filename
=
os
.
path
.
join
(
curdir
,
filename
)
gen_record
(
filename
,
1
)
# ##################### test
# tmp = unique_name('./Training','Training')
# print(tmp)
filename
=
os
.
path
.
join
(
currentdir
,
filename
)
gen_record
(
filename
)
projectmood/process_model.py
View file @
02f3f431
...
...
@@ -142,6 +142,7 @@ logging.info("Fisherface training finished - {}% average\n".format(np.mean(metas
if
not
arguments
.
test
:
fishface
.
write
(
'resources/models/detection_model.xml'
)
logging
.
info
(
'saved trained classifier'
)
if
arguments
.
email
:
sendMail
(
'Fisherface training finished'
)
sendMail
(
'Fisherface training finished'
,
filepath
=
'resources/models/detection_model.xml'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment