我有两种机器学习模型。当我想读取和显示路径中有多少文件夹时,我的模型之一运行良好。在我的第二个模型中,我尝试使用相同的代码来读取文件夹,但它给了我错误。
输出应该是这样的:
There are 40 images in category 1
There are 40 images in category 2
这是我运行以产生上述输出的代码(这是我的第一个 ml 模型):
for category in category_im_listing: # Read files one by one
im_listing = os.listdir(dataset_path + "/" + category)
num_im = size(im_listing)
print("There are " + str(num_im) + " images in category " + str(count + 1))
for file in im_listing:
img = Image.open(dataset_path + "/" + category + "/" + file) # open file
img = img.resize((150,150))
gray = img.convert('L') # convert the image to single channel
# calculate HOG
fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm='L2', feature_vector=True)
data.append(fd)
labels.append(count)
count = count + 1
然而,当我尝试对我的第二个模型使用相同的代码时,它显示如下输出:
There are 50 images in category 1
1
1
1
1
2
2
2
2
2
2
3
3
3
3
3
3
3
1
1
1
1
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
1
1
1
1
2
这里是我在我的第二个模型上运行的代码,它导致我收到类似上面的错误,在我的第一个代码中,我使用 Image.open 但在这里我将其更改为 cv2.imread 因为当我尝试使用完全相同代码就像在第一个模型中一样,它给了我错误。但是如果我更改为 cv2 它只能读取一个类别文件夹:
for category in category_im_listing: #reading files one by one
im_listing = os.listdir(image_path + "/" + category)
num_im = size(im_listing)
print("There are " + str(num_im) + " images in category " + str(count + 1))
for file in im_listing:
img=cv2.imread(image_path + "/" + category + "/" + file) # I changed Image.open to cv2.imread
# calculate PHOG
p = phog(img, bin, angle, L, roi)#I changed this part
data.append(p)
labels.append(count)
count = count + 1
如何修复我的代码以产生我在第一个模型中的产品输出?