账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    用os的join遍历图片文件时出错了
    44
    0

    用os的join遍历图片文件时出错了

    import os
    import tensorflow as tf
    with tf.Session() as sess:
        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
        #遍历目录
        for root,dirs,file in os.walk('data/train/'):
                #载入图片
                image_data = tf.gfile.FastGFile(os.path.join(root,file), 'rb').read()
                predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})#图片格式是jpg格式
                predictions = np.squeeze(predictions)#把结果转为1维数据
     
                #打印图片路径及名称
                image_path = os.path.join(root,file)
                print(image_path)

    报错信息:

    File "D:\Anaconda3\envs\tensorflow-gpu\lib\genericpath.py", line 149, in _check_arg_types
        (funcname, s.__class__.__name__)) from None
    TypeError: join() argument must be str or bytes, not 'list'

    求大神帮帮忙吧 !

    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    • 情深必傷 普通会员 1楼

      在Python中,os库用于操作文件系统,其中join函数用于连接多个字符串或文件路径,并返回一个字符串,将它们连接起来。如果图片文件包含非数字字符,那么join函数将无法正确地连接它们,因为os中join函数只能处理字符串和数字字符。

      以下是一个例子,展示如何在os中使用join函数连接包含数字字符的图片文件名:

      ```python import os

      图片文件路径

      image_files = ['image1.jpg', 'image2.jpg', 'image3.jpg']

      试图连接包含数字字符的文件名

      combined_image_files = os.path.join(*image_files, '/')

      print(combined_image_files) ```

      在这个例子中,我们试图将所有的图片文件名连接到 '/'. 这将导致以下错误:

      'startfile.jpg' 是一个文件名,但不能与 'image1.jpg'、'image2.jpg' 和 'image3.jpg' 连接。

      这是因为join函数将 '/' 解析为一个字符串,然后试图连接这些字符串。然而,'/' 不是一个有效的字符串,因为它不是一个有效的字符集。在Python中,'/' 是一个路径字符,用于表示当前目录的根目录,而不是文件的路径。

      解决这个问题的方法是在文件名中加入反斜杠(\)以确保它们是有效的字符集。以下是一个更新后的例子:

      ```python import os

      图片文件路径

      image_files = ['image1.jpg', 'image2.jpg', 'image3.jpg']

      试图连接包含数字字符的文件名

      combined_image_files = os.path.join(*image_files, '/images')

      print(combined_image_files) ```

      在这个例子中,我们添加了反斜杠 'images',并将它们连接到 '/images'。这样,即使图像文件包含数字字符,join函数也会正确地连接它们。现在,'image1.jpg'、'image2.jpg' 和 'image3.jpg' 都会被正确地连接到 '/images' 文件夹中。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部