Python:从函数中获取数据

时间:2017-07-01 13:22:26

标签: python python-2.7

我试图从此功能获取数据:

def read_images(path, sz=None):
"""Reads the images in a given folder, resizes images on the fly if size is given.

Args:
    path: Path to a folder with subfolders representing the subjects (persons).
    sz: A tuple with the size Resizes

Returns:
    A list [X,y]

        X: The images, which is a Python list of numpy arrays.
        y: The corresponding labels (the unique number of the subject, person) in a Python list.
"""
c = 0
X,y = [], []
for dirname, dirnames, filenames in os.walk(path):
    for subdirname in dirnames:
        subject_path = os.path.join(dirname, subdirname)
        for filename in os.listdir(subject_path):
            try:
                im = Image.open(os.path.join(subject_path, filename))
                im = im.convert("L")
                # resize to given size (if given)
                if (sz is not None):
                    im = im.resize(self.sz, Image.ANTIALIAS)
                X.append(np.asarray(im, dtype=np.uint8))
                y.append(c)
            except IOError, (errno, strerror):
                print "I/O error({0}): {1}".format(errno, strerror)
            except:
                print "Unexpected error:", sys.exc_info()[0]
                raise
        c = c+1
return [X,y]

问题是当我调用函数

[X,y] = read_images('/Trainer')

其中Trainer是包含图像的文件夹。 现在,当我调用此函数时,函数无法到达文件夹,并且X和y的值仍为空。我试图打印X并打印y和两个值,其中X = [] y = [] 我也尝试过这样做

TrainerPath = "C:\Users\Eng. Aladdin Hammodi\Desktop\recognizer\Trainer"
[X,y] = read_images(TrainerPath) 

但仍然有同样的事情。我该怎么办?

2 个答案:

答案 0 :(得分:1)

public PriceList updatePriceList(final PriceList priceList) throws PriceListNotFoundException { final PriceListItem priceListItem = createPriceListItem(task, price); priceList.getPriceListItems().add(priceListItem); return priceListDao.save(priceList); } 符号会转义字符串中的字符。尝试使用原始字符串修饰符,如下所示: \

答案 1 :(得分:0)

如果你逃避" \"会发生什么? - 通过在路径前放置和r(r' C:\ Users ....')或类似&C; \\用户......'?