image_sort.py 1.49 KB
Newer Older
Arne Gerdes's avatar
Arne Gerdes committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import glob
from shutil import copyfile

emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"]  # Define emotion order
participants = glob.glob("Basis_data\\source_emotion\\*")  # Returns a list of all folders with participant numbers

for x in participants:
    part = "%s" % x[-4:]  # store current participant number
    for sessions in glob.glob("Basis_data\\%s\\*" % x):  # Store list of sessions for current participant
        for files in glob.glob("Basis_data\\%s\\*" % sessions):
            current_session = files[20:-30]
            file = open(files, 'r')

            emotion = int(
                float(file.readline()))  # emotions are encoded as a float, readline as float, then convert to integer.

            sourcefile_emotion = glob.glob("Basis_data\\source_images\\%s\\%s\\*" % (part, current_session))[
                -1]  # get path for last image in sequence, which contains the emotion
            sourcefile_neutral = glob.glob("Basis_data\\source_images\\%s\\%s\\*" % (part, current_session))[
                0]  # do same for neutral image

            dest_neut = "Basis_data\\sorted_set\\neutral\\%s" % sourcefile_neutral[25:]  # Generate path to put neutral image
            dest_emot = "Basis_data\\sorted_set\\%s\\%s" % (
            emotions[emotion], sourcefile_emotion[25:])  # Do same for emotion containing image

            copyfile(sourcefile_neutral, dest_neut)  # Copy file
            copyfile(sourcefile_emotion, dest_emot)  # Copy file