cohn_kanade.py 2.31 KB
Newer Older
tihmels's avatar
tihmels committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# ***************************************************
# * Copyright © 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
# * This file is part of whiteboard-project-matcher.
# *
# * whiteboard-project-matcher can not be copied and/or distributed without the express
# * permission of Tjado Ihmels
# *
# * Parts of this program are legally copied and adapted from
# * van Gent, P. (2016). Emotion Recognition With Python, OpenCV and a Face Dataset. A tech blog about fun things with Python and embedded electronics. Retrieved from:
# * http://www.paulvangent.com/2016/04/01/emotion-recognition-with-python-opencv-and-a-face-dataset/
# ***************************************************/


15
"""
tihmels's avatar
tihmels committed
16
This script maps the source_emotions to the source_images and creates a sorted set
17
18
"""

Arne Gerdes's avatar
Arne Gerdes committed
19
20
21
import glob
from shutil import copyfile

22
23
emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"]

tihmels's avatar
tihmels committed
24
# returns a list with each subfolder representing a participant number
25
participants = glob.glob("../resources/img_data/source_emotion/*")
Arne Gerdes's avatar
Arne Gerdes committed
26
27

for x in participants:
28

tihmels's avatar
tihmels committed
29
    # participant number
30
    number = "%s" % x[-4:]
31
32
    for sessions in glob.glob("%s/*" % x):
        for files in glob.glob("%s/*" % sessions):
tihmels's avatar
tihmels committed
33
34

            # session number
tihmels's avatar
tihmels committed
35
            current_session = files[39:-30]
Arne Gerdes's avatar
Arne Gerdes committed
36

tihmels's avatar
tihmels committed
37
            # opens the correlating .txt file
38
39
            file = open(files, 'r')

tihmels's avatar
tihmels committed
40
            # the respective emotion is encoded as a float value
41
            emotion = int(float(file.readline()))
Arne Gerdes's avatar
Arne Gerdes committed
42

tihmels's avatar
tihmels committed
43
44
45
            # 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()
Arne Gerdes's avatar
Arne Gerdes committed
46

tihmels's avatar
tihmels committed
47
48
            # the last image is the distinct emotion
            sourcefile_emotion = source_images[-1]
49

tihmels's avatar
tihmels committed
50
51
            # the first image is the neutral expression
            sourcefile_neutral = source_images[0]
52

tihmels's avatar
tihmels committed
53
54
55
            # 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:])
Arne Gerdes's avatar
Arne Gerdes committed
56

tihmels's avatar
tihmels committed
57
58
59
            # copy files
            copyfile(sourcefile_neutral, destination_neutral)
            copyfile(sourcefile_emotion, destination_emotion)