请帮我解决这个错误。我需要将图像一张一张地放入一个循环中以转换为灰色并计算 HOG 特征。但是之前我需要一张一张地拍摄图像。
# define path to images:
pos_im_path = r"/content/positive_images" # This is the path of our positive input dataset
# define the same for negatives
neg_im_path= r"negative_images"
# read the image files:
pos_im_listing = os.listdir(pos_im_path) # it will read all the files in the positive image path (so all the required images)
neg_im_listing = os.listdir(neg_im_path)
num_pos_samples = size(pos_im_listing) # simply states the total no. of images
num_neg_samples = size(neg_im_listing)
print(num_pos_samples) # prints the number value of the no.of samples in positive dataset
print(num_neg_samples)
data= []
labels = []
# compute HOG features and label them:
#/content/positive_images/22.png
for file in pos_im_listing: #this loop enables reading the files in the pos_im_listing variable one by one
img = Image.open(pos_im_path + '\\' + file) # open the file
#img = img.resize((64,128))
gray = img.convert('L') # convert the image into single channel i.e. RGB to grayscale
# calculate HOG for positive features
fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm='L2', feature_vector=True)# fd= feature descriptor
data.append(fd)
labels.append(1)
# Same for the negative images
for file in neg_im_listing:
img= Image.open(neg_im_path + '/' + file)
#img = img.resize((64,128))
gray= img.convert('L')
# Now we calculate the HOG for negative features
fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm='L2', feature_vector=True)
data.append(fd)
labels.append(0)
# encode the labels, converting them from strings to integers
le = LabelEncoder()
labels = le.fit_transform(labels)```