Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:从函数获取数据_Python_Python 2.7 - Fatal编程技术网

Python:从函数获取数据

Python:从函数获取数据,python,python-2.7,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 Res

我正在尝试从此函数获取数据:

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) 

但还是有同样的东西。我该怎么办?

符号将转义字符串中的字符。尝试使用原始字符串修饰符,如下所示:
TrainerPath=r“C:\Users\Eng.Aladdin Hammodi\Desktop\recognizer\Trainer”
如果您通过在路径(r'C:\Users…)之前放置和r或者像“C:\\Users…”?

您是否尝试过使用
os.listdir('C:\Users\Eng.Aladdin Hammodi\Desktop\recognizer\Trainer')列出您的文件,我没有。我到底应该在代码中添加什么?正是我上面写的。在开始处添加它并打印输出。我在os.listdir('C:\Users\Eng.Aladdin Hammodi\Desktop\Recognitizer\Trainer')WindowsError:[错误123]文件名、目录名或卷标语法不正确:“C:\\Users\\Eng.Aladdin Hammodi\\Desktop\Recognitizer\\Trainer/*。”您调试过吗?os.walk(…)返回什么,只是一个空生成器还是一个错误?它在代码中的哪一点断开了?文件“C:\Python27\lib\site packages\numpy\core\shape\u base.py”,第234行,在vstack return\u nx.concatenate([对于tup中的_m,至少是_2d(_m)),0)ValueError:除了连接轴之外的所有输入数组维度都必须完全匹配>>>这是特定于连接numpy数组的方式,不看数据就什么都说不出来。也许可以尝试发布一个单独的问题,因为这与这个问题无关。我把它贴在这里了