【问题标题】:TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U78') dtype('<U78') dtype('<U78'TypeError: ufunc 'add' 不包含签名匹配类型的循环 dtype('<U78') dtype('<U78') dtype('<U78'
【发布时间】:2019-03-27 06:04:25
【问题描述】:

我正在从目录中读取图像,当我遍历文件名时,我得到标题中提到的错误。变量“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(' 的循环

【问题讨论】:

  • 请显示整个错误消息,包括回溯,而不仅仅是其中的一部分
  • np.fromfile 不知道如何加载 jpeg 文件(或者实际上是任何常见的图像格式) - 你需要 imageio.imread(imagePath)

标签: python numpy subprocess numpy-ndarray numpy-ufunc


【解决方案1】:

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

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

结果,你最终做了

"/" + "file1.png"
"/" + "你的头像.jpg"
"/" +

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

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

【讨论】:

  • 哇!我要睡觉了
猜你喜欢
  • 2018-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 2016-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多