【问题标题】:TypeError : 'int' object is not iterableTypeError : \'int\' 对象不可迭代
【发布时间】:2023-01-22 17:46:44
【问题描述】:

尝试预处理视频帧以进行异常事件检测。模型已经过训练,但我无法用以下代码找出问题。(初学者)。请帮助这位受人尊敬的开发人员。

` def Fit_Preprocessing(path: object, frames_ext: object) -> object:

if frames_ext is None:
    raise TypeError(
        'Invalid Value for argument `frames_ext`, it cannot be None. Give proper extensions of the frames e.g: `.tif` or `.png` etc!')
print('\n\nProcessing Images in this Dataset Path: {0}\n'.format(path))
file_names: List[Union[str, List[str]]]
onlyfiles, file_names, dirs = ReadFileNames(path, frames_ext)
img_list = [1, 2, 3, 4]
for img in tqdm(range(len(onlyfiles))):
    images = onlyfiles[img]
    count = 0
    for images in onlyfiles[img]:
        img.split('/')
        img_name = dirs[i] + '_' + file_names[i][count]
        write_path = 'ProcessedImages/' + path.split('/')[1]
        gray = ProcessImg(img_name, read_path=img, write=True,
                          write_path=write_path, res_shape=(227, 227))
        img_list.append(gray)
        count += 1
return img_list

` 收到此错误:

在此数据集中处理图像路径:C:/Users/Public/Downloads/Sur​​veillance with deep learning/Datasets/UCSD_Anomaly_Dataset.v1p2/UCSDped1/Test

0%| | 0/47 [00:00<?, ?it/s] 回溯(最后一次通话): 文件“C:/Users/Public/Downloads/Sur​​veillance-with-deep-learning/preprocessing.py”,第 171 行,位于 img_list: object = Fit_Preprocessing(路径, frames_ext='.Fit') 文件“C:/Users/Public/Downloads/Sur​​veillance-with-deep-learning/preprocessing.py”,第 154 行,在 Fit_Preprocessing 中 对于 onlyfiles[img] 中的图像: TypeError: 'int' 对象不可迭代

进程结束,退出代码为 1

1 尝试使用 images = img_list 来修复循环但它不起作用(初学者)

【问题讨论】:

    标签: python-3.7


    【解决方案1】:

    您收到错误的原因是因为在 for 循环中:

    for images in onlyfiles[img]:
    

    您正在使用索引获取值来访问它,onlyfiles[img] 将根据您的代码返回一个 int 值。因为我不知道你在做什么,我的建议是把onlyfiles[img]变成list,所以它只迭代一件事或多件事,使用:

    for images in [onlyfiles[img]]:
    

    例子:

    my_int = 123
    for i in my_int:
        print(i)
    

    得到错误:

    Traceback (most recent call last):
      File "main.py", line 2, in <module>
        for i in my_int:
    TypeError: 'int' object is not iterable
    

    所以如果你把它变成一个列表:

    my_int = 123
    for i in [my_int]:
        print(i)
    

    给出:

    123
    

    或者如果你想遍历数字,把它变成一个字符串:

    my_int = 123
    for i in str(my_int):
        print(i)
    

    给出:

    1
    2
    3
    

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 1970-01-01
      • 2018-09-29
      • 2020-02-26
      • 2015-04-06
      • 2013-10-31
      • 2018-03-14
      相关资源
      最近更新 更多