image_sort.py 1.47 KB
Newer Older
1 2 3 4
import glob
from shutil import copyfile

emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"]  # Define emotion order
tihmels's avatar
tihmels committed
5
participants = glob.glob("basis_data/source_emotion/*")  # Returns a list of all folders with participant numbers
6 7 8

for x in participants:
    part = "%s" % x[-4:]  # store current participant number
tihmels's avatar
tihmels committed
9 10
    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):
11 12 13 14 15 16
            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.

tihmels's avatar
tihmels committed
17
            sourcefile_emotion = glob.glob("basis_data/source_images/%s/%s/*" % (part, current_session))[
18
                -1]  # get path for last image in sequence, which contains the emotion
tihmels's avatar
tihmels committed
19
            sourcefile_neutral = glob.glob("basis_data/source_images/%s/%s/*" % (part, current_session))[
20 21
                0]  # do same for neutral image

tihmels's avatar
tihmels committed
22 23
            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" % (
24 25 26 27
            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