Python Opencv图像
下面是代码我的Read_img函数有问题,当我运行image变量时它返回一个错误 这里是错误Python Opencv图像,python,opencv,Python,Opencv,下面是代码我的Read_img函数有问题,当我运行image变量时它返回一个错误 这里是错误 def Read_img(path): img = cv2.imread(path) (h, w) = img.shape[:2] WIDTH = 500 RATIO = WIDTH / float(w) HEIGHT = int(h * RATIO) + 50 return cv2.resize(img, (WIDTH, HEIGHT)) for na
def Read_img(path):
img = cv2.imread(path)
(h, w) = img.shape[:2]
WIDTH = 500
RATIO = WIDTH / float(w)
HEIGHT = int(h * RATIO) + 50
return cv2.resize(img, (WIDTH, HEIGHT))
for names in os.listdir(known_faces_dir):
print(names)
for file_name in os.listdir(f'{known_faces_dir}/{names}'):
print(file_name)
image = Read_img(f'{known_faces_dir}/{names}')
文件“Facial_Rec.py”,第23行,在
image=Read\u img(f'{known\u faces\u dir}/{names})
文件“Facial_Rec.py”,第12行,在Read_img`中输入代码`
(h,w)=图像形状[:2]
AttributeError:“非类型”对象没有属性“形状”
请帮忙 当您尝试打开图像但路径不正确时,OpenCV不会告诉您它找不到图像,它会返回None。因此,当您想要检查形状时,如果类型为“无”,则会出现错误,只需继续并传递到下一个循环
File "Facial_Rec.py", line 23, in <module>
image = Read_img(f'{known_faces_dir}/{names}')
File "Facial_Rec.py", line 12, in Read_img`enter code here`
(h, w) = img.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'
看起来您正在尝试将文件夹作为图像读取。试试
image=Read\u img(f'{known\u faces\u dir}/{names}/{file\u name}')
。
if type(img) is None:
continue