python - 为什么 cv2.imread 无法从我的路径文件夹中读取我的所有文件夹?
问题描述
我有两种类型的机器学习模型。当我想读取和显示路径中有多少个文件夹时,我的一个模型运行良好。在我的第二个模型中,我尝试使用相同的代码来读取文件夹,但它给了我错误。
输出应该是这样的:
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
如何修复我的代码以产生像我在第一个模型中一样的产品输出?
解决方案
推荐阅读
- python - 通过 pyodbc 从 Python 连接到 Db2 时出错
- java - 如何将“hadoop jar”命令迁移到 GCP?
- dc.js - dc.js 表——选择行,突出显示 onclick 并为 Crossfilter 应用过滤器
- json - ESP8266 和 Arduino UNO 之间的通信问题
- python - 如何将值添加到 aa 列表?
- jmeter - Jmeter for REST API 中线程数和加速周期之间的理想比率是多少
- php - Mysql Like 查询不匹配记录
- javascript - 成功登录后初始状态未更新
- python - Databricks:OSError:[Errno 22] 无效参数
- javascript - Mongoose updateOne() 正常但没有更新