0

Currently I'm working on a face recognition project. I'm suing the following code so python can see the images that I have put for each person.

import os

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(".jpg"):
            path = os.path.join(root, file)
            print(path)

So, when I try to print the path using command prompt, it gives me nothing it just exist. It's supposed to give all the pictures in the images folder. So what seem to be the problem?

Dan Mašek
  • 17,852
  • 6
  • 57
  • 85
mar
  • 37
  • 5
  • what do you mean it just exists, the cmd is supposed to close when the code ends, so your code is not ending and is stuck somewhere? – Mahrkeenerh Nov 12 '21 at 12:23
  • 1
    add more prints into your code and verify you are in the directory you think you are-. also add prints for "else: ...." for each if so you get feedback on not passing conditions – Patrick Artner Nov 12 '21 at 12:33
  • @Mahrkeenerh Yes sorry. It just doesn't print anything. – mar Nov 12 '21 at 12:36
  • @PatrickArtner I added else, it goes to the else part. Somehow it's unable to go to see the images – mar Nov 12 '21 at 12:44
  • 1
    add `print(image_dir)` - remove the os.walk (you never walk into subdirs anyway - why os.walk? => https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory ) , copy the exact output,. change to console, cd into your copied value. `dir` it and look if any files that fit your filter are in it. Or just comment your if-condition for now and see what (if any) files are found at all – Patrick Artner Nov 12 '21 at 12:48
  • used these codes "print("path is %s " % path.curdir) print("dirname = %s " % path.dirname(path.curdir)) print("basename = %s" % path.basename(path.curdir))" – mar Nov 12 '21 at 23:27
  • the dirname came out empty. Do you think that might be the problem? – mar Nov 12 '21 at 23:27
  • and I did what you said. print(image_dir) ths was the output C:\Users\person\PycharmProjects\pythonProject\Images. it's in the right path – mar Nov 12 '21 at 23:30

0 Answers0