Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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_Opencv_Python Import - Fatal编程技术网

Python指定文件位置目录

Python指定文件位置目录,python,opencv,python-import,Python,Opencv,Python Import,我正在Windows7机器上使用Python64位空闲,试图为opencv机器学习编写一个脚本。我试图找出为什么我下面的代码找不到我的Windows PC上图片文件所在的目录 有更好的方法吗?在空闲状态下,shell输出中不会发生任何事情,我希望打印功能能够正常工作 我的C:驱动器上有一个名为faces的文件夹中的.py文件,如下所示。 C:\Users\ben\Documents\Python\opencv\Faces\images\ben import cv2 import os impor

我正在Windows7机器上使用Python64位空闲,试图为opencv机器学习编写一个脚本。我试图找出为什么我下面的代码找不到我的Windows PC上图片文件所在的目录

有更好的方法吗?在空闲状态下,shell输出中不会发生任何事情,我希望打印功能能够正常工作

我的C:驱动器上有一个名为faces的文件夹中的.py文件,如下所示。 C:\Users\ben\Documents\Python\opencv\Faces\images\ben

import cv2
import os
import numpy as np
from PIL import Image
import pickle

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
image_dir = os.path.join(BASE_DIR, "images")

for root, dirs, files in os.walk(image_dir):
    for file in files:
        if file.endswith("png") or file.endswith("JPEG"):
            path = os.path.join(root, file)
            label = os.path.basename(root).replace(" ", "-").lower()
            print(path)
图片文件位于Faces\images\ben

import cv2
import os
import numpy as np
from PIL import Image
import pickle

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
image_dir = os.path.join(BASE_DIR, "images")

for root, dirs, files in os.walk(image_dir):
    for file in files:
        if file.endswith("png") or file.endswith("JPEG"):
            path = os.path.join(root, file)
            label = os.path.basename(root).replace(" ", "-").lower()
            print(path)

因为,您的代码文件被放置在Faces文件夹中 图像路径如下所示

loc = os.path.join(os.path.join(os.path.dirname(__file__),"images"),"ben")

如果您的python文件在

C:\Users\ben\Documents\Python\opencv\Faces  
C:\Users\ben\Documents\Python\opencv\Faces\images\ben
图片也在里面

C:\Users\ben\Documents\Python\opencv\Faces  
C:\Users\ben\Documents\Python\opencv\Faces\images\ben
然后:

如果仍然存在问题,请尝试手动生成映像\u dir:

image_dir = "%s\%s\%s"%(BASE_DIR, "images", "ben")