可能是一個菜鳥問題,但在運行時使此錯誤串列索引超出范圍。
影像 = glob.glob('/OwnCollection')
cars = []
notcars = []
all_cars = []
all_notcars = []
for image in images:
if 'non-vehicles' in image:
all_notcars.append(image)
else:
all_cars.append(image)
# Get only 1/5 of the training data to avoid overfitting
for ix, notcar in enumerate(all_notcars):
if ix % 5 == 0:
notcars.append(notcar)
for ix, car in enumerate(all_cars):
if ix % 5 == 0:
cars.append(car)
car_image = mpimg.imread(cars[1])
notcar_image = mpimg.imread(notcars[5])
錯誤代碼
---> 24 car_image = mpimg.imread(cars[1])
25 notcar_image = mpimg.imread(notcars[5])
26
IndexError: list index out of range
檔案夾內有兩個名為“non-vehicles”的檔案夾和第二個名為“vehicles”的檔案夾
任何幫助都受到高度贊賞。
uj5u.com熱心網友回復:
您的代碼命令像這樣獲取數字 1 和 5 中的陣列
car_image = mpimg.imread(cars[1])
notcar_image = mpimg.imread(notcars[5])
但是在這個程式中有一個小于 1 的陣列,所以你必須用命令知道你的陣列有多長
print(len(cars))
并且您的陣列命令小于長陣列
uj5u.com熱心網友回復:
如果您訪問 Python 串列中的無效索引,則會出現“串列索引超出范圍”錯誤。換句話說,如果你保存了一個變數,它將被存盤在陣列的第一個單元格中
![串列索引超出范圍(notcars[5])?](https://img.uj5u.com/2021/10/27/8241095e78a449bf88e75318b548086b.png)
所以你的代碼可能需要像這樣
car_image = mpimg.imread(cars[0])
也許仔細檢查你的影像在哪里可能會有用,如果沒有找到任何東西,這可能會導致索引錯誤
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/338091.html
