简体   繁体   English

我不能使用 python 图像路径

[英]I can't use python image paths

I have this code.我有这个代码。 This is meant to take a set of images that I have in a set of folders, and try and fish them out, and append them to a list, to be concatenated later.这是为了获取我在一组文件夹中的一组图像,并尝试将它们取出,并将 append 放入一个列表中,稍后将它们连接起来。 However, every time I try using paths.image_list , I get no feedback (empty null lists.) I have no idea why this is happening.但是,每次我尝试使用paths.image_list时,我都没有收到任何反馈(空的 null 列表。)我不知道为什么会这样。

phases = ['Phase A', 'Phase B', 'Phase C']
sides = ['Primary', 'Secondary']
percentShorts = [round(f, 2) for f in list(np.arange(0.01, 1.0, 0.01))]
sections = ['sec_1/', 'sec_2/', 'sec_3/']
triggerTimes = [round(f, 2) for f in list(np.arange(0.06, 0.4, 0.06))]
powers= np.arange(10000, 210000,10000)
##triggerTimes = [0.3]
folders = ['input_current\\', 'output_current\\', 'output_voltage\\']
imager = ['a','b','c']

PREFIX = "C:\\Users\\Edwin\\Desktop\\PROJECT\\AI\\Data\\"
H_PREFIX = PREFIX + 'Healthy\\Scalogram\\'
F_PREFIX = PREFIX + 'Faulty\\Scalogram\\'
print("Healthy Prefix = ",H_PREFIX, "and Faulty Prefix= ", F_PREFIX)

START = 0.01
STEP = 0.01
BATCH = 99
BN = 1

percentShorts = [round(f, 2) for f in list(np.arange(START, START + STEP*BATCH, STEP))]
percentShorts = percentShorts[:BATCH]

##HEALTHY_COUNT = 718
HEALTHY_COUNT = 3
FAULTY_COUNT = len(phases)*len(sides)*BATCH*len(sections)*len(triggerTimes)

print("Healthy Count: {}".format(HEALTHY_COUNT))
print("Faulty Count: {}".format(FAULTY_COUNT))
print("Combined Count: {}".format(FAULTY_COUNT + HEALTHY_COUNT))

data = [] # Store images concatenated along the channel axis
healthLabels = [] # Indicate presence of fault or otherwise: ['Healthy', 'Faulty']
phaseLabels = [] # Store phase labels for faults: ['Phase A', 'Phase B', 'Phase C', 'None']
sideLabels = [] # Store side labels for faults: ['Primary', 'Secondary', 'None']
percentLabels = [] # Store percentage short labels for faults: [percentShort, 0]


print("[START]: Started processing Healthy Images at {}".format(datetime.now()))
print('[INFO]: Started processing Healthy Images')

# Healthy Images
for i in range(HEALTHY_COUNT):
    # List to hold horizontally-concatenated images for v-concat
    images = []

    for folder in folders:
      for power in powers:
        for ima in imager:
          newpath = H_PREFIX + '\\' +str(power)+ '\\' + folder
          imagePaths = sorted(list(paths.list_images(newpath)))
          print(imagePaths)
        # Loop over the image, read and append them to the list of images
          for imagePath in imagePaths:
            images.append(cv.cvtColor(cv.imread(imagePath), cv.COLOR_BGR2RGB))

But every time, the image paths returns empty.但是每次,图像路径都返回空。 I do not know why, could someone help me?我不知道为什么,有人可以帮助我吗?

When you run your code you get a path like:当您运行代码时,您会得到如下路径:

C:\Users\Edwin\Desktop\PROJECT\AI\Data\Healthy\Scalogram\\10000\input_current\

Pay attention to the double slash between Scalogram and 10000. This path doesn't exist and thus there aren't any images found in that path.注意 Scalogram 和 10000 之间的双斜线。该路径不存在,因此在该路径中找不到任何图像。

In my opinion it is easier to use a path package to take care of joining paths.在我看来,使用路径 package 来处理连接路径更容易。 This way you avoid errors like double slashes.这样可以避免双斜杠等错误。 In addition to that, it makes your code independent of the operating system (right now this would not work on linux because slashes are in windows and linux are in opposite directions.)除此之外,它使您的代码独立于操作系统(现在这在 linux 上不起作用,因为斜杠在 windows 和 linux 中是相反的方向)。

Here is an example:这是一个例子:

import os
...
folders = ['input_current', 'output_current', 'output_voltage']
...
PREFIX = os.path.join("C:", "Users", "Edwin", "Desktop", "PROJECT", "AI", "Data")
H_PREFIX = os.path.join(PREFIX, "Healthy", "Scalogram")
...
newpath = os.path.join(H_PREFIX, str(power), folder)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM