从Python中的不同文件夹中读取文件列表

发布于 2025-02-06 15:40:06 字数 1321 浏览 1 评论 0原文

for root, dirs, files in os.walk(os.getcwd()):
    for dirct in dirs:
    lis_sal_map =[]
    gt = ''
    for name in files:
    
       (base, ext) = os.path.splitext(name) # split base and extension
        if ext in ('.jpg'):          # check the extension
            gt = name
            print("gt old is", gt)
        if ext in ('.png'):
        #full_name = os.path.join(root, name)
            lis_sal_map.append(name)
    print("list sal map", lis_sal_map) 
        
    
print("gt before file", gt)
img_gt = Image.open(gt)
numpydata_in = np.array(img_gt).astype('uint8')
numpydata_out = list_sal_map[0]
nss = nss(numpydata_in, numpydata_out)
print("nss is", nss)

我有18个文件夹,每个文件夹包含10张图像,一个图像作为地面真理,另一些作为预测的图像。我正在尝试编写一个程序,该程序将逐一访问这18个文件夹并比较每个文件夹中的图像。您能帮我解决问题吗?我获得的当前输出如下:

list sal map []
list sal map []
gt befoe file 
Traceback (most recent call last):

fp.seek(0)
AttributeError: 'str' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

Trace back (most recent call last):
File "result_auto.py", line 61, in <module>
img_gt = Image.open(gt)
File "anaconda3/lib/python3.8/site-packages/PIL/Image.py", line 2897, in open
fp = io.BytesIO(fp.read())
AttributeError: 'str' object has no attribute 'read'
for root, dirs, files in os.walk(os.getcwd()):
    for dirct in dirs:
    lis_sal_map =[]
    gt = ''
    for name in files:
    
       (base, ext) = os.path.splitext(name) # split base and extension
        if ext in ('.jpg'):          # check the extension
            gt = name
            print("gt old is", gt)
        if ext in ('.png'):
        #full_name = os.path.join(root, name)
            lis_sal_map.append(name)
    print("list sal map", lis_sal_map) 
        
    
print("gt before file", gt)
img_gt = Image.open(gt)
numpydata_in = np.array(img_gt).astype('uint8')
numpydata_out = list_sal_map[0]
nss = nss(numpydata_in, numpydata_out)
print("nss is", nss)

I have 18 folders each containing 10 images, with one as ground truth and others as predicted images. I am trying to write a program that will access these 18 folders one by one and compare the images within each folder. Can you help me with where it is going wrong? The current output that I am getting is like below:

list sal map []
list sal map []
gt befoe file 
Traceback (most recent call last):

fp.seek(0)
AttributeError: 'str' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

Trace back (most recent call last):
File "result_auto.py", line 61, in <module>
img_gt = Image.open(gt)
File "anaconda3/lib/python3.8/site-packages/PIL/Image.py", line 2897, in open
fp = io.BytesIO(fp.read())
AttributeError: 'str' object has no attribute 'read'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文