GantMan / nsfw_model

Keras model of NSFW detector
Other
1.8k stars 279 forks source link

Crash while classifying a list of images: isdir("list") raises exception #81

Open Davidelanz opened 3 years ago

Davidelanz commented 3 years ago

I am running Python 3.6.9 64-bit on a Ubuntu 18.04.5 docker container.

I spotted a bug for the "predict multiple images at once" feature:

# Predict multiple images at once
predict.classify(model, ['/Users/bedapudi/Desktop/2.jpg', '/Users/bedapudi/Desktop/6.jpg'])

The exception raised is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/#########/nsfw_model/test_script.py in 
      25    '/Users/bedapudi/Desktop/6.jpg'])
      26 ]
----> 27 predict.classify(model, imglist)

//#########/nsfw_model/nsfw_detector/predict.py in classify(model, input_paths, image_dim)
     67 def classify(model, input_paths, image_dim=IMAGE_DIM):
     68     """ Classify given a model, input paths (could be single string), and image dimensionality...."""
---> 69     images, image_paths = load_images(input_paths, (image_dim, image_dim))
     70     probs = classify_nd(model, images)
     71     return dict(zip(image_paths, probs))

//#########/nsfw_model/nsfw_detector/predict.py in load_images(image_paths, image_size, verbose)
     33     #else:
     34     if True:
---> 35         if isdir(image_paths):
     36             parent = abspath(image_paths)
     37             image_paths = [join(parent, f) for f in listdir(

/usr/lib/python3.6/genericpath.py in isdir(s)
     40     """Return true if the pathname refers to an existing directory."""
     41     try:
---> 42         st = os.stat(s)
     43     except OSError:
     44         return False

TypeError: stat: path should be string, bytes, os.PathLike or integer, not list

I guess the problem was in

def load_images(image_paths, image_size, verbose=True):

    loaded_images = []
    loaded_image_paths = []

    if isdir(image_paths):
        parent = abspath(image_paths)
        image_paths = [join(parent, f) for f in listdir(image_paths) if isfile(join(parent, f))]
    elif isfile(image_paths):
        image_paths = [image_paths]

I fixed it in the following way:

def load_images(image_paths, image_size, verbose=True):

    loaded_images = []
    loaded_image_paths = []

    if type(image_paths) == list:
        pass
    else:
        if isdir(image_paths):
            parent = abspath(image_paths)
            image_paths = [join(parent, f) for f in listdir(image_paths) if isfile(join(parent, f))]
        elif isfile(image_paths):
            image_paths = [image_paths]

Did I got something wrong or it is a legit fix?