Traceback (most recent call last): File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 99, in <module> img_transformed = apply(img, torchvision.transforms.RandomErasing(p=0.9, scale=(0.2, 0.33), ratio=(0.3, 3.3), value='random'),"zg10_") File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 38, in apply Y = [aug(img) for _ in range(num_rows * num_cols)] File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 38, in <listcomp> Y = [aug(img) for _ in range(num_rows * num_cols)] File "C:\Users\sxq\AppData\Roaming\Python\Python310\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl return forward_call(*input, **kwargs) File "C:\Users\sxq\AppData\Roaming\Python\Python310\site-packages\torchvision\transforms\transforms.py", line 1748, in forward x, y, h, w, v = self.get_params(img, scale=self.scale, ratio=self.ratio, value=value) File "C:\Users\sxq\AppData\Roaming\Python\Python310\site-packages\torchvision\transforms\transforms.py", line 1697, in get_params img_c, img_h, img_w = img.shape[-3], img.shape[-2], img.shape[-1] File "C:\ProgramData\anaconda3\lib\site-packages\PIL\Image.py", line 529, in __getattr__ raise AttributeError(name) AttributeError: shape. Did you mean: 'save'?
时间: 2023-06-30 20:19:01 浏览: 55
根据报错信息,出现了 AttributeError: shape,意味着程序试图在一个没有 shape 属性的对象上调用该属性。根据代码,这个错误可能是因为 img 不是一个 Numpy 数组或 PyTorch 张量,而是一个 PIL 图像对象。PIL 图像对象没有 shape 属性,而应该使用 size 属性。你可以更改代码,将 img.shape 改为 img.size 或者将 PIL 图像对象转换为 Numpy 数组或 PyTorch 张量。
相关问题
Traceback (most recent call last): File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 90, in <module> apply(img, torchvision.transforms.RandomHorizontalFlip(0.7), "zg1_") File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 59, in apply Load_name[i] = new_name1 ValueError: could not convert string to float: 'C:/Users/sxq/PycharmProjects/pythonProject/ZGCS/zg1_1_000014.jpg'
这个错误是因为你正在尝试将一个字符串类型的文件名赋值给一个浮点类型的numpy数组。在代码的第59行,你正在将一个新的文件名赋值给一个numpy数组,但是numpy数组的数据类型是浮点类型,而文件名是字符串类型,因此会发生此错误。
要解决这个问题,你需要将numpy数组的数据类型更改为字符串类型。你可以使用以下代码来初始化numpy数组:
```
Load_name = np.empty(Y, dtype="U100")
```
这将创建一个大小为Y的numpy数组,数据类型为字符串类型。然后,你可以将文件名赋值给numpy数组,例如:
```
Load_name[i] = str(new_name1)
```
这将确保将字符串类型的文件名正确地赋值给numpy数组。
[ WARN:0@0.488] global loadsave.cpp:244 cv::findDecoder imread_('002.jpg'): can't open/read file: check file path/integrity Traceback (most recent call last): File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 162, in <module> ZG_load = ZG('C:/Users/sxq/PycharmProjects/pythonProject/ZGCS/002.bmp',10) File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 156, in ZG ZG_load = applycc(img_tensor, torchvision.transforms.RandomErasing(p=0.9, scale=(0.02, 0.33), ratio=(0.3, 3.3), value='random'), type_zg) File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 78, in applycc if len(img_if.shape) == 2: AttributeError: 'NoneType' object has no attribute 'shape'
这个错误提示表明 OpenCV 无法打开指定的文件,可能是文件路径不正确或文件已经损坏。你需要检查文件路径是否正确以及文件是否存在。如果文件已经损坏,你可以尝试使用其他软件打开或者重新下载文件。
另外,当 `cv2.imread()` 函数无法打开指定的文件时,它将返回一个 `None` 对象,而 `None` 对象没有 `shape` 属性,因此会导致 `'NoneType' object has no attribute 'shape'` 的错误。你需要在处理图片之前,先检查图片是否成功读取。例如:
```python
img_if = cv2.imread(file_path)
if img_if is not None:
# 处理图片...
else:
print("无法打开文件:", file_path)
```