我正在尝试加载以下文件:
'data/chapter_1/capd_yard_signs\\Dueñas_2020.png'
但是当我这样做时,cv2.imread 返回一个错误:
imread_('data/chapter_1/capd_yard_signs\Due├▒as_2020.png'): can't open/read file: check file path/integrity load file
当我用os.path.join指定文件名时,我尝试对文件进行编码和解码
f = os.path.join("data/chapter_1/capd_yard_signs", filename.encode().decode())
但这并没有解决问题。
我错过了什么?
这就是我最终让它工作的方式:
from PIL import Image
pil = Image.open(f).convert('RGB') # load the image with pillow and make sure it is in RGB
pilCv = np.array(pil) # convert the image to an array
img = pilCv[:,:,::-1].copy() # convert the array to be in BGR
好吧...这应该有效:
import cv2, numpy as np
pic = cv2.imdecode(np.fromfile(os.path.join("data/chapter_1/capd_yard_signs", filename).replace('\\','/'), dtype=np.uint8), 0)
np.fromfile 首先读取非 ASCII 字符,.replace('\','/') 也应该解决您的不均匀 / 和 \ 问题。然后 imdecode 完成它的工作。 (这里是灰度。对于颜色,将 0 替换为 cv2.IMREAD_COLOR。)