条件文件加载和标签 python
Conditional file loading and labeling python
我正在尝试制作一个从目录加载某些图像的函数。条件是如果图像名称包含特定数字,则应将其作为对象加载(具有名称和数字)。
这是我目前所做的:
get_image_data([71,72,82,105...])
directory = os.listdir("/Users/Me/Desktop/path2images/")
#Sample of files
0070_pressure_pred.png
0070_pressure_target.png
0070_velX_pred.png
0070_velX_target.png
0070_velY_pred.png
0070_velY_target.png
0071_pressure_pred.png
0071_pressure_target.png
0071_velX_pred.png
0071_velX_target.png
0071_velY_pred.png
0071_velY_target.png
0072_pressure_pred.png
0072_pressure_target.png
0072_velX_pred.png
函数:
def get_pressure_prediction(file_numbers):
#takes a list of image number and returns them
for files in directory:
for x in file_numbers:
if str(x) in files[:4]:
print("File '{}' matched".format(files)) #Code functions fine until here
if str(pressure_pred) in files:
"pp{}".format(x) = mpimg.imread(path + files) #attempting to load image and label it with pp{image number}
elif str(pressure_target) in files:
"pt{}".format(x) = mpimg.imread(path + files)
elif str(velX_pred) in files:
"vxp{}".format(x) = mpimg.imread(path + files)
elif str(velX_target) in files:
"vxt{}".format(x) = mpimg.imread(path + files)
elif str(velY_pred) in files:
"vyp{}".format(x) = mpimg.imread(path + files)
elif str(velY_target) in files:
"vyt{}".format(x) = mpimg.imread(path + files)
break
我收到此错误消息:
"vyt{}".format(x) = mpimg.imread(path + files)
^
SyntaxError: can't assign to function call
您不能为字符串("vyt{}".format(x)
是字符串)分配另一个值。
我建议您将函数输出分配给字典中的指定键:
mapping = dict()
if str(pressure_pred) in files:
mapping["pp{}".format(x)] = mpimg.imread(path + files)
elif ...
...
# You will have to change it in all your conditions
# and then you will be able to access it as follows:
res = mapping["vytX"] # where vytX is the desired variable
您不能有条件地标记变量。
"pp{}".format(x) 是一个字符串,变量名不能是字符串。例如 "foo" = 0
会给你一个语法错误。
这是另一个不起作用的例子:
for i in range(5):
"variable{i}" = i
我建议创建一个 class 来存储图像的名称和路径,然后将图像附加到列表中,然后您可以对列表中的图像做任何您想做的事情。示例:
class Image:
def __init__(self, label, filePath):
self.label = label
self.image = mpimg.imread(filePath)
images = []
if str(x) in files[:4]:
print("File '{}' matched".format(files))
if str(pressure_pred) in files:
images.append(Image("pp{}".format(x), path + files))
elif str(pressure_target) in files:
images.append(Image("pt{}".format(x), path + files))
elif str(velX_pred) in files:
images.append(Image("vxp{}".format(x), path + files))
elif str(velX_target) in files:
images.append(Image("vxt{}".format(x), path + files))
elif str(velY_pred) in files:
images.append(Image("vyp{}".format(x), path + files))
elif str(velY_target) in files:
images.append(Image("vyt{}".format(x), path + files))
break
我正在尝试制作一个从目录加载某些图像的函数。条件是如果图像名称包含特定数字,则应将其作为对象加载(具有名称和数字)。
这是我目前所做的:
get_image_data([71,72,82,105...])
directory = os.listdir("/Users/Me/Desktop/path2images/")
#Sample of files
0070_pressure_pred.png
0070_pressure_target.png
0070_velX_pred.png
0070_velX_target.png
0070_velY_pred.png
0070_velY_target.png
0071_pressure_pred.png
0071_pressure_target.png
0071_velX_pred.png
0071_velX_target.png
0071_velY_pred.png
0071_velY_target.png
0072_pressure_pred.png
0072_pressure_target.png
0072_velX_pred.png
函数:
def get_pressure_prediction(file_numbers):
#takes a list of image number and returns them
for files in directory:
for x in file_numbers:
if str(x) in files[:4]:
print("File '{}' matched".format(files)) #Code functions fine until here
if str(pressure_pred) in files:
"pp{}".format(x) = mpimg.imread(path + files) #attempting to load image and label it with pp{image number}
elif str(pressure_target) in files:
"pt{}".format(x) = mpimg.imread(path + files)
elif str(velX_pred) in files:
"vxp{}".format(x) = mpimg.imread(path + files)
elif str(velX_target) in files:
"vxt{}".format(x) = mpimg.imread(path + files)
elif str(velY_pred) in files:
"vyp{}".format(x) = mpimg.imread(path + files)
elif str(velY_target) in files:
"vyt{}".format(x) = mpimg.imread(path + files)
break
我收到此错误消息:
"vyt{}".format(x) = mpimg.imread(path + files)
^
SyntaxError: can't assign to function call
您不能为字符串("vyt{}".format(x)
是字符串)分配另一个值。
我建议您将函数输出分配给字典中的指定键:
mapping = dict()
if str(pressure_pred) in files:
mapping["pp{}".format(x)] = mpimg.imread(path + files)
elif ...
...
# You will have to change it in all your conditions
# and then you will be able to access it as follows:
res = mapping["vytX"] # where vytX is the desired variable
您不能有条件地标记变量。
"pp{}".format(x) 是一个字符串,变量名不能是字符串。例如 "foo" = 0 会给你一个语法错误。
这是另一个不起作用的例子:
for i in range(5):
"variable{i}" = i
我建议创建一个 class 来存储图像的名称和路径,然后将图像附加到列表中,然后您可以对列表中的图像做任何您想做的事情。示例:
class Image:
def __init__(self, label, filePath):
self.label = label
self.image = mpimg.imread(filePath)
images = []
if str(x) in files[:4]:
print("File '{}' matched".format(files))
if str(pressure_pred) in files:
images.append(Image("pp{}".format(x), path + files))
elif str(pressure_target) in files:
images.append(Image("pt{}".format(x), path + files))
elif str(velX_pred) in files:
images.append(Image("vxp{}".format(x), path + files))
elif str(velX_target) in files:
images.append(Image("vxt{}".format(x), path + files))
elif str(velY_pred) in files:
images.append(Image("vyp{}".format(x), path + files))
elif str(velY_target) in files:
images.append(Image("vyt{}".format(x), path + files))
break