如何解决Python cv2断言失败
我正在尝试运行对象检测脚本,并且不断出现以下错误,我也检查了图像是否在文件夹中正确的位置,并且具有与脚本所需相同的尺寸:
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-44-c4a2d5093dc2> in <module>
26
27 # Call predict() function
---> 28 predict(test_dataset)
<ipython-input-44-c4a2d5093dc2> in predict(test_dataset)
2 for idx in range(len(test_dataset)):
3 print(idx)
----> 4 image,top_pad,bottom_pad,left_pad,right_pad = test_dataset[idx]
5 # Convert the image to Pytorch tensor ['CHW' format]
6 x_tensor = torch.from_numpy(image).to(DEVICE).unsqueeze(0)
<ipython-input-39-c47004d473ea> in __getitem__(self,i)
25
26 # read test image
---> 27 image = cv2.cvtColor(cv2.imread(self.image_paths[i]),cv2.COLOR_BGR2RGB)
28 width,height,_ = image.shape
29 # calculate total height and width padding required
error: OpenCV(4.4.0) /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/pip-req-build-5arl9blp/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
错误功能的代码如下:
class BuildingDataset(torch.utils.data.Dataset):
def __getitem__(self,i):
# read test image
image = cv2.cvtColor(cv2.imread(self.image_paths[i]),cv2.COLOR_BGR2RGB)
width,_ = image.shape
# calculate total height and width padding required
w_pad = math.ceil(width/16)*16 - width
h_pad = math.ceil(height/16)*16 - height
# calculate padding required from 4 sides
left_pad = w_pad//2
right_pad = w_pad - left_pad
top_pad = h_pad//2
bottom_pad = h_pad - top_pad
# pad the image
image = np.pad(image,((top_pad,bottom_pad),(left_pad,right_pad),(0,0)),'constant')
# apply preprocessing
if self.preprocessing:
sample = self.preprocessing(image=image)
image = sample['image']
return image,right_pad
我意识到,如果将图像放入以前从未使用过的新文件夹中,它可以工作,但结果令人沮丧。任何帮助将不胜感激。
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。