Python 类型错误:ufunc';添加';未包含具有签名匹配类型dtype(';<;U78';)dtype(';<;U78';)dtype(';<;U78';)的循环;
我正在从目录中读取图像,当我循环浏览文件名时,我发现标题中提到了错误。变量“imagePath”是本地计算机中图像的路径。当删除“np.fromfile(imagePath)”时,代码会运行,它甚至会打印图像的路径,但当我尝试使用numpy读取它们时,它会崩溃Python 类型错误:ufunc';添加';未包含具有签名匹配类型dtype(';<;U78';)dtype(';<;U78';)dtype(';<;U78';)的循环;,python,numpy,subprocess,numpy-ndarray,numpy-ufunc,Python,Numpy,Subprocess,Numpy Ndarray,Numpy Ufunc,我正在从目录中读取图像,当我循环浏览文件名时,我发现标题中提到了错误。变量“imagePath”是本地计算机中图像的路径。当删除“np.fromfile(imagePath)”时,代码会运行,它甚至会打印图像的路径,但当我尝试使用numpy读取它们时,它会崩溃 def getTrainingDataFromFile(): for subdir, dirs, images in os.walk(directory): for sub, dirs, images in os.walk(sub
def getTrainingDataFromFile():
for subdir, dirs, images in os.walk(directory):
for sub, dirs, images in os.walk(subdir):
for currentImage in images:
imagePath = str(os.getcwd() + "/" + sub.replace("./", "") + "/" + currentImage)
if '.jpg' in imagePath:
face = np.fromfile(imagePath)
images.append(face)
TypeError回溯(最近一次调用上次) () ---->1 getTrainingDataFromFile() 在getTrainingDataFromFile()中 16对于操作系统中的sub、Dir和图像。漫游(subdir): 17对于图像中的当前图像: --->18 thisImage=str(os.getcwd()+“/”+sub.replace(“./”,”)+“/”+currentImage) 19如果此图像中的“.jpg”: 20 face=np.fromfile(此图像,dtype=np.uint8)
TypeError:ufunc“add”不包含签名类型与dtype匹配的循环(“您正在使用变量
images
存储:
“/”+“您的gravatar.jpg”
“/”+ 在最后一种情况下,它显然失败了
请尝试选择两个不同的变量名。请显示整个错误消息,包括回溯,而不仅仅是其中的一部分。
np.fromfile
不知道如何加载jpeg文件(或任何常见的图像格式)-您需要imageio.imread(imagePath)
。哇!我需要睡觉了
<ipython-input-8-ce35c0ab49e6> in <module>()
----> 1 getTrainingDataFromFile()
<ipython-input-7-ae9589186aa3> in getTrainingDataFromFile()
16 for sub, dirs, images in os.walk(subdir):
17 for currentImage in images:
---> 18 thisImage = str(os.getcwd() + "/" + sub.replace("./", "") + "/" + currentImage)
19 if '.jpg' in thisImage:
20 face = np.fromfile(thisImage,dtype=np.uint8)