import os
import cv2
folder = ['test images', 'ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER',
'SHARK', 'YFT' ]
Path = r'D:\ncfm\train'
for i in range(9):
listing = os.listdir(path+'/'+folder[i])
folder[i+1] = np.array([np.array(cv2.imread(path+'/'+folder[i]+'/'+file)).flatten()
for file in listing])
FileNotFoundError Traceback (most recent call
last)
<ipython-input-152-d8f8c2149488> in <module>
5
6 for i in range(9):
----> 7 listing = os.listdir(path+'/'+folder[i])
8 folder[i+1] =
np.array([np.array(cv2.imread(path+'/'+folder[i]+'/'+file)).flatten()
9 for file in listing])
FileNotFoundError: [WinError 3] The system cannot find the path specified:
'Users\\USER\\Desktop\\ncfmtrain\\YFT\\*.jpg/test images'
我已尝试纠正许多次。但是问题仍然存在。然后我尝试了对我有用的这段代码。
import os
from os import listdir
for i in range(9):
for fld in folders:
index = folders.index(fld)
print('Load folders {} (Index: {})'.format(fld, index))
path = os.path.join('Users', 'USER' , 'Desktop','ncfm' 'train', fld, '*.jpg')
L.append(len(path))
break
这对我来说很好。但随后出现以下错误:
ValueError: Found input variables with inconsistent numbers of samples: [8, 3777]
我想这些是相关的。
答案 0 :(得分:0)
使用pathlib进行文件系统访问。
from pathlib import Path
jpeg_images = list(Path('D:/ncfm/train').glob('**/*.jpg'))
print(jpeg_images)
np.array([np.array(cv2.imread(str(file))).flatten() for file in jpeg_images])
'Users\\USER\\Desktop\\ncfmtrain\\YFT\*.jpg/test images'
的评论
pathlib
测试一个文件
file = Path(r'C:\Users\USER\Desktop\ncfmtrain\YFT\image_name.jpg')
print(cv2.imread(str(file))).flatten())