import glob
from shutil import copyfile
emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"] #Define emotion order
participants = glob.glob("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("%s\\*" %x): #Store list of sessions for current participant
for files in glob.glob("%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("source_images\\%s\\%s\\*" %(part, current_session))[-1] #get path for last image in sequence, which contains the emotion
sourcefile_neutral = glob.glob("source_images\\%s\\%s\\*" %(part, current_session))[0] #do same for neutral image
dest_neut = "sorted_set\\neutral\\%s" %sourcefile_neutral[25:] #Generate path to put neutral image
for i in range(len(emotion)):
dest_emot = "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
上面的代码会抛出如下错误:
runfile('K:/Facedata landmarks/data/load_images.py', wdir='K:/Facedata landmarks/data')
Traceback (most recent call last):
File "<ipython-input-1-0b76baf7ba54>", line 1, in <module>
runfile('K:/Facedata landmarks/data/load_images.py', wdir='K:/Facedata landmarks/data')
File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)
File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "K:/Facedata landmarks/data/load_images.py", line 21, in <module>
emotion = int(float(file.readline())) #emotions are encoded as a float, readline as float, then convert to integer.
ValueError: could not convert string to float: ' 9.0000000e+00 4.0000000e+00\n'
我正在读取字符串的文件数据,但我将其作为float读取,最后将其转换为int。但是后来python也在抛出错误。任何人都可以帮我解决这个问题吗?我不明白这里出了什么问题以及如何解决。