列表索引超出范围
import cv2 #openCV
import numpy as np #Numpy
import os #OS
from random import shuffle
from tqdm import tqdm
testdir=r'C:\Users\ovais\Desktop\TestBike'
trainDir=r'C:\Users\ovais\Desktop\TrainBike'
imageSize=50
lr=1e-3 #0.003
modelName='BikevsCats.{}.{}.model'.format(lr,'2convbasic')
导入所有东西
def label_img(img):
word_label=img.split('.')[-3]
if word_label=='Bike':return [1,0]
elif word_label=='Car':return [0,1]
定义了功能
def createData():
train_data=[]
for img in tqdm(os.listdir(trainDir)):
label=label_img(img)
path=os.path.join(trainDir,img)
img=cv2.resize(cv.imread(path,cv2.IMREAD.GRAYSCALE),(imageSize,imageSize))
train_data.append([np.array(img),np.array(label)])
shuffle(train_data)
np.save('train_data.npy',train_data)
return train_data'
定义了功能
def process_test():
test_data=[]
for img in tqdm(os.listdir(testdir)):
path=os.path.join(testdir,img)
img_num=img.split('.')[0]
img=cv2.resize(cv.imread(path,cv2.IMREAD.GRAYSCALE),(imageSize,imageSize))
test_data.append([np.array(img),img_num])
np.save('test_data.npy',test_data)
return test_data
定义了功能
train_data=createData()
当我运行此行时,它会显示
IndexError Traceback (most recent call last)
<ipython-input-52-8eccc0c8df5c> in <module>()
----> 1 train_data=createData()
<ipython-input-50-006faf0cffbb> in createData()
2 train_data=[]
3 for img in tqdm(os.listdir(trainDir)):
----> 4 label=label_img(img)
5 path=os.path.join(trainDir,img)
6 img=cv2.resize(cv.imread(path,cv2.IMREAD.GRAYSCALE),(imageSize,imageSize))
<ipython-input-25-ffb241fb0632> in label_img(img)
1 def label_img(img):
----> 2 word_label=img.split('.')[-3]
3 if word_label=='Bike':return [1,0]
4 elif word_label=='Car':return [0,1]
IndexError: list index out of range