简体   繁体   中英

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. However, every time I try using paths.image_list , I get no feedback (empty null lists.) I have no idea why this is happening.

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.

In my opinion it is easier to use a path package to take care of joining paths. 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.)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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