存储文件夹和内部文件夹的路径
Storing the path to folders and inner folders
我在尝试读取文件夹内的子文件夹时遇到困难。我想做的是:我有我的路径“C:\Dataset”,其中有 2 个文件夹和两个文件夹,我有带有图片的人名,例如:“C:\Dataset\Drunk\JohnDoe\Pic1”、“C :\Dataset\Sober\JaneDoe\Pic1”。我希望能够读取每张图片并将它们存储在路径变量中。
目前,我到目前为止所得到的,基本上,只要它们在 Drunk and Sober 中,我就会得到图像,例如:'C:\Dataset\Drunk\Pic1',以及我正在使用的代码这是:
DATADIR = "C:\Dataset"
CATEGORIES = ["Positive", "Negative"]
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
break
break
基本上,我想做的是,当我在 Drunk 文件夹中迭代时,它也会在内部文件夹中迭代,读取 C:\Dataset\Drunk\JohnDoe\nthPic、C:\Dataset\Drunk\JoeDoe\nthPic、C:\Dataset\Drunk 和 Sober \nthJoe\nthPic C:\Dataset\Drunk\JamesDoe\nthPic。因此,当我执行 cv2.imread 时,它会抓取整个文件夹映射
这基本上就是我的目标。
你还需要一个嵌套:
它将所有图像保存在字典images
中,关键是完整路径。
DATADIR = "C:\Dataset"
CATEGORIES = ["Drunk", "Sober"]
images = {}
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
for person in os.listdir(path):
personfolder = os.path.join(path, person):
for imgname in os.listdir(personfolder):
fullpath = os.path.join(personfolder, imgname)
images[fullpath] = cv2.imread(fullpath, cv2.IMREAD_GRAYSCALE)
我在尝试读取文件夹内的子文件夹时遇到困难。我想做的是:我有我的路径“C:\Dataset”,其中有 2 个文件夹和两个文件夹,我有带有图片的人名,例如:“C:\Dataset\Drunk\JohnDoe\Pic1”、“C :\Dataset\Sober\JaneDoe\Pic1”。我希望能够读取每张图片并将它们存储在路径变量中。
目前,我到目前为止所得到的,基本上,只要它们在 Drunk and Sober 中,我就会得到图像,例如:'C:\Dataset\Drunk\Pic1',以及我正在使用的代码这是:
DATADIR = "C:\Dataset"
CATEGORIES = ["Positive", "Negative"]
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
break
break
基本上,我想做的是,当我在 Drunk 文件夹中迭代时,它也会在内部文件夹中迭代,读取 C:\Dataset\Drunk\JohnDoe\nthPic、C:\Dataset\Drunk\JoeDoe\nthPic、C:\Dataset\Drunk 和 Sober \nthJoe\nthPic C:\Dataset\Drunk\JamesDoe\nthPic。因此,当我执行 cv2.imread 时,它会抓取整个文件夹映射
这基本上就是我的目标。
你还需要一个嵌套:
它将所有图像保存在字典images
中,关键是完整路径。
DATADIR = "C:\Dataset"
CATEGORIES = ["Drunk", "Sober"]
images = {}
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
for person in os.listdir(path):
personfolder = os.path.join(path, person):
for imgname in os.listdir(personfolder):
fullpath = os.path.join(personfolder, imgname)
images[fullpath] = cv2.imread(fullpath, cv2.IMREAD_GRAYSCALE)