Traceback (most recent call last): File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 157, in <module> ZG_load = ZG('C:/Users/sxq/PycharmProjects/pythonProject/ZGCS/002.bmp',10) File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 151, in ZG ZG_load = apply(img, 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 51, in apply Y = [aug(img) for _ in range(num_rows * num_cols)] File "C:\Users\sxq\PycharmProjects\pythonProject\main.py", line 51, 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-07-21 18:52:54 浏览: 165
这个错误提示是因为在PIL库中的Image对象没有shape属性,而你的代码中却使用了img.shape来获取图像的尺寸信息。如果你需要获取图像的尺寸信息,可以使用img.size来获取,它返回的是一个元组,包含图像的宽度和高度。你需要修改代码中获取图像尺寸的部分,将img.shape改为img.size即可。
相关问题
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)
```
阅读全文