TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U78') dtype('<U78') dtype('<U78'

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U78') dtype('<U78') dtype('<U78'

我正在从目录中读取图像,当我遍历文件名时,出现标题中提到的错误。变量 'imagePath' 是我本地机器中图像的路径。当删除 'np.fromfile(imagePath)' 代码运行时,它甚至会打印图像的路径,但是当我尝试用 numpy 读取它们时会爆炸。

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 Traceback(最后一次调用)

<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)

TypeError: ufunc 'add' 不包含签名匹配类型 dtype('

您正在使用变量 images 来存储:

  1. 文件名列表
  2. 图片列表

结果,你最终做了

"/" + "file1.png"
"/" + "your-gravatar.jpg"
"/" +

而在最后一种情况下,它显然失败了。

尝试选择两个不同的变量名。