类型错误:“JpegImageFile”对象不可迭代

时间:2021-01-01 17:46:44

标签: python tensorflow deep-learning

我在 python 错误类型“JpegImageFile”对象不可迭代中创建函数时遇到错误

请建议如何解决这个问题。

def image_processing(test_path, img_size=(331, 331, 3)):
    test_filenames = [i for i in (test_path)]
    images = np.zeros([img_size[0], img_size[1],3],dtype=np.uint8)
    for ix, img_dir in enumerate(tqdm(test_filenames)):
        img = load_img(img_dir, target_size=img_size)
        images[ix]=img
        del img
    print('test image data:', images.shape)
    return images 
   
X = image_processing(image, img_size)

完整错误

TypeError: 'JpegImageFile' object is not iterable
Traceback:
File "c:\programdata\anaconda3\lib\site-packages\streamlit\script_runner.py", line 324, in _run_script
    exec(code, module.__dict__)
File "E:\Dog-120-Breeds\app_3.py", line 77, in <module>
    X = image_processing(image, img_size)
File "E:\Dog-120-Breeds\app_3.py", line 50, in image_processing
    test_filenames = [i for i in (test_path)]

1 个答案:

答案 0 :(得分:0)

我看不到你在一个函数之外定义了什么,但是看看你的变量名,你似乎将图像作为 test_path 传递到函数中,你可能想在其中传递路径.

此外,如果您使用 test_filenames = [i for i in (test_path)] 行的目的是获取路径下所有文件的文件名,您可能需要查看 os.listdir() - 您当前的方法只会迭代路径。