Search code examples
pythonnumpysubprocessnumpy-ndarraynumpy-ufunc

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


I am reading in images from directories and as I loop through file names I get error mentioned in the title. The variable 'imagePath' is the path to image in my local machine. When 'np.fromfile(imagePath)' is removed the code runs, it even will print the image's path, but blows up when I try to read them in with 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 (most recent call last)

<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' did not contain a loop with signature matching types dtype('


Solution

  • You're using the variable images to store:

    1. A list of filenames
    2. A list of images

    As a result, you end up doing

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

    And in the last case, it obviously fails.

    Try choosing two different variable names.