簡體   English   中英

我不能使用 python 圖像路徑

[英]I can't use python image paths

我有這個代碼。 這是為了獲取我在一組文件夾中的一組圖像,並嘗試將它們取出,並將 append 放入一個列表中,稍后將它們連接起來。 但是,每次我嘗試使用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))

但是每次,圖像路徑都返回空。 我不知道為什么,有人可以幫助我嗎?

當您運行代碼時,您會得到如下路徑:

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

注意 Scalogram 和 10000 之間的雙斜線。該路徑不存在,因此在該路徑中找不到任何圖像。

在我看來,使用路徑 package 來處理連接路徑更容易。 這樣可以避免雙斜杠等錯誤。 除此之外,它使您的代碼獨立於操作系統(現在這在 linux 上不起作用,因為斜杠在 windows 和 linux 中是相反的方向)。

這是一個例子:

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